-
-
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
Need a way to get test status in afterEach() method #5292
Comments
+1 |
As a data point, my company's test suite uses the mocha |
I use Browserstack to run my integration test, and I need to flag the browserstack build as complete or fail. |
Same as the others, we are using Jest with the TestObject/SauceLabs device farm and we need a way to get the current test status to update the test status on the TestObject website. It would be very useful to have something like |
I was able to find a workaround to get the current test status using jasmine custom reports.
|
Is there any progress on that? |
Will this feature be implemented in the near future |
I believe the category of "passing state into tests/hooks" suggestions is currently not a top priority unfortunately. Note that we recommend not using any This seems like something that could be implemented in user land? And made into reusable helpers with something like test('a', saveTestState(() => {/* ... */}));
afterEach(ifTestFailed(() => {/* ... */})); |
Hi @jeysal , thanks for the suggestion! |
I was thinking about something like: let lastTestFailed;
export const saveTestState = async (testFn) => {
try {
await testFn()
lastTestFailed = false;
} catch (err) {
lastTestFailed = true;
throw err
}
}
export const ifTestFailed = async (hookFn) => {
if(lastTestFailed) await hookFn()
} Not tested and not complete e.g. because it doesn't take the possible |
I found a solution using Create a file in the same directory as const JSDOMEnvironment = require('jest-environment-jsdom-fifteen')
class SeleniumEnvironment extends JSDOMEnvironment {
constructor(config, context) {
super(config, context)
this.global.hasTestFailures = false
}
handleTestEvent(event, state) {
if (event.name === 'test_fn_failure') {
this.global.hasTestFailures = true
}
}
}
module.exports = SeleniumEnvironment In Now in your |
I am also looking for a way to do this. @pplante this didn't work for me. I have also never used |
@rgomezp You need to define the |
Hack the global.jasmine.currentEnv_.fail works for me. describe('Name of the group', () => {
beforeAll(() => {
global.__CASE_FAILED__= false
global.jasmine.currentEnv_.fail = new Proxy(global.jasmine.currentEnv_.fail,{
apply(target, that, args) {
global.__CASE__FAILED__ = true
// you also can record the failed info...
target.apply(that, args)
}
}
)
})
afterAll(async () => {
if(global.__CASE_FAILED__) {
console.log("there are some case failed");
// TODO ...
}
})
it("should xxxx", async () => {
// TODO ...
expect(false).toBe(true)
})
}); |
I recently had to get access to the test name during the test, so I followed this comment #7774 (comment). My plan is to use EDIT: FWIW, it works with playwright, afterEach(() => {
console.log(jasmine.currentTest)
console.table(jasmine.currentTest)
})
it("asdf", async () => {
await page.waitForSelector("does-not-exist", {
timeout: 1000
})
// expect(true).toBe(false)
}) |
Leaving our solution here in case it's useful to anybody else. You can store current spec results in Jasmine and access it in
Here's a link to my StackOverflow answer as well: https://stackoverflow.com/a/62557472/293280 |
How can I access jasmine in the .test.js files? I'm getting |
I added the reporter in our Appium's |
as @joshuapinter explained here:
I created a little helper function: get-current-spec-result.tslet currentSpecResult: jasmine.CustomReporterResult;
jasmine.getEnv().addReporter({
specStarted: (result) => currentSpecResult = result,
});
export function getCurrentSpecResult() {
return currentSpecResult;
} so I can import it in any spec and as a side effect, the reporter gets registered (but only if I use it and only once): get-current-spec-result.spec.tsimport { getCurrentSpecResult } from './get-current-spec-result.ts';
describe('get spec result', () => {
it('should fail', () => {
fail('!');
});
afterEach(() => {
const result = getCurrentSpecResult();
const failed = result.failedExpectations?.pop();
expect(failed).toBeTruthy();
});
}); wicked? ;-D |
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. |
Commenting to prevent the bot from closing this as stale. Would really be good to access test status in after each without having to hack Jasmine. |
or without needing jasmine at all |
Stunned this feature doesnt exist! Please provide an ETA |
Also surprised this feature doesn't exist when converting from mocha/chai to jest to make use of its snapshots. In case it helps anyone, it feels dirty but at least it works post jasmine deprecation. Override handleTestEvent to add test status to global in a custom environment class CustomEnvironment extends NodeEnvironment {
handleTestEvent(event: Event) {
if (!this.global.testStatuses) this.global.testStatuses = {};
switch (event.name) {
case 'test_fn_failure':
// event contains info about the test and its parent describe blocks if you need more specific naming.
// I got a full path by looping through parents until I got to 'ROOT_DESCRIBE_BLOCK'
const testName = event.test.name;
this.global.testStatuses[testName] = 'failed'
break;
...
// I also used test_fn_start and test_fn_success
}
}
} Use event test name to match up in after each hook (in setupFilesAfterEnv)
Adding testStatuses to global in global.d.ts declare global {
var testStatuses: { [testPath: string]: 'failed' | 'passed' | 'running' };
} Just note this'll break if test names are not unique in this specific implementation, but could use other identifiers. |
Any update on this one? 😥 |
For me the problem is that |
This would be great functionality |
+1 Need a way to get test status in afterEach() method |
Do you want to request a feature or report a bug?
feature request.
What is the current behavior?
Cannot find a way to get test status in afterEach()
What is the expected behavior?
Need a way to get test status, like mocha can do this with
this.currentTest.state
.Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
Jest 22.0.6
The text was updated successfully, but these errors were encountered: