Retrieve global variable after running jest programatically
See original GitHub issueš Feature Proposal
I would love a way to retrieve global variables from jest. For example:
test.js
global.foo = 'bar';
Running jest using the API:
import { runCLI } from 'jest';
runCLI({
config: config,
runInBand: true,
silent: true
}, [process.cwd()]).then(result => {
result.globals.foo //=> somehow retrieve globals?
});
Motivation
Iām one of the core developers of Stryker, the mutation testing framework for JavaScript and friends. A lot of users are using Stryker with Jest. Functionally, it works fine. However, the performance is not great at the moment.
Using coverage analysis can greatly speedup the mutation testing process. For more information about coverage analysis, see https://stryker-mutator.io/blog/2018-01-10/typescript-coverage-analysis-support. Stryker has support for coverage analysis, as long as the test runner can communicate the coverage report back to the main Stryker process. It can measure both total test coverage as well as code coverage per test. Both coverage reports are different.
Example
We will use global variables like this:
- A user runs Stryker on a Jest project using coverage analysis
perTest
- Stryker will instrument the production code for code coverage. It uses a global coverage object (using istanbul)
- Stryker will run jest via the programmatic interface with the stryker-jest-runner. It will use this configuration options:
runInBand: true
collectCoverage: false
setupFiles: ['path/to/custom/stryker-jasmine/hooks/file']
- The stryker-jest-runner communicates the global coverage object back to Stryker
- Stryker runs mutation testing using the perTest coverage analysis mode. It will filter the exact tests to run using the
setupFiles
option to configure a jasmine spec filter.
Pitch
In the Jest as a Platform talk it is stated that jest is a platform because it allows you to build on top of. I think building a mutation testing framework this way fits that bill perfectly.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:16
- Comments:29 (14 by maintainers)
I think it should stay open - a custom env is just a workaround, we should make it possible to add arbitrary things to the
TestResult
object so you can use it in Jest reporters. I donāt have any suggestions on how to achieve that, howeverThanks @nicojs
@SimenB would the Jest team be open to accepting a pr that introduced this functionality? I see this request from time to time, and would personally like to be able to decorate test results with various meta-datas to report on later.
Perhaps Iām looking for something that already exists: My current use case calls for integrating with various test-reporting frameworks, like testrails and xray et al⦠Iād like to be able to represent a specification with a well-defined property, run all of the tests and then report results to a third party with the associated
id
.Something like (pseudocode incoming)
A custom node environment, or writing to the filesystem feels heavy for something that seems āstraightforwardā to me.