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 collect coverage from /dist/**/*.js instead of /src/**/*.ts

See original GitHub issue

Describe the bug Right now in the tests, we’re doing a mix-match of import {..} ../src/... and import {..} ../dist/.. The issue with the latter is that jest coverage is set to collect from src/**/.ts. Except, there’s a very subtle and nasty bug when tests import from src folder. The state of objects that are instantiated upon module import. I ran into this hair-pulling bug in core-blockchain/src/plugin.ts and also in core-p2p/src/plugin.ts

line 4: import { config } from "./config"; line 19: config.init(options);

./config instantiates a config object when that module is loaded. Now here’s the kicker. Doing import {config} from "/src/config" counts as a different module, when compared to import {config} from "/dist/config"

If ANOTHER class imports the ./config module, the ./config shares its path too. So if you import a class with from /src/ then the ./config module will be loaded from /src/config. If you import from /dist, the ./config is loaded from /dist/config

Why is this an issue? The tests were importing & loading the tested class from src, but the jest-runner was booting up and loading classes from dist . Because those two are considered different paths/modules, they will each new up a config object resulting in two instances of the Config class. However, when later in line 19 shown above, only the config instance created by code running from /dist will have the .init method called on it. The src config instance will not. This creates a very peculiar issue where the tests will fail because, the config instance the tests refer to, hasn’t been initialized and the tests were expecting that behavior.

So we change the tests to import from dist, but now we get no coverage because jest is looking at src source files.

Since we can’t force jest-runner to boot from src, tests should be referencing dist imports(which are supposed to be the compiled sources anyway so it’s appropriate for tests to refer to them), we should change where jest is looking for coverage to dist/**/*.js. Solves both that bug, and we still get the coverage numbers, while losing readability since coverage will be based on the transpiled files, which aren’t always human-readable.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
air1onecommented, Jan 16, 2019

Thanks guys for your answers. After some more investigation I think I have a solution that can make everyone happy :

Basically when we are in a package, say core-api, we just register manually the core-api plugin. This gets us typescript coverage.

Why does this work ? Because when we register manually our package, we will register the code that is transpiled by ts-jest and as this is the typescript pre-processor we use for tests it works fine.

Right now we let core-container register automatically all the packages, so basically when we’re in core-api, it registers the core-api code that’s in dist and that has nothing to do with the code we’re actually transpiling at the moment (again, this one is transpiled by ts-jest). Hence the coverage issue.

I tested on core-api package and got coverage with my solution. I will prepare a pull request and update the other packages, so you can see what I’m talking about 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Jest
Indicates whether the coverage information should be collected while executing the test. Because this retrofits all executed files with coverage ...
Read more >
Configuring code coverage in Jest, the right way
In this brief tutorial we see how to configure code coverage in Jest, the popular testing framework for JavaScript. If you're new to...
Read more >
How can I ignore a file pattern for Jest code coverage?
I've tried regex and file patterns but none of these just ignores the *.entity.ts files in the final report. When I add for...
Read more >
Configuring package.json · Jest
{js,jsx}", "!**/node_modules/**", "!**/vendor/**"]. This will collect coverage information for all the files inside the project's rootDir , except the ones ...
Read more >
Configuring Jest compiled - w3resource
jest.config.js ``module.exports = { verbose: true, }; ... This collects coverage information for all the files inside the project's rootDir, ...
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