I am having difficulty trying to set up Vue CLI 3 with Jest to show test coverage. I have done everything possible to make it work, but it is still showing no coverage:
Ran all test suites.
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 0 | 0 | 0 | |
----------|----------|----------|----------|----------|-------------------|
=============================== Coverage summary ===============================
Statements : Unknown% ( 0/0 )
Branches : Unknown% ( 0/0 )
Functions : Unknown% ( 0/0 )
Lines : Unknown% ( 0/0 )
Below is an excerpt of my configuration:
jest.config.js:
module.exports = {
moduleFileExtensions: [
'js',
'jsx',
'json',
'vue'
],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.jsx?$': 'babel-jest'
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
snapshotSerializers: [
'jest-serializer-vue'
],
testMatch: [
'**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
],
transformIgnorePatterns: ['<rootDir>/node_modules/'],
testURL: 'http://localhost/'
}
package.json:
....
"scripts": {
"test:unit": "nyc vue-cli-service test:unit"
},
"nyc": {
"check-coverage": true,
"per-file": true,
"lines": 90,
"statements": 90,
"functions": 90,
"branches": 90,
"include": [
"src/**/*.{js,vue}"
],
"exclude": [
"src/*.js"
],
"reporter": [
"lcov",
"text",
"text-summary"
],
"extension": [
".js",
".vue"
],
"verbose": true,
"cache": true,
"all": true
}
How do I properly configure Vue CLI 3 and Jest to show test coverage?
Jest is collecting coverage only on the function under tests, not from the entire project. This means that despite we are seeing 100% coverage here, potentially we are testing only a fraction of our code. Now Jest is identify correctly what needs to be tested.
If you need global configuration for Jest, you can use the optional jest. config. js file which is also automatically created by the Vue CLI 3 scaffold. Looking in this config you can see an option roots which specifies the client unit test directory.
From the root of the project, you'll find it in coverage/lcov-report/index. html . Though this is a good start, there's often configuration required because certain files will be included and others will be excluded that are not desired to be. It's worth noting: Jest automatically exclude any .
If you are using the Vue CLI to build your project, you can use the plugin cli-plugin-unit-jest to run Jest tests. The plugin pulls all required dependencies (including jest), creates a jest. config. js file with sensible defaults, and generates a sample test suite.
Jest has its own coverage facilities, so remove nyc
from package.json:
"scripts": {
// "test:unit": "nyc vue-cli-service test:unit" // DELETE
"test:unit": "vue-cli-service test:unit"
},
// "nyc": {...} // DELETE
To enable Jest's coverage, set collectCoverage
and collectCoverageFrom
in jest.config.js (per the vue-test-utils
docs):
collectCoverage: true,
collectCoverageFrom: [
'src/**/*.{js,vue}',
'!src/main.js', // No need to cover bootstrap file
],
Running yarn test:unit
should yield console output like this:
GitHub demo
Also note that the Jest console output only lists files that contain executable JavaScript (methods
for Vue SFCs). If you're working off the default Vue CLI generated template, HelloWorld.vue
contains no methods
, so it won't be listed. In the screenshot above, I've added an unused method to HelloWorld.vue
to demonstrate Jest's uncovered lines report.
While @tony19's answer is perfectly valid, you don't necessarily need to add anything in your custom jest configuration. For a project built with the Vue CLI service, just adding the following script in the package.json
worked fine, and the coverage is showing up for Vue components:
"test:coverage": "vue-cli-service test:unit --coverage",
There are additional options you can add, such as changing the reporter(s), and having a distinct Jest configuration just for this script. To get the full list of options, you can run the following command in your terminal:
npx vue-cli-service test:unit help
And, among these options, you'll find collectCoverage
and collectCoverageFrom
which can help you keep everything in the script, rather than having a custom config file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With