I have test:
import { shallowMount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import component from './component.vue';
const localVue = createLocalVue();
localVue.use(Vuex);
jest.mock('infrastructure/store/global-instance', () => ({
injectToReadonlyField: jest.fn()
}));
jest.mock('components/store/entity', () => ({
ThemeClassName: jest.fn()
}));
describe('s-button', () => {
let getters: any;
let store: any;
beforeEach(() => {
getters = {
theme: () => 'material'
};
store = new Vuex.Store({
modules: {
style: {
namespaced: true,
getters
}
}
});
});
it('Component does not empty and slot work fine', () => {
const wrapper = shallowMount(component, {
store,
localVue,
slots: {
default: 'Test button'
}
});
expect(wrapper.html()).not.toEqual("");
expect(wrapper.text()).not.toEqual("");
expect(wrapper.text()).toEqual('Test button');
});
it('Style classes work fine', () => {
const wrapper = shallowMount(component, { store, localVue });
expect(wrapper.find('.s-button').exists()).toBe(true);
expect(wrapper.find('.text').exists()).toBe(true);
});
it('When we click on button it generate event', () => {
let wasGeneratedEvents = false;
const wrapper = shallowMount(component, {
store,
localVue,
listeners: {
click: () => wasGeneratedEvents = true
}
});
wrapper.find('.button').trigger('click');
expect(wasGeneratedEvents).toBe(true);
});
});
My .babelrc looks like this:
{
"plugins": [ "babel-plugin-syntax-dynamic-import" ],
"presets": [
"babel-preset-env"
]
}
jest.config.js:
const path = require('path');
const getPathAliases = require('./get-path-aliases');
const moduleNameMapper = {};
const pathAliases = getPathAliases();
for (const pathAliasName in pathAliases) {
moduleNameMapper[`^${pathAliasName}/(.*)`] = `${path.resolve(__dirname, pathAliases[pathAliasName])}/$1`;
}
module.exports = {
moduleFileExtensions: [
"js",
"jsx",
"ts",
"tsx",
"json",
"vue"
],
moduleNameMapper: moduleNameMapper,
moduleDirectories: [
"node_modules",
"sources"
],
transform: {
"^.+\.js$": "babel-jest",
"^.+\.vue$": "vue-jest",
"^.+\.tsx?$": "ts-jest"
},
testURL: "http://localhost/",
testRegex: "\.test\.(jsx?|tsx?)$",
testEnvironment: "node"
};
Here are the dependencies that I have in my testing package.json:
{
"dependencies": {
"vue": "2.6.12",
"vuex": "3.6.2"
}
"devDependencies": {
"@types/node": "14.0.0",
"@vue/test-utils": "1.1.3",
"babel-jest": "23.6.0",
"ts-jest": "26.5.6"
}
}
After running the test, I get the following error:
● Test suite failed to run
TypeError: Cannot read property 'installed' of undefined
4 |
5 | const localVue = createLocalVue();
> 6 | localVue.use(Vuex);
| ^
7 |
8 | jest.mock('infrastructure/store/global-instance', () => ({
9 | injectToReadonlyField: jest.fn()
Please tell me a solution or ideas to solve this problem
I tried changing dependency versions and rewriting the test, but it didn’t help
New contributor
Артем Малыйкин is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.