I have a test 'works with nested children' within the file fix-order-test.js.
Running the below runs all the tests in the file.
jest fix-order-test
How do I run only a single test? The below does not work as it searches for a file of the regex specified.
jest 'works with nested children'
22 Answers
From the command line, use the --testNamePattern or -t flag:
jest -t 'fix-order-test'
This will only run tests that match the test name pattern you provide. It's in the Jest documentation.
Another way is to run tests in watch mode, jest --watch, and then press P to filter the tests by typing the test file name or T to run a single test name.
If you have an it inside of a describe block, you have to run
jest -t '<describeString> <itString>'
Jest documentation recommends the following:
If a test is failing, one of the first things to check should be whether the test is failing when it's the only test that runs. In Jest it's simple to run only one test - just temporarily change that
testcommand to atest.only
test.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});
or
it.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});
Full command to run a single Jest test
Command:
node <path-to-jest> -i <you-test-file> -c <jest-config> -t "<test-block-name>"
<path-to-jest>:- Windows:
node_modules\jest\bin\jest.js - Others:
node_modules/.bin/jest
- Windows:
-i <you-test-file>: path to the file with tests (jsorts)-c <jest-config>: path to a separate Jest config file (JSON), if you keep your Jest configuration inpackage.json, you don't have to specify this parameter (Jest will find it without your help)-t <the-name-of-test-block>: actually it's a name (the first parameter) ofdescribe(...),it(...), ortest(...)block.
Example:
describe("math tests", () => {
it("1 + 1 = 2", () => {
expect(1 + 1).toBe(2);
});
it("-1 * -1 !== -1", () => {
expect(-1 * -1).not.toBe(-1);
});
});
So, the command
node node_modules/jest/bin/jest.js -i test/math-tests.js -c test/tests-config.json -t "1 + 1 = 2"
will test it("1 + 1 = 2", ...), but if you change the -t parameter to "math tests" then it will run both tests from the describe("math tests",...) block.
Remarks:
- For Windows, replace
node_modules/.bin/jestwithnode_modules\jest\bin\jest.js. - This approach allows you to debug the running script. To enable debugging add
'--inspect-brk'parameter to the command.