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

相关推荐
持梦远方5 分钟前
C 语言基础入门:基本数据类型与运算符详解
c语言·开发语言·c++
abigale0310 分钟前
webpack+vite前端构建工具 -11实战中的配置技巧
前端·webpack·node.js
zzywxc78722 分钟前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
专注API从业者30 分钟前
构建淘宝评论监控系统:API 接口开发与实时数据采集教程
大数据·前端·数据库·oracle
Joker`s smile33 分钟前
Chrome安装老版本、不同版本,自制便携版本用于前端调试
前端·chrome
weixin_4166399736 分钟前
爬虫工程师Chrome开发者工具简单介绍
前端·chrome·爬虫
我是如子啊41 分钟前
【解决“此扩展可能损坏”】Edge浏览器(chrome系列通杀))扩展损坏?一招保留数据快速修复
前端·chrome·edge
灵性花火41 分钟前
Qt的前端和后端过于耦合(0/7)
开发语言·前端·qt
DES 仿真实践家2 小时前
【Day 11-N22】Python类(3)——Python的继承性、多继承、方法重写
开发语言·笔记·python
Code Warrior3 小时前
【每日算法】专题五_位运算
开发语言·c++