vue-test-utils: Window is undefined, vue-test-utils needs to be run in a browser environment.
Version
1.0.0-beta.24
Steps to reproduce
Packages:
- “@vue/test-utils”: “^1.0.0-beta.24”
- “jest-serializer-vue”: “^2.0.2”
- “jest”: “^23.6.0”
- “babel-jest”: “^21.2.0”
Run jest --config test/unit/jest.conf.js
My config file: `const path = require(‘path’)
module.exports = { rootDir: path.resolve(__dirname, ‘…/…/’), testEnvironment: ‘node’, moduleFileExtensions: [ ‘js’, ‘json’, ‘vue’ ], moduleNameMapper: { ‘^@/(.)$': ‘<rootDir>/src/$1’ }, transform: { ‘^. \.js$’: ‘<rootDir>/node_modules/babel-jest’, '.\.(vue)$’: ‘<rootDir>/node_modules/vue-jest’ }, snapshotSerializers: [‘<rootDir>/node_modules/jest-serializer-vue’], setupFiles: [‘<rootDir>/test/unit/setup’], collectCoverage:false, coverageThreshold: { “global”: { “branches”: 0, “functions”: 0, “lines”: 0 } } }`
What is expected?
Tests should have a status of PASS.
What is actually happening?
All tests have a status of FAIL for the reason above.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 20 (3 by maintainers)
Commits related to this issue
- fix(jest): use jsdom environment since jest v27 See https://github.com/vuejs/vue-test-utils/issues/999#issuecomment-850273753 — committed to ocean-tech-ship/ocean-pearl by marhali 2 years ago
if you updated jest to v27 and start seeing this problem, then it’s most likely because jest v27 flipped the default test environment.
https://jestjs.io/ja/blog/2021/05/25/jest-27#flipping-defaults
So you need to add
to jest.config.js to change back to the previous behavior.
since you hadn’t added jsdom to your test setup, it’s a pretty safe bet you didn’t read the docs, which is the least you should do before creating an issue on the github repo.
https://vue-test-utils.vuejs.org/guides/#getting-started
I’m also seeing
When trying to setup unit tests with my vue app. Unfortunately https://vue-test-utils.vuejs.org/guides/#browser-environment just leads to https://vue-test-utils.vuejs.org/guides/#getting-started and doesn’t provide any helpful info on configuring vue-test-utils.
It’s unclear if I need to add jsdom, or if it should just work with jest and vue-test-utils. I’ve likely misconfigured something.
@M3psipax - I did read the docs. Here is a quote:
So I did see JSDOM is setup automatically and my tests did in fact run without adding JSDOM previously. For some reason my tests stopped working which is why I ended up posting an issue.
If JSDOM really is required by default then the docs need to be updated.
Oh my bad. Sorry. I use mocha, so I had to do this anyway.
In that case, you’re justified and the docs at best seem to be not entirely clear about that.
Damm god thx a lot
I solved it with: jest.config.js add this line
setupFilesAfterEnv: ['<rootDir>jest.setup.js'],jest.setup.jsrequire('jsdom-global')()which is okay for now but feels like a bad solution in general.
Anyone getting the following error after the suggestions above?
My jest config looks like this:
The full code in question is
That seems pretty self-explanatory - my only suggestion would be to link to https://vue-test-utils.vuejs.org/guides/#browser-environment instead of https://vue-test-utils.vuejs.org/guides/common-tips.html.
As a non-jest user, I’m curious why
were necessary. Are those a code bug, a documentation bug, or a quirk of your setup?
Yes currently Vue Test Utils needs to run in a DOM environment. There is a section on it in the docs, and the error message is intended to be descriptive.
I’m open to suggestions for how to improve the docs/ error message.
Well, it seems you don’t have a browser environment for your tests…
npm i --save-dev jsdom-global
add the following line in /test/unit/setup:
require(‘jsdom-global’)
It works thanks.
Just remembering that I need also to run
npm i --save-dev jest-environment-jsdom.😄