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

相关推荐
我滴老baby几秒前
Navidrome:飞牛OS安装、曲库导入与远程收听全流程
开发语言·数据库·php
一颗小芹菜的日常1 分钟前
vue-crack插件是干什么的
前端·javascript·vue.js
code2cat3 分钟前
Java进阶篇之AtomicLongArray:大数累加、上限检查与溢出边界
java·开发语言
小比特-combat5 分钟前
LVGL_1(使用示例讲解父子关系)
开发语言·前端·javascript
IT_陈寒5 分钟前
Java线程池的坑把我埋了,踩出来的血泪教训
前端·人工智能·后端
开开心心_Every8 分钟前
电脑OCR识别软件支持表格识别图片转Excel
java·开发语言·微信·计算机外设·ocr·intellij-idea·excel
弯_弯9 分钟前
企业知识库 RAG 落地:从 Demo 到生产,四条工程链路与参数级自查
android·java·javascript
徐小黑ACG9 分钟前
Golang 基础03 函数
开发语言·后端·golang
月夏9 分钟前
Node.js 读 Excel 我踩了 3 个坑,最后一个查遍 Stack Overflow 没答案
前端·javascript
亿元程序员11 分钟前
PSD都不用了!Codex直接生成Cocos预制体
前端