Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current test/spec name in Jest

Tags:

jestjs

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.

like image 771
Giacomo Tagliabue Avatar asked Oct 13 '18 00:10

Giacomo Tagliabue


People also ask

How do I know which tests jest will run?

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.

What are the configuration options for jest?

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):

How to generate HTML based test reports for jest tests?

#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.

How do I send jest test results to stderr?

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.


1 Answers

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:

Easiest

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.

With some setup, and soon to be obsoleted

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.

jest.config.js:

module.exports = {
    setupFilesAfterEnv: ['./jest.setup.js'],
    
    .................
};

jest.setup.js:

// Patch tests to include their own name
jasmine.getEnv().addReporter({
    specStarted: result => jasmine.currentTest = result,
    specDone: result => jasmine.currentTest = result,
});

Then, in your *.test.js files...

describe('Test description', () => {
    beforeEach(() => console.log('Before test', jasmine['currentTest'].fullName));

    test(...);
});
like image 139
Dan Dascalescu Avatar answered Sep 21 '22 01:09

Dan Dascalescu