vuejs / test-utils

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

Bun test runner support #2524

Closed rodhoward closed 1 month ago

rodhoward commented 1 month ago

I've been using test-utils for years! Thanks for all the hard work.

Having added lots of unit tests test performance becomes more important. I've been looking at bun as well as happy dom as a replacement for our existing unit tests in jest and jsdom.

I've followed the bun guid to set up happy dom and that seems to be working but adding test utils into the mix isn't working. I'm getting the following error:

1354 | // same stub, so we're safe and do not need to pass these stubs
1355 | // for each mount operation
1356 | var stubs = new WeakMap();
1357 | function registerStub(_a) {
1358 |     var source = _a.source, stub = _a.stub;
1359 |     stubs.set(stub, source);
                 ^
TypeError: WeakMap keys must be objects or non-registered symbols
      at registerStub (/home/rod/test/node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:1359:11)
      at createInstance (/home/rod/test/node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:8151:5)
      at mount (/home/rod/test/node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:8385:14)
      at /home/rod/test/src/components/___tests__/HelloWorld.spec.js:6:19
✗ dom test [2.00ms]

And my unit test looks like this:

import {test, expect} from 'bun:test';
import { mount } from '@vue/test-utils'
import Hello from "../HelloWorld.vue";

test('dom test', () => {
  const wrapper = mount(Hello, {msg: "bob's your uncle"});
  expect(wrapper.html()).toEqual("bob");

  // This is working fine:
  // document.body.innerHTML = `<button>My button</button>`;
  // const button = document.querySelector('button');
  // expect(button?.innerText).toEqual('My button');
});

I suspect this fix might need to come from bun.. but I'm hoping you might know better what exactly is the issue and can report it more accurately to them than I can. There might even be a feature request or bug in bun that relates to this issue that I could monitor?

Regards Rod

cexbrayat commented 1 month ago

I think you're running into https://github.com/oven-sh/bun/issues/5052

But I don't know if that's an error in bun/JavascriptCore or if the stubs WeakMap internally used by VTU is using some keys that aren't supposed to be used (reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Key_not_weakly_held).

If you're willing to take a look, you can check the key that is throwing and we'll see if that's a valid key and a bun issue or if the WeakMap is not correctly used in VTU and we should fix it.

rodhoward commented 1 month ago

mm I added some debugging and now I feel a little silly. I think the

import Hello from "../HelloWorld.vue";

Is just importing a file path or a string because bun doesn't have any plugin to tell it how to handle .vue files. This then causes the above error when trying to set the stubs:

stubs.set("src/components/HelloWorld.vue", "src/components/HelloWorld.vue");

https://github.com/oven-sh/bun/issues/5967

Thanks for the quick response.

cexbrayat commented 1 month ago

👍 Let's close then, thanks for looking into it.