Is there a way to get the current spec name from within the running test?
Basically I want to save a file, eg. using a function saveFile()
, with the name of the file being the spec name. Without having to manually retype the name of the test.
Lists all tests as JSON that Jest will run given the arguments, and exits. This can be used together with --findRelatedTests to know which tests Jest will run. Logs the heap usage after every test.
Every one of Jest's Configuration options can also be specified through the CLI. Here is a brief overview: Run all tests (default): Run only the tests that were specified with a pattern or filename: Run tests related to changed files based on hg/git (uncommitted files):
#1) The command line reports are good but not very readable. There are libraries/modules available to generate HTML based test reports for Jest tests. It can be achieved as shown below. Add node package for jest-html-reporter using the below command. Now add Jest configuration for the reporter in the package.json file of the node project.
Insert Jest's globals ( expect, test, describe, beforeEach etc.) into the global environment. If you set this to false, you should import from @jest/globals, e.g. Note: This option is only supported using the default jest-circus test runner. Prints the test results in JSON. This mode will send all other test output and user messages to stderr.
There's been a 2+-year old issue in the Jest repo to make the test name available, with no official solution as of Feb 2021. One core maintainer said none would be available any time soon.
The community has provided workarounds, however:
expect.getState().currentTestName
This will return the full path to the test, from the outermost describe to the test itself, separated with ' '
(not the best separator), e.g. static methods toString
. You can't easily tell which one was the describe
name, and which one was the test
name.
The same GitHub issue has this alternative method, which makes the test name accessible directly in the test via jasmine['currentTest'].fullName
, no extend
needed. Note though that Jasmine will no longer be the default test reported starting in Jest 27.
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js'],
.................
};
// Patch tests to include their own name
jasmine.getEnv().addReporter({
specStarted: result => jasmine.currentTest = result,
specDone: result => jasmine.currentTest = result,
});
describe('Test description', () => {
beforeEach(() => console.log('Before test', jasmine['currentTest'].fullName));
test(...);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With