-
-
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
Make test name available in beforeEach() and afterEach() #7774
Comments
Interesting suggestion, thanks! If this is implemented, the argument should probably be an object literal to make it easy to pass in more things to the function without a breaking change. |
Sounds cool. This will be possible once we get rid of |
That won't happen any time soon though - in the meantime you can use |
just wanted to add that this would be super useful when using an XHR vcr like nock back. Right now I have to do this in each test
it would be awesome to just do something like
|
in case this is helpful, mocha keeps a whole suite of helpers in the befores' https://mochajs.org/api/suite#fullTitle describe('test suite', () => {
beforeEach(function () {
console.log(this.currentTest.fullTitle())
// test suite nested names are included too
}
describe('nested names are', () => {
it('included too', () => {
})
})
}) |
My current solution. File: module.exports = {
setupFilesAfterEnv: ['./jest.setup.js'],
.................
}; File // Patch tests
jasmine.getEnv().addReporter({
specStarted: result => jasmine.currentTest = result,
specDone: result => jasmine.currentTest = result,
}); File describe('Test description', () => {
beforeEach(() => console.log('Before test', jasmine['currentTest'].fullName));
afterEach(() => console.log(
'Test',
jasmine['currentTest'].fullName,
'failed',
!!jasmine['currentTest'].failedExpectations.length
));
it('example', () => expect(1).toBe(1));
}); I hope it helps somebody. |
This would be handy simply because I'm using jest to run long integration tests with lots of logging. It's nice to see which test is being run before the setup in the before* hooks start. |
@optimistex 's solution still works today, so that's nice. Here's the types, so you don't have to do the declare namespace jasmine {
const currentTest: {
id: string;
description: string;
fullName: string;
failedExpectations: {
actual: string;
error: Error;
expected: string;
matcherName: string;
message: string;
passed: boolean;
stack: string;
}[];
passedExpectations: unknown[];
pendingReason: string;
testPath: string;
};
} Note I made |
Just a quick note: The last bits of Jasmine remaining in Jest are supposed to be phased out soon, as |
In jest-circus, I noticed that when I console.log the console.log({
type: 'test',
asyncError: "ErrorWithStack: etc...",
duration: null,
errors: [],
fn: [Function (anonymous)],
invocations: 1,
mode: undefined,
name: 'first it',
parent: {
type: 'describeBlock',
children: [ [Circular *1], [Object] ],
hooks: [],
mode: undefined,
name: 'first describe',
parent: {
type: 'describeBlock',
children: [Array],
hooks: [Array],
mode: undefined,
name: 'ROOT_DESCRIBE_BLOCK',
parent: undefined,
tests: []
},
tests: [ [Circular *1] ]
},
startedAt: 1591598218051,
status: null,
timeout: undefined
}) I was wondering if there is an equivalent function to EDIT: In the meantime this is what I did to get the full test name: if (event.name === "test_start") {
let testNames = [];
let currentTest = event.test;
while (currentTest) {
testNames.push(currentTest.name);
currentTest = currentTest.parent;
}
this.global.testName = testNames
.slice(0, testNames.length - 1)
.reverse()
.map((name) => name.replace(/\W/g, "-"))
.join("-");
} |
Bumping this if there is any clear update or expectation on this feature. |
I stumbled across the following which may be of interest to others. beforeEach(() => {
console.log(expect.getState().currentTestName);
})
afterEach(() => {
console.log(expect.getState().currentTestName);
}) |
Very nice, It works on [email protected]. Thanks! |
Despite being undocumented, Anyway, I've added this method as my #1 answer to this StackOverflow question about accessing the test name. |
This kind of logging should be done by jest itself (like junit) and there is no need to make this information in another way available. |
Using |
Thanks @billyvg for your answer. The linked code gave me an idea and wrote an answer on SO. |
Is there a way we can get the |
Based in the @aesyondu snippet and with hours of console.logs I achieved to get the describeName, testName and status This is my CustomNodeEnvironment.js
This my test
An my jest.config.js
An this is the log in which I have access to the detail of each test after its execution I will try to improve the while to get the describe and test names. |
Hello guys! |
Bump, it was so easy with Jasmine... |
Does this work if the test cases (the "test(..)") are running in parallel? Don't the test events get sent out of order? |
this is literally exactly what i came here looking for. this is how i do it in playwright. |
Just wanted to add a use-case to this kind of reflective-data available at runtime:
Specifically we'd love if the filepath itself would be available of the executing spec, but even the full test name (such as that returned by |
🚀 Feature Proposal
Make test name available in
beforeEach()
andafterEach()
.Motivation
When running Selenium tests it's helpful to take a screenshot after every test. Best name for those files would be the test name. Now I use url and timestamp, which are not that good.
For further logging purposes
beforeEach()
could use the test name too, to log when that test starts. It makes easier to search server logs from the same time.Example
The text was updated successfully, but these errors were encountered: