@@ -40,6 +40,8 @@ const injectStyles = (options: MountOptions) => {
40
40
**/
41
41
export const mount = ( jsx : React . ReactNode , options : MountOptions = { } ) => _mount ( 'mount' , jsx , options )
42
42
43
+ let lastMountedReactDom : ( typeof ReactDOM ) | undefined
44
+
43
45
/**
44
46
* @see `mount`
45
47
* @param type The type of mount executed
@@ -62,6 +64,8 @@ const _mount = (type: 'mount' | 'rerender', jsx: React.ReactNode, options: Mount
62
64
. then ( ( ) => {
63
65
const reactDomToUse = options . ReactDom || ReactDOM
64
66
67
+ lastMountedReactDom = reactDomToUse
68
+
65
69
const el = document . getElementById ( ROOT_ID )
66
70
67
71
if ( ! el ) {
@@ -153,21 +157,23 @@ const _unmount = (options: { boundComponentMessage?: string, log: boolean }) =>
153
157
const selector = `#${ ROOT_ID } `
154
158
155
159
return cy . get ( selector , { log : false } ) . then ( ( $el ) => {
156
- const wasUnmounted = ReactDOM . unmountComponentAtNode ( $el [ 0 ] )
157
-
158
- if ( wasUnmounted && options . log ) {
159
- Cypress . log ( {
160
- name : 'unmount' ,
161
- type : 'parent' ,
162
- message : [ options . boundComponentMessage ?? 'Unmounted component' ] ,
163
- consoleProps : ( ) => {
164
- return {
165
- description : 'Unmounts React component' ,
166
- parent : $el [ 0 ] ,
167
- home : 'https://github.com/cypress-io/cypress' ,
168
- }
169
- } ,
170
- } )
160
+ if ( lastMountedReactDom ) {
161
+ const wasUnmounted = lastMountedReactDom . unmountComponentAtNode ( $el [ 0 ] )
162
+
163
+ if ( wasUnmounted && options . log ) {
164
+ Cypress . log ( {
165
+ name : 'unmount' ,
166
+ type : 'parent' ,
167
+ message : [ options . boundComponentMessage ?? 'Unmounted component' ] ,
168
+ consoleProps : ( ) => {
169
+ return {
170
+ description : 'Unmounts React component' ,
171
+ parent : $el [ 0 ] ,
172
+ home : 'https://github.com/cypress-io/cypress' ,
173
+ }
174
+ } ,
175
+ } )
176
+ }
171
177
}
172
178
} )
173
179
} )
@@ -176,13 +182,13 @@ const _unmount = (options: { boundComponentMessage?: string, log: boolean }) =>
176
182
// Cleanup before each run
177
183
// NOTE: we cannot use unmount here because
178
184
// we are not in the context of a test
179
- Cypress . on ( 'test:before:run' , ( ) => {
185
+ const preMountCleanup = ( ) => {
180
186
const el = document . getElementById ( ROOT_ID )
181
187
182
- if ( el ) {
183
- ReactDOM . unmountComponentAtNode ( el )
188
+ if ( el && lastMountedReactDom ) {
189
+ lastMountedReactDom . unmountComponentAtNode ( el )
184
190
}
185
- } )
191
+ }
186
192
187
193
/**
188
194
* Creates new instance of `mount` function with default options
@@ -317,4 +323,4 @@ export declare namespace Cypress {
317
323
// it is required to unmount component in beforeEach hook in order to provide a clean state inside test
318
324
// because `mount` can be called after some preparation that can side effect unmount
319
325
// @see npm/react/cypress/component/advanced/set-timeout-example/loading-indicator-spec.js
320
- setupHooks ( unmount )
326
+ setupHooks ( preMountCleanup )
0 commit comments