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

相关推荐
阿珊和她的猫9 分钟前
Chrome 的 SameSite 属性:原理与解决方案
前端·chrome
百锦再9 分钟前
线程安全的单例模式全方位解读:从原理到最佳实践
java·javascript·安全·spring·单例模式·kafka·tomcat
belldeep18 分钟前
nodejs: 能在线编辑 Markdown 文档的 Web 服务程序,更多扩展功能
前端·node.js·markdown·mermaid·highlight·katax
程序员林北北22 分钟前
【前端进阶之旅】一种新的数据格式:TOON
前端·javascript·vue.js·react.js·typescript·json
木斯佳23 分钟前
前端八股文面经大全:2026-01-23快手AI应用方向前端实习一面面经深度解析
前端·人工智能·状态模式
百锦再23 分钟前
Java synchronized关键字详解:从入门到原理(两课时)
java·开发语言·struts·spring·kafka·tomcat·maven
linux_cfan24 分钟前
Chrome 彻底封杀 Flash 后,安防监控如何实现 RTSP 流无插件播放?(2026解决方案)
javascript·安全·实时音视频·html5
油丶酸萝卜别吃28 分钟前
什么是 Java 内存模型(JMM)?
java·开发语言
量子炒饭大师44 分钟前
【C++入门】Cyber神经的义体插件 —— 【类与对象】内部类
java·开发语言·c++·内部类·嵌套类
强子感冒了44 分钟前
Javascript学习笔记:BOM和DOM
javascript·笔记·学习