Commit a9476ec 1 parent 2880092 commit a9476ec Copy full SHA for a9476ec
File tree 9 files changed +78
-1
lines changed
9 files changed +78
-1
lines changed Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ export const getContainerEl = (): HTMLElement => {
46
46
return el
47
47
}
48
48
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. ` )
50
50
}
51
51
52
52
/**
@@ -200,6 +200,14 @@ export const injectStylesBeforeElement = (
200
200
}
201
201
202
202
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
+
203
211
// When running component specs, we cannot allow "cy.visit"
204
212
// because it will wipe out our preparation work, and does not make much sense
205
213
// thus we overwrite "cy.visit" to throw an error
Original file line number Diff line number Diff line change @@ -322,6 +322,15 @@ export declare namespace Cypress {
322
322
}
323
323
}
324
324
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
+
325
334
// it is required to unmount component in beforeEach hook in order to provide a clean state inside test
326
335
// because `mount` can be called after some preparation that can side effect unmount
327
336
// @see npm/react/cypress/component/advanced/set-timeout-example/loading-indicator-spec.js
Original file line number Diff line number Diff line change @@ -306,4 +306,12 @@ export function mountCallback (
306
306
}
307
307
}
308
308
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
309
317
setupHooks ( )
Original file line number Diff line number Diff line change @@ -426,4 +426,12 @@ export const mountCallback = (
426
426
return ( ) => mount ( component , options )
427
427
}
428
428
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
429
437
setupHooks ( )
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ import { mount } from 'cypress/react'
2
+
3
+ Cypress . Commands . add ( 'mount' , mount )
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments