Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit ced36a8

Browse files
committed
[cypress] Avoid clobbering conflicting event handlers
This works around cypress-io/cypress#22428.
1 parent 48613d1 commit ced36a8

File tree

9 files changed

+42
-11
lines changed

9 files changed

+42
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2023 Developer Innovations, LLC
2+
3+
declare module "cypress-on-fix" {
4+
import * as Cypress from "cypress";
5+
export default function onProxy(
6+
on: Cypress.PluginEvents
7+
): Cypress.PluginEvents;
8+
}

packages/cypress-plugin/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"cli-table3": "0.5.1",
5353
"cosmiconfig": "^7.0.1",
5454
"cypress-multi-reporters": "^1.6.3",
55+
"cypress-on-fix": "^1.0.2",
5556
"dayjs": "^1.10.4",
5657
"debug": "^4.3.3",
5758
"deep-equal": "^2.0.5",

packages/cypress-plugin/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
ENV_VAR_AUTO_SUPPORT,
2020
ENV_VAR_UNFLAKABLE_RESOLVED_CONFIG_JSON,
2121
} from "./config-env-vars";
22+
import cypressOnFix from "cypress-on-fix";
2223

2324
export { PluginOptions };
2425

@@ -100,9 +101,14 @@ const wrapSetupNodeEvents =
100101
| undefined
101102
) =>
102103
async (
103-
on: Cypress.PluginEvents,
104+
baseOn: Cypress.PluginEvents,
104105
config: Cypress.PluginConfigOptions
105106
): Promise<Cypress.PluginConfigOptions> => {
107+
// Due to https://github.com/cypress-io/cypress/issues/22428, only the last event handler
108+
// registered for each event type will be called. This means we'll clobber any event handlers
109+
// the user registers. To avoid this, we use cypress-on-fix.
110+
const on = cypressOnFix(baseOn);
111+
106112
const userModifiedConfig =
107113
userSetupNodeEvents !== undefined
108114
? await userSetupNodeEvents(on, config)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../tsconfig.json",
3-
"include": ["../mocha.d.ts", ".", ".eslintrc.js"]
3+
"include": ["../cypress-on-fix.d.ts", "../mocha.d.ts", ".", ".eslintrc.js"]
44
}

packages/cypress-plugin/test/integration-input-manual/cypress-config.mjs

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ import { registerCosmiconfigMock } from "unflakable-test-common/dist/config.js";
88
import { registerUnflakable } from "@unflakable/cypress-plugin";
99
import semverGte from "semver/functions/gte.js";
1010
import path from "path";
11+
import cypressOnFix from "cypress-on-fix";
1112

