Cypress 10+

背景

Cypress 10.0.0以后不再支持通过cypress/plugins/index.js配置插件。而是通过cypress.config.js的setupNodeEvents函数配置插件。

javascript 复制代码
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  // setupNodeEvents can be defined in either
  // the e2e or component configuration
  e2e: {
    setupNodeEvents(on, config) {
      // configure plugins here
      const _ = require('lodash') // yup, dev dependencies
      const path = require('path') // yup, core node library
      const debug = require('debug') // yup, dependencies
      const User = require('./lib/models/user') // yup, relative local modules

      console.log(__dirname) // /Users/janelane/Dev/my-project
      console.log(process.cwd()) // /Users/janelane/Dev/my-project

      on('<event>', (arg1, arg2) => {
        // plugin stuff here
      })            
    },
  },
})

配置文件说明

setupNodeEvents

on是一个用来注册cypress公开的各种事件的侦听器,setupNodeEvents中可以有多个侦听器。不同的事件及其代表的意义如下表

Event Description
after:run Occurs after the run is finished.
after:screenshot Occurs after a screenshot is taken.
after:spec Occurs after a spec is finished running.
before:browser:launch Occurs immediately before launching a browser.
before:run Occurs before the run starts.
before:spec Occurs when a spec is about to be run.
file:preprocessor Occurs when a spec or spec-related file needs to be transpiled for the browser.
task Occurs in conjunction with the cy.task command.

config是当前项目打开时已解析的cypress配置,此配置包含传递到浏览器中的所有值,某些插件可能会使用或者需要这些值,如果以编程方式修改了这些值,cypress将使用新值。

例子

file:preprocessor事件如何使用@cypress/webpack-preprocessor进行构建和预处理

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default

相关推荐
是苏浙19 小时前
零基础入门C语言之贪吃蛇的实现
c语言·开发语言·数据结构
克里斯蒂亚L19 小时前
开发一个计时器组件
前端·浏览器
化作星辰19 小时前
java 给鉴权kafka2.7(sasl)发送消息权限异常处理
java·大数据·开发语言·kafka
无极小卒19 小时前
如何在三维空间中生成任意方向的矩形内部点位坐标
开发语言·算法·c#
克里斯蒂亚诺更新19 小时前
微信小程序 点击某个marker改变其大小
开发语言·前端·javascript
天才奇男子19 小时前
从零开始搭建Linux Web服务器
linux·服务器·前端
长空任鸟飞_阿康20 小时前
AI 多模态全栈应用项目描述
前端·vue.js·人工智能·node.js·语音识别
Mintopia20 小时前
🌐 实时协同 AIGC:多人在线 Web 创作的技术架构设计
前端·人工智能·trae
Mintopia20 小时前
🔥 “Solo Coding”的近期热度解析(截至 2025 年末)
前端·人工智能·trae
顾安r20 小时前
11.14 脚本网页 迷宫逃离
服务器·javascript·游戏·flask·html