Vue cli — unit test configuration (jest)

MJ
Jun 2, 2021

run the install script.

yarn -D add @vue/cli-plugin-unit-jest @vue/test-utils eslint-plugin-jest identity-obj-proxy

In the root directory, make jest.config.js

module.exports = {
preset: "@vue/cli-plugin-unit-jest",
collectCoverage: false,
testMatch: ["**/__tests__/**/*.test.{j,t}s?(x)"],
modulePathIgnorePatterns: ["<rootDir>/node_modules", "<rootDir>/build", "<rootDir>/dist"],
moduleNameMapper: {
"^~/(.*)$": "<rootDir>/src/$1",
"^@/(.*)$": "<rootDir>/src/$1",
"^.+\\.(css|less|scss)$": "identity-obj-proxy"
},
verbose: true
};
  • you can remove modulePathIgnorePatterns, moduleNameMapper, and modify testMatch(where you put the test files)

In the package.json file. add configurations.

"scripts": {
"test": "vue-cli-service test:unit",
},
"eslintConfig": {
...
"extends" : [
...
"plugin:jest/recommended",
],
...
"overrides": [
{
"files": [
"**/__tests__/**/*.test.{j,t}s?(x)"
],
"env": {
"jest": true
}
}
],
...
}

make some test files, in the testMatch directories.

run the test.

yarn run test

--

--