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.

Jest coverage shows grey 0% for files exporting plain objects.

See original GitHub issue

Do you want to request a feature or report a bug? bug

What is the current behavior? Before running an app, I generate some files that export plain objects (e.g. environment variables).

Here’s one such example.

export default {
  someKey: "someValue"
}

These are tested with Jest snapshots to check they haven’t changed against expectations. In the past, such files would be included in tests and reported as green after running a simple snapshot test.

Now, Jest appears to show 0% for all columns in these files, but the row is not red, but rather grey. I wonder if this is a new behaviour? The tests still run and snapshots get generated.

screen shot 2018-04-16 at 4 45 29 pm

What is the expected behavior? Row should be green with 100% coverage.

Please provide your exact Jest configuration

"jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx}"
    ],
    "coveragePathIgnorePatterns": [
      "storybook.(js|jsx)"
    ],
    "setupFiles": [
      "<rootDir>/config/polyfills.js",
      "<rootDir>/config/jest/setup.js"
    ],
    "testMatch": [
      "<rootDir>/**/__tests__/**/*.js?(x)",
      "<rootDir>/src/**/?(*.)(spec|test).js?(x)"
    ],
    "testEnvironment": "node",
    "testURL": "https://localhost",
    "transform": {
      "^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.css$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx"
    ]
  },

Run npx envinfo --preset jest in your project directory and paste the results here

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i7-2720QM CPU @ 2.20GHz
  Binaries:
    Node: 9.8.0
    Yarn: 1.5.1
    npm: 5.6.0
  npmPackages:
    jest:
      wanted: 22.4.3
      installed: 22.4.3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
SimenBcommented, May 6, 2018

I don’t think this is something Jest has changed, this should be reported to Istanbul.

I think it says 0% since there is no executable code there.

Mind opening up an issue with istanbul and linking back here? Happy to reopen if the issue is in Jest

2reactions
ashleymooglecommented, May 3, 2019

Hello! I know this issue is kinda old but I had the same issue and fixed it like so: old env.js:

export default {
 value: true
}

old test:

import env from './env'
it('should have a value', () => {
    expect(env.value).toBeTruthy()
})

NEW AND WORKING: env.js:

const env = () => {
 return {
  value: true
 }
}
export default env

new test with coverage working:

import env from './env'
it('should have a value', () => {
    expect(env().value).toBeTruthy()
})

Tl;dr SimenB was right, its because the code was not executed. Funny enough that lack of coverage started appearing after I updated jest to the latest version. Cheers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Test Your Apps using Jest, Testing Library, Cypress ...
Code coverage: Jest allows you to get code coverage reports of your tests. These reports show what percentage of your code is currently ......
Read more >
Configuring Jest
The configuration file should simply export an object: ... The configuration also can be stored in a JSON file as a plain object:...
Read more >
Files containing only exports decreases the test coverage Jest
I'm using create-react-app ...
Read more >
Continuous integration for React applications using Jest and ...
In this tutorial, I will lead you through setting up a sample React and Redux application and writing tests for it. I will...
Read more >
module not found error can't resolve 'fs' in dotenv react
Finally in my index file and my components I don't need to import dotenv, ... "jest": "jest --coverage --logHeapUsage --maxWorkers=4 --config jest.config.js ...
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