I would like to get the result
(the returned value) of a test in another test (the next test) with Jest. Is there a way to do this?
I tried to return a value, but I don't now how to catch it and affect it to a const or a var.
test('a', () => {
expect(1).toBe(1)
return 'ok'
})
test('b', () => {
// I want to use the value returned by the first test: "ok"
})
I know i can use "global" variable, but I feel like it's a bit hacky.
Is there a way to get the returned value of a test callback in order to use it in another test?
For a single execution, you could have a top level object that stores execution information, which you parse in an afterAll
method.
Here is a dummy test suite which highlights what I mean. Of course you can get creative and be more organized, and have objects even at a higher level.
You could then store them in a file, send the results to a server, etc.
test.js
describe('A suite', () => {
let suiteSpecificData = {};
test('a test', () => {
expect(1).toBe(1)
suiteSpecificData["a test"] = "ok"
})
test('another test', () => {
let theOtherTestData = suiteSpecificData["a test"];
let thisTestData = suiteSpecificData["another test"] = {};
if (theOtherTestData === "ok") {
thisTestData.messageOne = "All good with the other test";
thisTestData.someMoreRandomStuff = [1,2,3];
}
})
afterAll(() => {
console.log(JSON.stringify(suiteSpecificData));
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.