1213
/**
1314
* @type {Cypress.ConfigOptions}
1415
*/
1516
export default {
1617
component: {
1718
/**
18-
* @param {Cypress.PluginEvents} on
19+
* @param {Cypress.PluginEvents} baseOn
1920
* @param {Cypress.PluginConfigOptions} config
2021
* @returns {Promise<Cypress.PluginConfigOptions | void> | Cypress.PluginConfigOptions | void}
2122
*/
22-
setupNodeEvents(on, config) {
23+
setupNodeEvents(baseOn, config) {
24+
const on = cypressOnFix(baseOn);
2325
registerCosmiconfigMock();
2426
registerSimpleGitMock();
2527
tasks.registerTasks(on);
@@ -35,11 +37,12 @@ export default {
3537
},
3638
e2e: {
3739
/**
38-
* @param {Cypress.PluginEvents} on
40+
* @param {Cypress.PluginEvents} baseOn
3941
* @param {Cypress.PluginConfigOptions} config
4042
* @returns {Promise<Cypress.PluginConfigOptions | void> | Cypress.PluginConfigOptions | void}
4143
*/
42-
setupNodeEvents(on, config) {
44+
setupNodeEvents(baseOn, config) {
45+
const on = cypressOnFix(baseOn);
4346
registerCosmiconfigMock();
4447
registerSimpleGitMock();
4548
tasks.registerTasks(on);

packages/cypress-plugin/test/integration-input-manual/cypress.config.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ const semverGte = require("semver/functions/gte");
1111

1212
const { registerUnflakable } = require("@unflakable/cypress-plugin");
1313
const path = require("path");
14+
const cypressOnFix = require("cypress-on-fix");
1415

1516
module.exports = {
1617
/**
1718
* @type {Cypress.ConfigOptions}
1819
*/
1920
component: {
2021
/**
21-
* @param {Cypress.PluginEvents} on
22+
* @param {Cypress.PluginEvents} baseOn
2223
* @param {Cypress.PluginConfigOptions} config
2324
* @returns {Promise<Cypress.PluginConfigOptions | void> | Cypress.PluginConfigOptions | void}
2425
*/
25-
setupNodeEvents(on, config) {
26+
setupNodeEvents(baseOn, config) {
27+
const on = cypressOnFix(baseOn);
2628
registerCosmiconfigMock();
2729
registerSimpleGitMock();
2830
registerTasks(on);
@@ -38,11 +40,12 @@ module.exports = {
3840
},
3941
e2e: {
4042
/**
41-
* @param {Cypress.PluginEvents} on
43+
* @param {Cypress.PluginEvents} baseOn
4244
* @param {Cypress.PluginConfigOptions} config
4345
* @returns {Promise<Cypress.PluginConfigOptions | void> | Cypress.PluginConfigOptions | void}
4446
*/
45-
setupNodeEvents(on, config) {
47+
setupNodeEvents(baseOn, config) {
48+
const on = cypressOnFix(baseOn);
4649
registerCosmiconfigMock();
4750
registerSimpleGitMock();
4851
registerTasks(on);

packages/cypress-plugin/test/integration-input-manual/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@unflakable/cypress-plugin": "workspace:^",
1212
"cypress": "11.2 - 13",
1313
"cypress-multi-reporters": "^1.6.3",
14+
"cypress-on-fix": "^1.0.2",
1415
"mocha": "=7.0.1",
1516
"mocha-junit-reporter": "^2.2.0",
1617
"process": "^0.11.10",

packages/cypress-plugin/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
// Avoids conflicting global definitions from, e.g., jest.
1616
"types": ["node"]
1717
},
18-
"include": ["mocha.d.ts", "rollup.config.mjs"]
18+
"include": ["cypress-on-fix.d.ts", "mocha.d.ts", "rollup.config.mjs"]
1919
}

yarn.lock

+9
Original file line numberDiff line numberDiff line change
@@ -3218,6 +3218,7 @@ __metadata:
32183218
cross-env: ^7.0.3
32193219
cypress: 11.2 - 13
32203220
cypress-multi-reporters: ^1.6.3
3221+
cypress-on-fix: ^1.0.2
32213222
dayjs: ^1.10.4
32223223
debug: ^4.3.3
32233224
deep-equal: ^2.0.5
@@ -4789,6 +4790,7 @@ __metadata:
47894790
"@unflakable/cypress-plugin": "workspace:^"
47904791
cypress: 11.2 - 13
47914792
cypress-multi-reporters: ^1.6.3
4793+
cypress-on-fix: ^1.0.2
47924794
mocha: =7.0.1
47934795
mocha-junit-reporter: ^2.2.0
47944796
process: ^0.11.10
@@ -4856,6 +4858,13 @@ __metadata:
48564858
languageName: node
48574859
linkType: hard
48584860

4861+
"cypress-on-fix@npm:^1.0.2":
4862+
version: 1.0.2
4863+
resolution: "cypress-on-fix@npm:1.0.2"
4864+
checksum: b35e0d49e4270237e7cbe95c21d458772d3df6bbb4423346c70f9417e61fdf061ad1d83aca76a854a378d001a68f50c17b8dd312fbe9c50b5d12e61fc317a785
4865+
languageName: node
4866+
linkType: hard
4867+
48594868
"cypress@npm:11.2 - 13":
48604869
version: 12.14.0
48614870
resolution: "cypress@npm:12.14.0"

0 commit comments

Comments
 (0)