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

Documentation on how to use test-utils without babel/webpack #1192

Closed
anatolsommer opened this issue Mar 23, 2019 · 6 comments · Fixed by #1373
Closed

Documentation on how to use test-utils without babel/webpack #1192

anatolsommer opened this issue Mar 23, 2019 · 6 comments · Fixed by #1373
Assignees
Labels

Comments

@anatolsommer
Copy link

anatolsommer commented Mar 23, 2019

What problem does this feature solve?

The documentation explains how to use vue-test-utils with different runners and build setups but not how to test a plain component without babel / webpack. Since Vue itself works great without using single file components or a build step it would be great to provide at least one simple example on how to use vue-test-utils in that case.

I have tried some obvious ways and asked for help on the forum and SO more than two weeks ago but since no one could help this really seems to be not documented at all.

https://forum.vuejs.org/t/how-to-test-plain-vue-components-not-single-file-components/58415
https://stackoverflow.com/questions/55052983/how-to-test-plain-vue-components-not-single-file-components

What does the proposed API look like?

var testUtils=require('@vue/test-utils'), Vue=require('vue');

require('jsdom-global')();    

testUtils.mount({ template: '<div>test</div>' });
// or
testUtils.mount(Vue.component('test', { template: '<div>test</div>' }));

would be obvious but fails with this error:

@vue/test-utils/dist/vue-test-utils.js:2471
var componentInstance = node.child;
TypeError: Cannot read property 'child' of undefined
@lmiller1990
Copy link
Member

You need to load the DOM before requiring Vue. Change you code to this:

require('jsdom-global')();    
var testUtils=require('@vue/test-utils'), Vue=require('vue');
// ...

Likely Jest loads JSDOM in some script before Vue is required, which is why it works there. I am not entirely clear on why this the order makes a difference.

@anatolsommer
Copy link
Author

It's actually not Vue but @vue/test-utils that has to be required after jsdom, but you are right.

This fact should definitely be included in the documentation.

test-utils could also check if jsdom was loaded and throw a more meaningful error if not.

Thanks a lot!

@eddyerburgh
Copy link
Member

@aaronsum1102 thanks for raising this issue. Would you like to write the guide?

@kahirokunn
Copy link
Contributor

I got same error.
And then I tried fix as per this document.
https://jestjs.io/docs/en/24.0/configuration#testenvironment-string

testEnvironment: "jsdom",

My test got all green.

@kahirokunn
Copy link
Contributor

if you do this, you can remove jsdom package from your package.json.
jest contains jsdom.

@lmiller1990 lmiller1990 self-assigned this Dec 16, 2019
@lmiller1990
Copy link
Member

I'm interested in writing a quick guide on how to use test-utils without babel/webpack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants