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.

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:

  1. A user runs Stryker on a Jest project using coverage analysis perTest
  2. Stryker will instrument the production code for code coverage. It uses a global coverage object (using istanbul)
  3. 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']
  4. The stryker-jest-runner communicates the global coverage object back to Stryker
  5. 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:open
  • Created 5 years ago
  • Reactions:16
  • Comments:29 (14 by maintainers)

github_iconTop GitHub Comments

10reactions
SimenBcommented, Nov 28, 2018

I still think a cleaner way of reading global variables would be preferred. But I’m willing to close this issue, if you disagree.

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, however

1reaction
dpchampscommented, Jul 14, 2021

Thanks @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)

// where this value is persisted somewhere, testRailsId : ABCD
it.testRails("ABCD", "does something cool", () => { ... });

// reporter.js
class TestRailsReporter extends Reporter {
  async onRunComplete(_, results) {
    // individual results includes Maybe<{testRailsId : "ABCD"}>
  }
}

A custom node environment, or writing to the filesystem feels heavy for something that seems ā€œstraightforwardā€ to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrieve global variable after test run in Jest - Stack Overflow
It seems that runCLI resolves with two arguments, an AggregatedResult type and a GlobalConfig type. AggregatedResult doesn't containĀ ...
Read more >
Globals - Jest
Runs a function after each one of the tests in this file completes. If the function returns a promise or is a generator,...
Read more >
Configuring package.json Ā· Jest
When using the --config option, the JSON file must not contain a "jest" key ... A set of global variables that need to...
Read more >
Jest Configuration And Debugging Jest Based Tests
When set to true, the Jest runner collects the coverage information. ... Let's try using a global variable in the test and see...
Read more >
Jest Globals - w3resource
Jest Globals Ā· Methods. afterAll(fn, timeout) Ā· Reference Ā· afterAll(fn, timeout). This will run a function after all the tests in this file...
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