-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: remove last mounted component upon subsequent mount calls #24470
Changes from 9 commits
6b22650
5ec765a
ce2c587
4348a89
b62a7d5
8c2b946
0606cec
07bd700
36da3ca
3a2ad63
e28b764
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,24 +72,12 @@ type MountingOptions<Props, Data = {}> = Omit<VTUMountingOptions<Props, Data>, ' | |
|
||
export type CyMountOptions<Props, Data = {}> = MountingOptions<Props, Data> | ||
|
||
Cypress.on('run:start', () => { | ||
// `mount` is designed to work with component testing only. | ||
// it assumes ROOT_SELECTOR exists, which is not the case in e2e. | ||
// if the user registers a custom command that imports `cypress/vue`, | ||
// this event will be registered and cause an error when the user | ||
// launches e2e (since it's common to use Cypress for both CT and E2E. | ||
// https://github.com/cypress-io/cypress/issues/17438 | ||
if (Cypress.testingType !== 'component') { | ||
return | ||
} | ||
|
||
Cypress.on('test:before:run', () => { | ||
Cypress.vueWrapper?.unmount() | ||
const el = getContainerEl() | ||
const cleanup = () => { | ||
Cypress.vueWrapper?.unmount() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's no Actually wondered, as part of cleanup do we want to unset There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to remove the |
||
const el = getContainerEl() | ||
|
||
el.innerHTML = '' | ||
}) | ||
}) | ||
el.innerHTML = '' | ||
} | ||
|
||
/** | ||
* The types for mount have been copied directly from the VTU mount | ||
|
@@ -378,6 +366,9 @@ export function mount< | |
|
||
// implementation | ||
export function mount (componentOptions: any, options: any = {}) { | ||
// Remove last mounted component if cy.mount is called more than once in a test | ||
cleanup() | ||
|
||
// TODO: get the real displayName and props from VTU shallowMount | ||
const componentName = getComponentDisplayName(componentOptions) | ||
|
||
|
@@ -484,4 +475,4 @@ export function mountCallback ( | |
// import { registerCT } from 'cypress/<my-framework>' | ||
// registerCT() | ||
// Note: This would be a breaking change | ||
setupHooks() | ||
setupHooks(cleanup) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ import { | |
mount as testUtilsMount, | ||
VueTestUtilsConfigOptions, | ||
Wrapper, | ||
enableAutoDestroy, | ||
} from '@vue/test-utils' | ||
import { | ||
injectStylesBeforeElement, | ||
|
@@ -266,6 +265,10 @@ declare global { | |
} | ||
} | ||
|
||
const cleanup = () => { | ||
Cypress.vueWrapper?.destroy() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any need to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #24470 (comment) |
||
} | ||
|
||
/** | ||
* Direct Vue errors to the top error handler | ||
* where they will fail Cypress test | ||
|
@@ -280,14 +283,6 @@ function failTestOnVueError (err, vm, info) { | |
}) | ||
} | ||
|
||
function registerAutoDestroy ($destroy: () => void) { | ||
Cypress.on('test:before:run', () => { | ||
$destroy() | ||
}) | ||
} | ||
|
||
enableAutoDestroy(registerAutoDestroy) | ||
|
||
const injectStyles = (options: StyleOptions) => { | ||
return injectStylesBeforeElement(options, document, getContainerEl()) | ||
} | ||
|
@@ -336,6 +331,9 @@ export const mount = ( | |
wrapper: Wrapper<Vue, Element> | ||
component: Wrapper<Vue, Element>['vm'] | ||
}> => { | ||
// Remove last mounted component if cy.mount is called more than once in a test | ||
cleanup() | ||
|
||
const options: Partial<MountOptions> = Cypress._.pick( | ||
optionsOrProps, | ||
defaultOptions, | ||
|
@@ -442,4 +440,4 @@ export const mountCallback = ( | |
// import { registerCT } from 'cypress/<my-framework>' | ||
// registerCT() | ||
// Note: This would be a breaking change | ||
setupHooks() | ||
setupHooks(cleanup) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment only applicable in React 18 or should it be here as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only applicable to 18, react17 doesn't have the concept of
root
but just a rerender into the same element.