ReferenceError: Vue is not defined while using shallowMount
See original GitHub issueI am working on vue project and while doing unit testing using jest, I am getting following error for shallowMount:
ReferenceError: Vue is not defined
> 1 | import { shallowMount } from '@vue/test-utils';
| ^
2 | import About from '@/views/About.vue';
Following are my devDependencies from package.json:
"devDependencies": {
"@babel/core": "^7.18.5",
"@babel/eslint-parser": "^7.12.16",
"@vue/cli-plugin-babel": "~5.0.4",
"@vue/cli-plugin-e2e-cypress": "~5.0.4",
"@vue/cli-plugin-eslint": "~5.0.4",
"@vue/cli-plugin-pwa": "~5.0.0",
"@vue/cli-plugin-router": "~5.0.4",
"@vue/cli-plugin-unit-jest": "^5.0.6",
"@vue/cli-plugin-vuex": "~5.0.4",
"@vue/cli-service": "^5.0.4",
"@vue/compiler-dom": "^3.2.37",
"@vue/compiler-sfc": "^3.0.0",
"@vue/eslint-config-airbnb": "^6.0.0",
"@vue/test-utils": "^2.0.0",
"@vue/vue3-jest": "^28.0.0",
"babel-jest": "^28.1.1",
"cypress": "^8.3.0",
"eslint": "^8.17.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-vue": "^9.1.0",
"eslint-plugin-vuejs-accessibility": "^1.1.0",
"jest": "^28.1.1",
"jest-environment-jsdom": "^28.1.1",
"typescript": "~3.9.3",
"vue-cli-plugin-i18n": "~2.3.1",
"vue-cli-plugin-tailwind": "~3.0.0",
"vue3-jest": "^27.0.0-alpha.1",
"webpack": "^5.73.0"
},
Following is my unit test code:
import { shallowMount } from '@vue/test-utils';
import About from '@/views/About.vue';
describe('About.vue', () => {
test('renders inner text', () => {
// eslint-disable-next-line no-bitwise
const wrapper = shallowMount(About);
expect(wrapper.text()).toContain('about');
});
});
Following is my Component code:
<template>
<div class='about'>
<h1>This is an about page</h1>
</div>
</template>
<script>
export default { name: 'about' };
</script>
Just wonder if I am doing something wrong with unit test or the component code or does the jest and other supporting libraries require version upgrade/downgrade?
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
ReferenceError: Vue is not defined while using shallowMount
The error means that some dep relies on Vue 2 instead of 3, but this is not confirmed by devDependencies. What are regular...
Read more >vue/test-utils ReferenceError: Vue is not defined - Get Help
Hi, I have a vue-cli app and I try to set up testing via jest. This is my package.json. ... I played with...
Read more >Help! My Vue.js Tests are Breaking! - Highland Solutions
ReferenceError : regeneratorRuntime is not defined. If you get “ReferenceError: regeneratorRuntime is not defined” trying to use async await in ...
Read more >Testing with Jest and Vue.js: Pocket guide - ITNEXT
So, why not create a pocket guide for later use? Let's start? Component State. One thing that you need to be careful is...
Read more >How fix __dirname not defined when using electron events ...
Then in your Vue component, you'd use the renderer process, ipcRendere, such as: import { ipcRenderer } from "electron"; export default { methods:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This answer fixed the problem for me: https://stackoverflow.com/a/72608494/13925126
don’t know why unfortunately, but works fine now.
relevant deps below - we use both testing-library and vue/test-utils
@thedamon pretty easy to find the explanation, it’s into Jest official docs: https://jestjs.io/docs/configuration#testenvironmentoptions-object
When
jest-environment-jsdom
is used, Jest automatically assume a “browser” import style, thus importing vue-utils browser buildcustomExportConditions
force Jest to use node imports, resulting in CJS build to be usedAnother way to solve this is to add an entry into
moduleNameMapper
as suggested here, but that would force you to add an entry for each package supporting “browser” export target I guess