diff --git a/src/mount.ts b/src/mount.ts index c0ad9d4bf..baf42a76c 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -420,6 +420,10 @@ export function mount( // ref: https://github.com/vuejs/vue-test-utils-next/issues/425 if (global?.stubs) { for (const [name, stub] of Object.entries(global.stubs)) { + // removes a previously component with the same name if it exists + // and avoids the warning from vue-next + // "Component has already been registered in target app." + delete app._context.components[name] if (stub === true) { const stubbed = createStub({ name, props: {} }) // default stub. diff --git a/tests/mountingOptions/stubs.global.spec.ts b/tests/mountingOptions/stubs.global.spec.ts index 0597f05ab..9a7d548ce 100644 --- a/tests/mountingOptions/stubs.global.spec.ts +++ b/tests/mountingOptions/stubs.global.spec.ts @@ -4,6 +4,7 @@ import { config, mount, RouterLinkStub } from '../../src' import Hello from '../components/Hello.vue' import ComponentWithoutName from '../components/ComponentWithoutName.vue' import ComponentWithSlots from '../components/ComponentWithSlots.vue' +import { createRouter, createWebHistory } from 'vue-router' describe('mounting options: stubs', () => { let configStubsSave = config.global.stubs @@ -83,6 +84,34 @@ describe('mounting options: stubs', () => { ) }) + it('does not warn if stubbing an already registered component', () => { + // a component with RouterView + const Comp = defineComponent({ + template: '' + }) + // we want to check if Vue does not log a warning because we register RouterView twice + jest.spyOn(console, 'warn') + + // let's register the router as a plugin (which registers the RouterView component once) + const router = createRouter({ + history: createWebHistory(), + routes: [{ path: '/', component: Comp }] + }) + // and let's stub RouterView, which registers RouterView a second time + const wrapper = mount(Comp, { + global: { + plugins: [router], + stubs: { + RouterView: true + } + } + }) + + expect(wrapper.html()).toBe('') + // check that no warning was emitted + expect(console.warn).not.toHaveBeenCalled() + }) + it('stubs a component without a name', () => { const Component = { template: '
',