test-utils: Error in tests with slots in Mocha

Hi! I think this is more of a question than a Vue Test Utils issue really, but I was hoping any of you could give me any pointer to what to look for.

We use Karma + Mocha, and we’re seeing that tests that contain slots throw the following error: TypeError: Cannot convert undefined or null to object.

I have created a reproduction with both Mocha and Jest (see below) and this only happens with the former, and Jest works perfectly, that’s why I’m thinking it’s not VTU’s fault.

The only difference I’ve noticed is that Mocha tests use vue-test-utils-esm-bundler.js and Jest uses vue-test-utils.cjs.js. Maybe the problem is there?

I have tried using a function as suggested by @lmiller1990 here, but the problem with this approach is that if we pass HTML, it will be rendered as text. For instance, when passing () => '<strong>Hello</strong>' the result of wrapper.html() will be &lt;strong&gt;Hello&lt;/strong&gt;. This might be acceptable for many use cases, but maybe this is an unexpected behavior that needs to be addressed?

Reproduction

This is a Vue CLI project in which I’ve chosen Mocha+Chai as the testing option, and then I’ve added Jest.

https://github.com/victorborrasdev/vue-test-utils-mocha-slots

Run Jest tests. They should pass:

npm run test:jest

Run Mocha tests. They should throw the error:

npm run test:jest

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (11 by maintainers)

Most upvoted comments

Finally got some time to dig into this a bit, think I found a fix, PR is up!

I will give it a shot

My team did a bit more research and we found that this started happening v.2.0.0-rc.13. If we downgrade to v.2.0.0-rc.12 string slots work fine.

Seems that it may have been caused by this PR https://github.com/vuejs/test-utils/pull/815

We are using Mocha/Chai/Sinon with vue-cli (@vue/cli-plugin-unit-mocha)

If it’s still a problem, we should leave it open 👍 so other people know, and perhaps someone has a fix.