TypeError: Cannot read properties of undefined (reading 'extend') - vuejs2

I using Jest 27.5.1 with TypeScript.
When I try to test, I got these error:
● Test suite failed to run
TypeError: Cannot read properties of undefined (reading 'extend')
2 | <div>Hello</div>
3 | </template>
> 4 |
| ^
5 | <script lang="ts">
6 | import Vue from 'vue'
7 | export default Vue.extend({
at src/pages/test.vue:4:1
at Object.<anonymous> (src/pages/test.vue:347:3)
at Object.<anonymous> (test/test.spec.ts:4:1)
at TestScheduler.scheduleTests (node_modules/#jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/#jest/core/build/runJest.js:404:19)
at _run10000 (node_modules/#jest/core/build/cli/index.js:320:7)
at runCLI (node_modules/#jest/core/build/cli/index.js:173:3)
files
test file(TypeScript):
import Vuetify from 'vuetify'
import { mount, createLocalVue } from '#vue/test-utils'
import test from '~/src/pages/test.vue'
const localVue = createLocalVue()
describe('Index', () => {
let vuetify: Vuetify
beforeEach(() => {
vuetify = new Vuetify()
})
test('is a Vue instance', () => {
const wrapper = mount(test, {
vuetify,
localVue,
})
expect(wrapper.vm).toBeTruthy()
})
})
jest.config.js:
module.exports = {
moduleNameMapper: {
'^#/(.*)$': '<rootDir>/$1',
'^~/(.*)$': '<rootDir>/$1',
'^vue$': 'vue/dist/vue.common.js',
},
moduleFileExtensions: ['ts', 'js', 'vue', 'json'],
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'babel-jest',
'.*\\.(vue)$': 'vue-jest',
},
collectCoverage: true,
collectCoverageFrom: [
'<rootDir>/src/components/**/*.vue',
'<rootDir>/src/pages/**/*.vue',
],
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['./test/setup.js'],
moduleDirectories: [__dirname, 'node_modules']
}
setup.js:
import Vue from 'vue'
import Vuetify from 'vuetify'
Vue.use(Vuetify)
package.json:
"dependencies": {
"#babel/core": "^7.19.3",
"#nuxtjs/axios": "^5.13.6",
"consola": "^2.15.3",
"core-js": "^3.19.3",
"dotenv": "^16.0.2",
"eslint": "^8.22.0",
"eslint-plugin-import": "^2.26.0",
"nuxt": "^2.15.8",
"nuxt-typed-vuex": "^0.3.1",
"ts-loader": "8.2.0",
"typed-vuex": "^0.3.1",
"typescript": "^4.8.4",
"vue": "^2.7.10",
"vue-server-renderer": "^2.6.14",
"vue-template-compiler": "^2.6.14",
"vuetify": "^2.6.10",
"vuex": "3.6.2",
"webpack": "^4.46.0"
},
"devDependencies": {
"#babel/eslint-parser": "^7.16.5",
"#nuxt/types": "^2.15.8",
"#nuxt/typescript-build": "^2.1.0",
"#nuxtjs/eslint-config-typescript": "^8.0.0",
"#nuxtjs/eslint-module": "^3.0.2",
"#nuxtjs/stylelint-module": "^4.1.0",
"#nuxtjs/vuetify": "^1.12.3",
"#types/jest": "^27.5.2",
"#types/node": "^18.7.18",
"#typescript-eslint/eslint-plugin": "^5.35.1",
"#typescript-eslint/parser": "^5.35.1",
"#vue/cli-plugin-unit-jest": "^5.0.8",
"#vue/test-utils": "^1.3.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^27.5.1",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-nuxt": "^3.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-vue": "^8.2.0",
"husky": "^8.0.1",
"jest": "^27.5.1",
"lint-staged": "^12.1.7",
"postcss-html": "^1.3.1",
"prettier": "^2.7.1",
"stylelint": "^14.1.0",
"stylelint-config-prettier": "^9.0.3",
"stylelint-config-recommended-vue": "^1.1.0",
"stylelint-config-standard": "^24.0.0",
"ts-jest": "^27.1.5",
"vue-jest": "^3.0.7"
}
What do I have to do to run the test correctly?
I don't know how to solve this problem myself, as it worked fine when I ran the same code in other environments.
Thank you for your help.

Mh seems the Vue instance cannot be reached. I'm also using vuetify with jest test and it look quite same. Also same as in the documentation by vuetify here https://vuetifyjs.com/en/getting-started/unit-testing/#spec-tests
The only difference I see is that the createLocalVue() initialization is outside of describe() in your case.
Try it like
describe('Index', () => {
let vuetify: Vuetify
const localVue = createLocalVue()
...
Instead of
const localVue = createLocalVue()
describe('Index', () => {
let vuetify: Vuetify
...

Related

Jest don't mount composition-api component on Vue 2.7 + TS + Vuetify with "vue-test-utils"

I'm trying to run unit test for composition-api component. I'm on Vue 2.7 with TS and the following packages:
"nuxt": "^2.15.8",
"#nuxtjs/composition-api": "^0.33.1",
"#vue/test-utils": "^1.3.0",
"jest": "^28.1.3",
The issue is the following error is thrown on component mount:
SyntaxError: Unexpected token (1:1141)
at Parser.pp$4.raise (node_modules/vue-template-es2015-compiler/buble.js:2757:13)
at Parser.pp.unexpected (node_modules/vue-template-es2015-compiler/buble.js:647:8)
at Parser.pp$3.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:2196:10)
at Parser.<anonymous> (node_modules/vue-template-es2015-compiler/buble.js:6003:24)
at Parser.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:6129:31)
at Parser.pp$3.parseExprSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2047:19)
at Parser.pp$3.parseMaybeUnary (node_modules/vue-template-es2015-compiler/buble.js:2024:17)
at Parser.pp$3.parseExprOps (node_modules/vue-template-es2015-compiler/buble.js:1966:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1949:19)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
The test example:
import { mount, createLocalVue } from '#vue/test-utils';
import CompositionApi from '#nuxtjs/composition-api';
import CustomSelect from '#/components/shared/CustomSelect';
import Vuetify from 'vuetify';
const localVue = createLocalVue();
localVue.use(CompositionApi);
let vuetify;
beforeEach(() => {
jest.clearAllMocks();
vuetify = new Vuetify();
});
describe('test select component', () => {
test('Mounts correclty', () => {
const wrapper = mount(CustomSelect, {
localVue,
vuetify,
});
expect(wrapper.vm).toBeTruthy();
});
});
The config:
// jest.config.js
module.exports = {
globalSetup: '<rootDir>/jest.setup.js',
setupFiles: ['<rootDir>/tests/jest.init.js'],
testEnvironment: 'jsdom',
moduleNameMapper: {
'^#/(.*)$': '<rootDir>/$1',
'^~/(.*)$': '<rootDir>/$1',
'^vue$': 'vue/dist/vue.common.js',
'^#nuxtjs/composition-api$':
'#nuxtjs/composition-api/dist/runtime/index.js',
},
moduleFileExtensions: ['js', 'vue', 'json'],
transform: {
'^.+\\.js$': 'babel-jest',
'.*\\.(vue)$': 'vue-jest',
'^.+\\.ts?$': 'ts-jest',
'.+\\.(css|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
},
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!(#nuxtjs/composition-api)/)',
],
snapshotSerializers: ['jest-serializer-vue'],
testMatch: ['<rootDir>/tests/unit/**/*.spec.js'],
};
package.json
{
.....
"test": "jest",
"test:watch": "jest --watch",
.....
},
"engines": {
"npm": "8.11",
"node": "16.16"
},
"dependencies": {
.....
"#babel/eslint-parser": "^7.18.9",
"#nuxt/types": "^2.15.8",
"#nuxt/typescript-build": "^2.1.0",
"#nuxtjs/composition-api": "^0.33.1",
"#nuxtjs/vuetify": "^1.12.3",
"#vueuse/core": "^9.3.1",
"flush-promises": "^1.0.2",
"jest-environment-jsdom": "^28.1.3",
"jest-mock-axios": "^4.7.0-beta",
"nuxt": "^2.15.8",
.....
},
"devDependencies": {
.....
"#babel/core": "^7.19.1",
"#babel/preset-env": "^7.19.1",
"#faker-js/faker": "^7.6.0",
"#vue/server-test-utils": "^1.3.0",
"#vue/test-utils": "^1.3.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^28.1.3",
"eslint": "^8.20.0",
"jest": "^28.1.3",
"jest-cli": "^28.1.3",
"jest-serializer-vue": "^2.0.2",
"jest-transform-stub": "^2.0.0",
"mockdate": "^3.0.2",
"vue-jest": "^3.0.7"
.....
}
}
I've tried all the suggestions from Jest docs, including: specifying "transformIgnorePatterns", specifying "moduleNameMapper", enabling "ECMAScript Modules" but it won't help.
What should be the working setup to test composition-api components on Vue 2.7 + Nuxt + Vuetify?

jest ReferenceError: Vue is not defined

We are introducing Jest to an existing project.
However, I wrote an example test code.
The above error occurred.
ReferenceError: Vue is not defined
1 | import User from "../components/modal/ad/AdAdd";
> 2 | import { mount } from "#vue/test-utils";
| ^
3 |
4 | describe("user component", () => {
5 | let wrapper;
How can I solve this??
//User.test.js
import User from "../components/modal/ad/AdAdd";
import { mount } from "#vue/test-utils";
describe("user component", () => {
let wrapper;
beforeEach(() => {
wrapper = mount(User);
});
test("render", () => {
expect(wrapper.vm.oSid).toBe(0);
});
});
export default {
data() {
return {
Sid: 0,
deleteList: [],
};
},
//package.json
"dependencies": {
"eslint-plugin-jest": "^26.2.2",
"vue": "^2.6.11",
"vuetify": "^2.4.0",
"vuex": "^3.4.0",
},
"devDependencies": {
"#babel/core": "^7.18.0",
"#babel/preset-env": "^7.18.0",
"#types/jest": "^27.5.1",
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-service": "^3.0.5",
"#vue/eslint-config-prettier": "^6.0.0",
"#vue/test-utils": "^2.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^28.1.0",
"eslint": "^6.7.2",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^6.2.2",
"jest": "^28.1.0",
"jest-environment-jsdom": "^28.1.0",
"speed-measure-webpack-plugin": "^1.5.0",
"uglifyjs-webpack-plugin": "^2.2.0",
"vue-cli-plugin-vuetify": "~2.4.0",
"vue-jest": "^3.0.7",
"vue-template-compiler": "^2.6.11",
"vuetify-loader": "^1.7.0"
}
I just mounted it, but it says the view is undefined. I don't know how to solve it.
What's wrong??
I haven't been able to solve the above error for several days.
I want to solve it.
Any help would be appreciated.
I had the same error with vue3 and jest v.28, what solved it was to add
testEnvironmentOptions: {
customExportConditions: ["node", "node-addons"],
},
into jest.config.js.
You are using Vue version 2 with #vue/test-utils 2.0.0 which is for Vue version 3.
You can find the correct #vue/test-utils version for Vue 2 here

Jest Nuxt Cannot read property 'child' of undefined

I have a problem with fire the Jest tests for the Nuxt/Vue components. Last day i connected all that need for test, and had resolve many errors with jsdom and babel issues, but can't resolve problem with Vue component.
The error is
TypeError: Cannot read property 'child' of undefined
4 | describe('Компонент Counter', () => {
5 | test("renders properly", () => {
> 6 | const wrapper = shallowMount(Greeting); // or mount - it's doesn't metter
| ^
7 | console.log(wrapper);
8 | });
9 |
My comoponent Greeting
<template>
<div> Hello World! </div>
</template>
<script>
export default {
name: 'Greeting'
};
</script>
My Greeting.spec.js
import {mount, shallowMount} from '#vue/test-utils';
import Greeting from '../../components/Greeting.vue';
require('jsdom-global')();
describe('Компонент Counter', () => {
test("renders properly", () => {
const wrapper = shallowMount(Greeting);
console.log(wrapper);
});
});
jest.config.js
module.exports = {
moduleFileExtensions: ["js", "json", "vue"],
watchman: false,
preset: "#nuxt/test-utils",
transform: {
"^.+\\.js$": "babel-jest",
".*\\.(vue)$": "#vue/vue2-jest"
},
};
package.json
"scripts": {
"test": "jest"
},
"devDependencies": {
"#nuxt/test-utils": "^0.2.2",
"#types/jest": "^27.0.3",
"#vue/cli-plugin-babel": "^4.5.15",
"#vue/test-utils": "^1.3.0",
"#vue/vue2-jest": "^27.0.0-alpha.4",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^8.2.1",
"babel-jest": "^27.4.5",
"eslint": "^4.15.0",
"eslint-friendly-formatter": "^3.0.0",
"eslint-loader": "^1.7.1",
"eslint-plugin-vue": "^4.0.0",
"jest": "^27.4.5",
"jsdom-global": "^3.0.2",
"node-sass": "^4.7.2",
"pug": "^2.0.3",
"pug-plain-loader": "^1.0.0",
"sass-loader": "^6.0.7",
"ts-jest": "^27.1.2",
"vue": "^2.6.14",
"vue-jest": "^3.0.7",
"vue-template-compiler": "^2.6.14",
"vueify": "^9.4.1"
}
So the answer is that Jest have an attribute for working with JSDOM and i don't need an additional requires.
i just add in jest.config.js single line in the bottom
module.exports = {
moduleFileExtensions: ["js", "json", "vue"],
watchman: false,
preset: "#nuxt/test-utils",
transform: {
"^.+\\.js$": "babel-jest",
".*\\.(vue)$": "#vue/vue2-jest"
},
testEnvironment: 'jsdom'
};

Quasar unit testing for vue3

I am using quasar for developing a vue app. I am using vue 3.0.7. I have installed jest and created the jest.config File.
The jest config file contains the following:
clearMocks: true,
moduleFileExtensions: [
"js",
"jsx",
"ts",
"vue"
],
testMatch: [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[tj]s?(x)"
],
transform: {
"^.+\\.js$": "babel-jest",
".*\\.(vue)$": "vue-jest"
},
transformIgnorePatterns: [
"\\\\node_modules\\\\",
"\\.pnp\\.[^\\\\]+$"
],
I am writing the following test for a demo vue component:
import { shallowMount} from "#vue/test-utils"
import ComponentVueTest from "./ComponentVueTest"
describe('ComponentVueTest', ()=>{
test("Text shoudl be in the html" , () => {
let wrapper = shallowMount(ComponentVueTest);
expect(wrapper.html).toContain('JustTesting');
})
})
The component that is being tested:
<template>
JustTesting
</template>
<script>
import { defineComponent } from '#vue/composition-api'
export default defineComponent({
setup() {
},
})
</script>
<style>
</style>
and also the package.json dependencies:
"dependencies": {
"#quasar/extras": "^1.0.0",
"#typescript-eslint/eslint-plugin": "^4.29.1",
"#typescript-eslint/parser": "^4.29.1",
"#vue/composition-api": "^1.0.6",
"cordova": "^10.0.0",
"core-js": "^3.6.5",
"quasar": "^2.0.0",
"vue": "^2.6.14",
"vue-loader": "^16.5.0",
"vue-template-compiler": "^2.6.14"
},
"devDependencies": {
"#babel/core": "^7.15.0",
"#babel/eslint-parser": "^7.13.14",
"#babel/preset-env": "^7.15.0",
"#quasar/app": "^3.0.0",
"#quasar/quasar-app-extension-testing": "^1.0.3",
"#quasar/quasar-app-extension-testing-unit-jest": "^2.2.2",
"#vue/test-utils": "^2.0.0-rc.12",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^27.0.6",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-jest": "^24.1.0",
"eslint-plugin-vue": "^7.0.0",
"eslint-webpack-plugin": "^2.4.0",
"jest": "^27.0.6",
"vue-jest": "^3.0.7"
},
When i run the npm run test ("test": "npx jest") i get the following error:
FAIL src/__tests__/Demo.spec.js
● Test suite failed to run
TypeError: Vue.defineComponent is not a function
> 1 | import { shallowMount} from "#vue/test-utils"
| ^
2 | import ComponentVueTest from "./ComponentVueTest"
3 |
4 | describe('ComponentVueTest', ()=>{
at Object.<anonymous> (node_modules/#vue/test-utils/dist/vue-test-utils.cjs.js:7856:26)
at Object.<anonymous> (src/__tests__/Demo.spec.js:1:1)
I have been trying for hours to fix it and can't seem to understand what is wrong
Try changing:
import { defineComponent } from '#vue/composition-api' to
import { defineComponent } from 'vue'
Also looks like you need to change your wrapper.html to wrapper.text() and add toMatch rather than toContain, that said the following should work:
import { shallowMount} from "#vue/test-utils"
import ComponentVueTest from "./ComponentVueTest"
describe('ComponentVueTest', ()=>{
test("Text should be in the html" , () => {
let wrapper = shallowMount(ComponentVueTest);
expect(wrapper.text()).toMatch('JustTesting');
})
})
The solution to unit testing vue3 in quasar v2 is to use #quasar/quasar-app-extension-testing-unit-jest. Just install it with
quasar ext add #quasar/quasar-app-extension-testing-unit-jest
and it should set up everything for you. Notice it is still in alpha and some components are not fully working yet(for me QPage wasn't working).
Hope this helps somebody :)

After Upgrading vue I get a white screen of death, [HMR] Waiting for update signal from WDS

I upgraded vue and now I get a white screen of death, I'll attach the code
The main error I think I get in the browser is [HMR] Waiting for update signal from WDS...
// src/main.js
import Vue from 'vue'
import vuetify from '#/plugins/vuetify' // path to vuetify export
new Vue({
vuetify,
}).$mount('#app')
// src/plugins/vuetify.js
import "#mdi/font/css/materialdesignicons.css" // Ensure you are using css-loader
import Vue from "vue"
import Vuetify from "vuetify/lib"
import {
transitions,
VApp,
VAvatar,
VBtn,
VCard,
VChip,
VFooter,
VForm,
VGrid,
VIcon,
VImg,
VList,
VNavigationDrawer,
VParallax,
VSwitch,
VTextField,
VToolbar
} from "vuetify"
Vue.use(Vuetify)
export default new Vuetify({
icons: {
iconfont: "mdi" // default - only for display purposes
},
components: {
VApp,
VAvatar,
VParallax,
VNavigationDrawer,
VFooter,
VList,
VCard,
VBtn,
VChip,
VIcon,
VGrid,
VToolbar,
VTextField,
transitions,
VForm,
VImg,
VSwitch
},
theme: {
primary: "#5fdaee",
secondary: "#424242",
accent: "#82B1FF",
error: "#FF5252",
info: "#2196F3",
success: "#4CAF50",
warning: "#FFC107"
}
})
"dependencies": {
"#mdi/font": "^3.8.95",
"axios": "^0.19.0",
"register-service-worker": "^1.6.2",
"vue": "^2.6.10",
"vue-analytics": "^5.17.0",
"vue-router": "^3.0.7",
"vuetify": "^2.0.0"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^3.9.2",
"#vue/cli-plugin-e2e-nightwatch": "^3.9.2",
"#vue/cli-plugin-eslint": "^3.9.2",
"#vue/cli-plugin-pwa": "^3.9.0",
"#vue/cli-plugin-unit-jest": "^3.9.0",
"#vue/cli-service": "^3.9.3",
"#vue/eslint-config-standard": "^4.0.0",
"#vue/test-utils": "^1.0.0-beta.25",
"babel-core": "^7.0.0-0",
"babel-jest": "^24.8.0",
"babel-plugin-transform-imports": "^2.0.0",
"http-server": "^0.11.1",
"less": "^3.9.0",
"postcss-custom-properties": "^9.0.2",
"postcss-nested": "^4.1.2",
"postcss-smart-import": "^0.7.6",
"prettier": "^1.18.2",
"sass": "^1.22.7",
"sass-loader": "^7.1.0",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"vue-cli-plugin-vuetify": "^0.6.1",
"vue-template-compiler": "^2.6.10",
"webpack-cli": "^3.3.6"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
when viewing with devtools all I see is