-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieve global variable after running jest programatically #7421
Comments
Um, can't you use this? https://jestjs.io/docs/en/configuration#globals-object |
No, that only works to pass global variables to the testing environment. No way to get changed variables back out. |
@nicojs would globalSetup and globalTeardown work? I.e. can you send the coverage results back to stryker in globalTeardown (which is like a global afterEach for each file that is tested) |
Another option would be a custom reporter (which gets the results of each test file and all results after all tests run). This is how our built-in coverage works, here's our coverage reporter |
I don't think tests should "return results". However, I'm not sure what the best API for this would be. Adding it to A workaround you can maybe use is add an
No, they run outside the sandbox and do not have access to its
That works since we put the coverage data into |
Thanks for all the responses! It is appreciated 👍
Something like that would work for us. We would add it it in a custom jasmine reporter: jasmine.getEnv().addReporter({
jasmineDone(result) {
jest.report({strykerCoverage: {bla: 42}})
}
});
That isn't the case for us right? As we use
I understand, but the problem would be that we would have to keep maintaining 'shadow' test environments for all supported test environments. Code would be identical except for the teardown part. Feels like a lot of maintenance work and error-prone for end users.
Wow. Didn't event think of that. It feels dirty, but it works. If I write the coverage data to disk, I can pick it up later. Just to be clear: this is the only way to share global data in Jest today? I still would like a better way of doing it, but we can proceed to implement this. The if (global.jasmine) {
jasmine.getEnv().addReporter({
jasmineDone() {
require('fs').writeFileSync('coveragedata', global.strykerCoverage, 'utf8');
}
});
} Unfortunately, we need to use |
@nicojs does a Jest custom reporter work for you then? |
It is also possible that you can write a custom test environment in order to have more control over the global environment. Example: https://github.com/facebook/jest/blob/master/packages/jest-environment-jsdom/src/index.js#L45 |
Not really, the
I understand. Can we think of a way to still use the test environment that the user configured (jsdom, or other), but also load our custom environment? As specified before, I'm not interested in maintaining a "shadow" environment for every jest environment out there. |
Is |
This is the way code coverage in JavaScript generally works, right? By passing coverage data along the global scope? We could also use a shared node module to store the data statically: const coverageStore = require('./coverageStore');
coverageStore.foo = 'bar'; But it suffers from the same issue. Jest will make sure the test environment doesn't interfere with the runtime environment. I.e. Maybe there is another way though. If we use the variable name |
We need to find a solution that does not rely on Jasmine APIs, as they will be deprecated and removed at some point.
In your case yes, but we're not adding a feature that only works when running in band.
const NodeEnvironment = require('jest-environment-node');
class StrykerEnvNode extends NodeEnvironment {
teardown() {
doStrukerStuff();
return super.teardown();
}
}
module.exports = StrykerEnvNode;
It uses a heavily modified fork. We will be making
Yeah, don't mess with that 😉 |
@nicojs it's definitely possible for you to inherit from an existing test environment so you reduce the duplication. They are just ES6 classes. |
@SimenB thanks for the code sample. It seems to work fine. We can read the globals from Another benefit of using jest's environment teardown is that it can run asynchronously. So we can use 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 |
I was looking for a way to do just that and I'd love to see a simple api for it. |
@SimenB Is there a way we could discuss what would be a good API to achieve this. I might be in a position where I can get the time to work on contribution on this, if we can agree on a solution. |
Hey guys, any news about this issue ? |
I'm also interested in this API. Any news about this? |
It would be great to have this feature so that we could expose any arbitrary data to the custom test reporter and output the same using jest-junit via system-out xml element |
@narayanpai I added a feature to jest-junit recently which may fit your use-case. It allows you to add custom test suite properties (which is a supported part of the junit spec). https://github.com/jest-community/jest-junit#adding-custom-testsuite-properties |
@palmerj3 Doesn’t the feature only let you process the result data in which only way of including any custom data from the test environment is |
You get the same data reporters themselves get. So if your complaint is about passing custom data to reporters then you're in the right place. |
I don't understand why adding custom data to test result is such a hard thing to agree on. Just allow something like |
What's the status on this issue? |
I'm pretty sure the core team didn't make progress on this yet. I ended up implementing this feature in StrykerJS using the suggestion by @SimenB . You can use https://github.com/stryker-mutator/stryker-js/tree/master/packages/jest-runner as inspiration |
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 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. |
Any news on that? What would be needed for this change to get in? |
I'm interested in this too. any updates? |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 Feature Proposal
I would love a way to retrieve global variables from jest. For example:
test.js
Running jest using the API:
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:
perTest
runInBand: true
collectCoverage: false
setupFiles: ['path/to/custom/stryker-jasmine/hooks/file']
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.
The text was updated successfully, but these errors were encountered: