question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue SFCs not included in coverage reports

See original GitHub issue

Problem

I’m using vue-test-utils with Jest to run unit tests for single-file Vue components. I’m relying on vue-jest to transform the component files so that I don’t have to rely on Webpack, etc.

I’m able to require Vue components in tests and run them without any problems, but for some reason I’m not able to get Jest to include these files in the coverage reports that it generates.

I’m using Jest at 25.1.0, vue-jest at 3.0.5, and vue at 2.6.11.

One other thing I should note: for this project, my Vue components are all written in ES5 (plus module.exports and require statements; I’m relying on a non-JS module tool to deliver this code in browser). I’m not running any webpack or babel transformations on JS code either at runtime or in testing, just the vue-jest transform for SFCs. Everything works fine until it comes to generating the coverage report.

Source code is visible here: https://github.com/egardner/mediawiki-extensions-VueTest

Configuration

All JS/Vue files live in a resources folder which is structured like this:

resources/
├── components
│   ├── ApiRequestModule.vue
│   ├── App.vue
│   ├── ChildComponent.vue
│   ├── ComputedPropertyModule.vue
│   ├── ParentChildCommunicationModule.vue
│   ├── TwoWayBindingModule.vue
│   └── index.js
├── init.js
└── plugins
    ├── api.js
    ├── i18n.js
    └── index.js

Here are the relevant parts of the Jest configuration being used:

module.exports = {
  clearMocks: true,
  collectCoverage: true,
  collectCoverageFrom: [
    "resources/**/*.(js|vue)"
  ],
  coverageDirectory: "coverage",
  coveragePathIgnorePatterns: [,
    "/node_modules/",
    "resources/components/index.js",
    "resources/plugins/index.js",
    "resources/init.js"
  ],
  globals: {
    "vue-jest": {
      babelConfig: false,
      hideStyleWarn: true,
      experimentalCSSCompile: true
    }
  },
  moduleFileExtensions: [
    "js",
    "json",
    "vue"
  ],
  setupFiles: [
    "./jest.setup.js"
  ],
  transform: {
    ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
  },
};

Output

When I run the tests using Jest, I get output like this:

 PASS  tests/jest/ChildComponent.test.js
 PASS  tests/jest/TwoWayBindingModule.test.js
 PASS  tests/jest/ComputedPropertyModule.test.js
 PASS  tests/jest/App.test.js
 PASS  tests/jest/ApiRequestModule.test.js
 PASS  tests/jest/ParentChildCommunictionModule.test.js
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |   92.86 |    66.67 |     100 |   92.86 |
 api.js   |     100 |      100 |     100 |     100 |
 i18n.js  |   88.89 |       50 |     100 |   88.89 | 52
----------|---------|----------|---------|---------|-------------------

Test Suites: 6 passed, 6 total
Tests:       13 passed, 13 total
Snapshots:   0 total
Time:        3.981s
Ran all test suites.

The tests (which require various Vue files) run and succeed, but the coverage report only includes the plain JS files.

Do Vue files need to be transformed with Webpack or Babel so that they can be assessed for coverage in Jest? I had assumed that the vue-jest transformations would allow for this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:13

github_iconTop GitHub Comments

7reactions
ThePeachcommented, Feb 26, 2020

And then running npx vue-cli-service test:unit

so that version of vue-cli-service seems to be using jest 24.9.0

I have exactly the same problem this post is talking about and I was running 25.1.0

Downgraded to 24.9.0 and coverage started working again. So major update of jest is breaking something with the vue-jest reporting.

7reactions
egardnercommented, Feb 13, 2020

@TerminalSpirit this is useful, thank you for sharing. It seems like vue-cli-service knows some tricks that we don’t! I don’t understand what the relevant config differences are, but I was able to get coverage reports by doing the following:

Adding these packages to package.json (I didn’t need the babel plugin)

"@vue/cli-plugin-unit-jest": "4.2.2",
"@vue/cli-service": "4.2.2",

And then running npx vue-cli-service test:unit

That gives me the following report:

-------------------------------------|----------|----------|----------|----------|-------------------|
File                                 |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-------------------------------------|----------|----------|----------|----------|-------------------|
All files                            |    96.97 |    66.67 |      100 |    96.97 |                   |
 components                          |      100 |      100 |      100 |      100 |                   |
  ApiRequestModule.vue               |      100 |      100 |      100 |      100 |                   |
  App.vue                            |      100 |      100 |      100 |      100 |                   |
  ChildComponent.vue                 |      100 |      100 |      100 |      100 |                   |
  ComputedPropertyModule.vue         |      100 |      100 |      100 |      100 |                   |
  ParentChildCommunicationModule.vue |      100 |      100 |      100 |      100 |                   |
  TwoWayBindingModule.vue            |      100 |      100 |      100 |      100 |                   |
 plugins                             |    92.86 |    66.67 |      100 |    92.86 |                   |
  api.js                             |      100 |      100 |      100 |      100 |                   |
  i18n.js                            |    88.89 |       50 |      100 |    88.89 |                52 |
-------------------------------------|----------|----------|----------|----------|-------------------|

This is a decent solution for now but I’d still love to understand what options are actually being provided to Jest here that suddenly allows everything to work properly…

Read more comments on GitHub >

github_iconTop Results From Across the Web

No coverage for .vue files when using Jest - Get Help
Coverage is reported for plain .js files in the same folders without any trouble. ... Issue: Vue SFCs not included in coverage reports....
Read more >
Vue files not included in coverage reports
when I use jest --coverage to run jest, I found some vue files not included in coverage reports, but some vue files is...
Read more >
Chapter 1. Introduction to testing Vue applications
This book will teach you to test Vue applications effectively to make sure you get the benefits of testing and avoid the pitfalls....
Read more >
How you can test your Vue.js apps in less than seven ...
We can also include code coverage in the test results using the istanbul plugin to get a report like this: It is used...
Read more >
Jest+Vue: no coverage shown for .vue files that don't ...
Since the absence of List.vue (when it doesn't contain any executable code) in the coverage report is caused by behavior of Jest/Istanbul, please...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found