vuejs / test-utils

Vue Test Utils for Vue 3
https://test-utils.vuejs.org
MIT License
1.04k stars 244 forks source link

Bug: findComponent() without a wrapper no longer works with version >= 2.4.5 #2543

Open Louis-Stekhoven-Smith opened 2 days ago

Louis-Stekhoven-Smith commented 2 days ago

Describe the bug

using findComponent(Test) without a wrapper no longer works after upgrading version >=2.4.5

To Reproduce

import { h } from 'vue';
import { mount } from '@vue/test-utils';

function Test(props, context) {
    return h('div', {
        ...props,
        innerHTML: 'This is a test',
    });
}

describe('Mounting component', () => {
    test('with wrapper', () => {
        const Wrapper = {
            components: { Test },
            template: `<Test />`,
        };

        const wrapper = mount(Wrapper, {});
        const item = wrapper.findComponent(Test);
        expect(item.exists()).toBe(true);
    });

    // this test fails now, findComponent returns [Object: null prototype] {} 
    test('without wrapper', () => {
        const wrapper = mount(Test, {});
        const item = wrapper.findComponent(Test);
        expect(item.exists()).toBe(true);
        expect(item.html()).toStrictEqual('<div>This is a test</div>');
    });
});

Finding components by querySelector seems to still work though

        wrapper.findComponent('.foo')
        wrapper.findComponent('[data-test="foo"]')

Expected behavior Both tests should pass.

cexbrayat commented 2 days ago

hi @Louis-Stekhoven-Smith

Interesting. I'm not sure that's a big deal, as finding the component you just mounted is probably not very common. But if you really want to see this fix, feel free to open a PR, that would be great! The code you're looking for is probably the matches function https://github.com/vuejs/test-utils/blob/3988a3161074d3ea6bbcbc57995f4f2829a83b39/src/utils/find.ts#L22

cexbrayat commented 2 days ago

(and if you know which version introduced the regression, you can probably pinpoint the culprit commit by adding a unit test that reproduces your issue. You'll then have a good idea how to fix it)