Skip to content

Commit a9476ec

Browse files
authored
fix: remove CT side effects from mount when e2e testing (#22633)
1 parent 2880092 commit a9476ec

File tree

9 files changed

+78
-1
lines changed

9 files changed

+78
-1
lines changed

npm/mount-utils/src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const getContainerEl = (): HTMLElement => {
4646
return el
4747
}
4848

49-
throw Error(`No element found that matches selector ${ROOT_SELECTOR}. Please use the mount utils to mount it properly`)
49+
throw Error(`No element found that matches selector ${ROOT_SELECTOR}. Please add a root element with data-cy-root attribute to your "component-index.html" file so that Cypress can attach your component to the DOM.`)
5050
}
5151

5252
/**
@@ -200,6 +200,14 @@ export const injectStylesBeforeElement = (
200200
}
201201

202202
export function setupHooks (optionalCallback?: Function) {
203+
// Consumed by the framework "mount" libs. A user might register their own mount in the scaffolded 'commands.js'
204+
// file that is imported by e2e and component support files by default. We don't want CT side effects to run when e2e
205+
// testing so we early return.
206+
// System test to verify CT side effects do not pollute e2e: system-tests/test/e2e_with_mount_import_spec.ts
207+
if (Cypress.testingType !== 'component') {
208+
return
209+
}
210+
203211
// When running component specs, we cannot allow "cy.visit"
204212
// because it will wipe out our preparation work, and does not make much sense
205213
// thus we overwrite "cy.visit" to throw an error

npm/react/src/mount.ts

+9
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ export declare namespace Cypress {
322322
}
323323
}
324324

325+
// Side effects from "import { mount } from '@cypress/<my-framework>'" are annoying, we should avoid doing this
326+
// by creating an explicit function/import that the user can register in their 'component.js' support file,
327+
// such as:
328+
// import 'cypress/<my-framework>/support'
329+
// or
330+
// import { registerCT } from 'cypress/<my-framework>'
331+
// registerCT()
332+
// Note: This would be a breaking change
333+
325334
// it is required to unmount component in beforeEach hook in order to provide a clean state inside test
326335
// because `mount` can be called after some preparation that can side effect unmount
327336
// @see npm/react/cypress/component/advanced/set-timeout-example/loading-indicator-spec.js

npm/vue/src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,12 @@ export function mountCallback (
306306
}
307307
}
308308

309+
// Side effects from "import { mount } from '@cypress/<my-framework>'" are annoying, we should avoid doing this
310+
// by creating an explicit function/import that the user can register in their 'component.js' support file,
311+
// such as:
312+
// import 'cypress/<my-framework>/support'
313+
// or
314+
// import { registerCT } from 'cypress/<my-framework>'
315+
// registerCT()
316+
// Note: This would be a breaking change
309317
setupHooks()

npm/vue2/src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,12 @@ export const mountCallback = (
426426
return () => mount(component, options)
427427
}
428428

429+
// Side effects from "import { mount } from '@cypress/<my-framework>'" are annoying, we should avoid doing this
430+
// by creating an explicit function/import that the user can register in their 'component.js' support file,
431+
// such as:
432+
// import 'cypress/<my-framework>/support'
433+
// or
434+
// import { registerCT } from 'cypress/<my-framework>'
435+
// registerCT()
436+
// Note: This would be a breaking change
429437
setupHooks()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { defineConfig } = require('cypress')
2+
3+
module.exports = defineConfig({
4+
e2e: {
5+
supportFile: 'cypress/support/e2e-with-mount.js',
6+
},
7+
component: {
8+
specPattern: 'cypress/component-tests/*.spec.js',
9+
devServer: {
10+
bundler: 'webpack',
11+
},
12+
},
13+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
it('should pass with component mount registered', () => {
2+
// "cy.visit" is disabled when component testing due to a side effect of "import { mount } from 'cypress/{react,vue}'"
3+
// These side effects have been disabled when testingType === 'e2e' so this will now pass.
4+
cy.visit('./index.html')
5+
cy.contains('h1', 'Hello World')
6+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { mount } from 'cypress/react'
2+
3+
Cypress.Commands.add('mount', mount)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<h1>Hello World</h1>
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import systemTests from '../lib/system-tests'
2+
3+
// see: https://github.com/cypress-io/cypress/issues/22589
4+
systemTests.it('should not run CT side effects in e2e with mount registration', {
5+
project: 'component-tests',
6+
spec: 'passing-with-mount.cy.js',
7+
browser: 'chrome',
8+
configFile: 'cypress-e2e-mount-import.config.js',
9+
expectedExitCode: 0,
10+
})

0 commit comments

Comments
 (0)