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

相关推荐
程序员yu6 分钟前
数智码力:Python作用域完全梳理,全局变量局部变量不再混乱
开发语言·python·学习
你别说话了7 分钟前
Vue实现高效拖拽效果 vue-Draggable
前端·vue.js
万物智能15 分钟前
PWM散热风扇设置—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
前端·后端·算法
前端柱子26 分钟前
q-floodfill白边锯齿?一招搞定抗锯齿边缘问题
前端
阿虎儿27 分钟前
我的 HTML 样板(HTML Boilerplate)
前端·html
Amos_Web31 分钟前
Rspack 源码解析(八):Module、Chunk 与 Runtime ID
前端·rust·源码阅读
彭于晏分晏37 分钟前
深入理解 Vue 3 响应式原理:从 Proxy 到依赖收集
前端
晚安日记wanna40 分钟前
Vue3 script setup 的四层追问答到第三层才算过关
前端·vue.js·面试
不灭的黄金瞳12341 分钟前
Java数据类型与变量
java·开发语言·intellij-idea
前端柱子1 小时前
Chaikin‘s Corner Cutting 算法 应用canvas绘制平滑的曲线
前端