Skip to content

Commit 6b515c7

Browse files
fix(@cypress/react): Correctly unmount react components (#15250)
1 parent 6f02719 commit 6b515c7

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

npm/react/src/hooks.ts

-17
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,6 @@ export function setupHooks (rootId: string) {
1616
}
1717
})
1818

19-
/** This function stays here only for old experimental component-testing */
20-
function renderTestingPlatform () {
21-
if (document.getElementById(rootId)) {
22-
return
23-
}
24-
25-
const rootNode = document.createElement('div')
26-
27-
rootNode.setAttribute('id', rootId)
28-
document.getElementsByTagName('body')[0].prepend(rootNode)
29-
30-
const selector = `#${rootId}`
31-
32-
return cy.get(selector, { log: false })
33-
}
34-
3519
/**
3620
* Remove any style or extra link elements from the iframe placeholder
3721
* left from any previous test
@@ -64,7 +48,6 @@ export function setupHooks (rootId: string) {
6448
return
6549
}
6650

67-
renderTestingPlatform()
6851
cleanupStyles()
6952
})
7053
}

npm/react/src/mount.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import ReactDOM, { unmountComponentAtNode } from 'react-dom'
2+
import * as ReactDOM from 'react-dom'
33
import getDisplayName from './getDisplayName'
44
import { injectStylesBeforeElement } from './utils'
55
import { setupHooks } from './hooks'
@@ -50,7 +50,7 @@ export const mount = (jsx: React.ReactNode, options: MountOptions = {}) => {
5050
// @ts-ignore
5151
let logInstance: Cypress.Log
5252

53-
return cy
53+
return unmount({ log: false })
5454
.then(() => {
5555
if (options.log !== false) {
5656
logInstance = Cypress.log({
@@ -154,13 +154,16 @@ export const mount = (jsx: React.ReactNode, options: MountOptions = {}) => {
154154
})
155155
```
156156
*/
157-
export const unmount = () => {
157+
export const unmount = (options = { log: true }) => {
158158
return cy.then(() => {
159-
cy.log('unmounting...')
160159
const selector = `#${ROOT_ID}`
161160

162161
return cy.get(selector, { log: false }).then(($el) => {
163-
unmountComponentAtNode($el[0])
162+
const wasUnmounted = ReactDOM.unmountComponentAtNode($el[0])
163+
164+
if (wasUnmounted && options.log) {
165+
cy.log('Unmounted component at', $el)
166+
}
164167
})
165168
})
166169
}

npm/webpack-dev-server/src/aut-runner.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ export function init (importPromises, parent = (window.opener || window.parent))
2626
Cypress.onSpecWindow(window, importPromises)
2727
Cypress.action('app:window:before:load', window)
2828

29-
beforeEach(() => {
30-
// This really have no sense for @cypress/react and @cypress/vue
31-
// It is anyway required to mount a new
29+
// Before all tests we are mounting the root element, **not beforeEach**
30+
// Cleaning up platform between tests is the responsibility of the specific adapter
31+
// because unmounting react/vue component should be done using specific framework API
32+
// (for devtools and to get rid of global event listeners from previous tests.)
33+
before(() => {
3234
appendTargetIfNotExists('__cy_root')
3335
})
3436

0 commit comments

Comments
 (0)