Skip to content
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(mount): avoid warning when stubbing #483

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions tests/mountingOptions/stubs.global.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: '<RouterView />'
})
// 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('<router-view-stub></router-view-stub>')
// check that no warning was emitted
expect(console.warn).not.toHaveBeenCalled()
})

it('stubs a component without a name', () => {
const Component = {
template: '<div><foo/></div>',
Expand Down