Reputation: 21846
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'
Upvotes: 909
Views: 908566
Reputation: 384
Yarn or npm
yarn test src/test/<path to the testfile>.ts
npm test src/test/<path to the testfile>.ts
Upvotes: 3
Reputation: 434
Just adding my five cents here. For VSCode, the vscode-jest-runner (also a github link) extension works just great for selectively running and/or debugging specific tests and needs literally no configuration (for ex., you don't have to modify launch.json).
Upvotes: 1
Reputation: 7404
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
test
command 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);
});
Upvotes: 353
Reputation: 93
The standalone -- is *nix magic for marking the end of options, meaning (for NPM) that everything after that is passed to the command being run.
If you have 2 test file with these names: Sample-test1.js, Sample-test2.js and you just wish to run 1 of them, you should run this command.
npm test -- Sample-test1.js
which this part is static => npm test --
and the second part is your test name file, like this => Sample-test1.js
Upvotes: 7
Reputation: 2348
The quickest/simplest way that I've found is with
npx jest TheNameOfYourFile
also, you can "subscribe" to keep reading changes with
npx jest TheNameOfYourFile --watchAll
Upvotes: 3
Reputation: 7053
If anyone is trying to use jest -t '<testName>'
and wondering why it isn't working, it's worth noting that -t
argument is actually a regex pattern, not a string literal.
If your test name has no special characters in it then it will just work as expected (using either the string from it
or describe
or combinations of these).
If your test name does have special characters such as brackets, just escape them with a backslash. e.g. a test like:
it("/ (GET)", () => {
return request(app.getHttpServer())
.get("/health")
.expect(200)
.expect("Hello World");
});
Could be targeted with jest -t "\/ \(GET\)"
.
The regex doesn't need to match the entire string either, so you can match against common parts if you want to run a subset based on a consistent naming convention.
Upvotes: 6
Reputation: 7475
Command:
node <path-to-jest> -i <your-test-file> -c <jest-config> -t "<test-block-name>"
<path-to-jest>
:
node_modules\jest\bin\jest.js
node_modules/.bin/jest
-i <you-test-file>
: path to the file with tests (js
or ts
)-c <jest-config>
: path to a separate Jest config file (JSON), if you keep your Jest configuration in package.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) of describe(...)
, it(...)
, or test(...)
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:
node_modules/.bin/jest
with node_modules\jest\bin\jest.js
.'--inspect-brk'
parameter to the command.Having Jest installed, you can simplify the syntax of this command (above) by using NPM scripts. In "package.json"
add a new script to the "scripts"
section:
"scripts": {
"test:math": "jest -i test/my-tests.js -t \"math tests\"",
}
In this case, we use an alias 'jest'
instead of writing the full path to it. Also, we don't specify the configuration file path since we can place it in "package.json"
as well and Jest will look into it by default. Now you can run the command:
npm run test:math
And the "math tests"
block with two tests will be executed. Or, of course, you can specify one particular test by its name.
Another option would be to pull the <the-name-of-test-block>
parameter outside the "test:math"
script and pass it from the NPM command:
package.json:
"scripts": {
"test:math": "jest -i test/my-tests.js -t",
}
Command:
npm run test:math "math tests"
Now you can manage the name of the run test(s) with a much shorter command.
Remarks:
'jest'
command will work with NPM scripts becausenpm makes
"./node_modules/.bin"
the first entry in thePATH
environment variable when running any lifecycle scripts, so this will work fine, even if your program is not globally installed (NPM blog) 2. This approach doesn't seem to allow debugging because Jest is run via its binary/CLI, not vianode
.
If you are using Visual Studio Code you can take advantage of it and run the currently selected test (in the code editor) by pressing the F5 button. To do this, we will need to create a new launch configuration block in the ".vscode/launch.json"
file. In that configuration, we will use predefined variables which are substituted with the appropriate (unfortunately not always) values when running. Of all available we are only interested in these:
${relativeFile}
- the current opened file relative to
${workspaceFolder}
${selectedText}
- the current selected text in the active fileBut before writing out the launch configuration we should add the 'test'
script in our 'package.json'
(if we haven't done it yet).
File package.json:
"scripts": {
"test": "jest"
}
Then we can use it in our launch configuration.
Launch configuration:
{
"type": "node",
"request": "launch",
"name": "Run selected Jest test",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"test"
],
"args": [
"--",
"-i",
"${relativeFile}",
"-t",
"${selectedText}"
],
"console": "integratedTerminal",
}
It actually does the same as the commands described earlier in this answer. Now that everything is ready, we can run any test we want without having to rewrite the command parameters manually.
Here's all you need to do:
Select currently created launch config in the debug panel:
Open the file with tests in the code editor and select the name of the test you want to test (without quotation marks):
Press F5 button.
And voilà!
Now to run any test you want. Just open it in the editor, select its name, and press F5.
Unfortunately, it won't be "voilà" on a Windows machines because they substitute (who knows why) the ${relativeFile}
variable with the path having reversed slashes and Jest wouldn't understand such a path.
(In case if the command needs troubleshooting, see similar approach in https://www.basefactor.com/using-visual-studio-code-to-debug-jest-based-unit-tests)
Remarks:
'--inspect-brk'
parameter.'package.json'
.Upvotes: 227
Reputation: 598
After checking the Jest CLI doc, I found this is how we run a specific test in a specific file.
jest --findRelatedTests path/to/fileA.js path/to/fileB.js -t "test name"
With yarn,
yarn test --findRelatedTests path/to/fileA.js path/to/fileB.js -t "test name"
With npm,
npm test -- --findRelatedTests path/to/fileA.js path/to/fileB.js -t "test name"
For reference please check Jest Cli Options
Upvotes: 1
Reputation: 349
run this command line :
npm run test-jest unit_test/file_name -- -t test_name
My Package.json
"test-jest": "jest --verbose=true --force-exit",
"test-jest:watch": "jest --watchAll",
Upvotes: 0
Reputation: 430
In the latest version of jest, you can run any single test in multiple ways.
fit('only this test will run', () => {});
it.only('only this test will run',() => {});
Upvotes: 7
Reputation: 1155
https://jestjs.io/docs/cli#--testnamepatternregex
Where your test is something like this file called my.test.js
test("My Sum", () => {
const sum = 3;
expect(sum).toBe(3);
});
Run on CLI with the test name
jest -t Sum
Use npm test with regex match part of file name example: my.test.js
npm test -t my
Upvotes: 7
Reputation: 5020
You can try using the following command cause it's working for me
npm run test -- -t 'Your test name'
Or the other way you can do is just add .only
with your test like the following and run the command npm run test
it.only('Your test name', () => {})
Upvotes: 24
Reputation: 429
On jest 26.6.0 this is the only thing that worked for me:
jest -- test/unit/name-of-test-file.test.ts
and to watch
jest --watch -- test/unit/name-of-test-file.test.ts
Upvotes: 11
Reputation: 28435
For VSCode You can use jest-run-it extension that will help you run and debug Jest tests from your editor.
Upvotes: 9
Reputation: 753
For VSCode in Windows, I use these in my launch.json file. Note the use of ${pathSeparator} to handle the difference in Win and Mac. Select one in the debug dropdown and press F5 to run.
{
"name": "Debug Selected Jest Test",
"type": "node",
"request": "launch",
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand"],
"args": ["--", "-i", "${fileDirnameBasename}${pathSeparator}${fileBasename} ", "-t", "${selectedText}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
},
{
"name": "Debug Named Jest Test",
"type": "node",
"request": "launch",
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand"],
"args": ["--", "-i", "${fileDirnameBasename}${pathSeparator}${fileBasename} ", "-t", "filename.test.js"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
},
Upvotes: 0
Reputation: 110922
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>'
Upvotes: 1036
Reputation: 935
I took me a while to find this so I'd like to add it here for people like me who use yarn:
yarn test -i "src/components/folderX/folderY/.../Filename.ts" -t "name of test"
So filename after -i and testname after -t.
Upvotes: 4
Reputation: 897
npm test __tests__/filename.test.ts
- to run a single file.
test.only('check single test', () => { expect(true).toBe(true)});
- to run a single test case
test.skip('to skip testcase, () => {expect(false).toBe(false_});
- to skip a test case
Upvotes: 5
Reputation: 1670
Use:
npm run test -- test-name
This will only work if your test specification name is unique.
The code above would reference a file with this name: test-name.component.spec.ts
Upvotes: 15
Reputation: 41
Just a little add-on, because it seems there was kind of a fight if to use ./node_modules/.bin/jest -i ...
or just jest -i ...
or npm test -- -i ...
jest
works if you have it installed globally (as with npm install -g jest
), a not-so-clean way of handling dependenciesnpx jest -i ...
=> this is exactly what npx is for. It saves you from writing ./node_modules/.bin/...
.Upvotes: 4
Reputation: 1274
There is now a nice Jest plugin for this called jest-watch-typeahead
it makes this process much simpler.
Upvotes: 2
Reputation: 4870
In Visual Studio Code, this lets me run/debug only one Jest test, with breakpoints: Debugging tests in Visual Studio Code
My launch.json
file has this inside:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
]
}
And this in file package.json
:
"scripts": {
"test": "jest"
}
test
(or it
) to test.only
(or it.only
). To run one test suite (several tests), change describe
to describe.only
.Upvotes: 10
Reputation: 2724
With the latest Jest version, you can use one of the following to only run one test, and the same for a test suite.
it.only('test 1', () => {})
test.only('test 1', () => {})
fit('test 1', () => {})
jest 'test 1'
may work too if the test name is unique.
Upvotes: 12
Reputation: 10396
./node_modules/.bin/jest --config test/jest-unit-config.json --runInBand src/components/OpenForm/OpenForm.spec.js -t 'show expanded'
./node_modules/.bin/...
is a wonderful way, to access the locally installed Jest (or Mocha or...) binary that came with the locally installed package. (Yes, in your npm scripts you can jest
with nothing before, but this is handy on command line... (that's also a good start for your debugging config, whichever IDE you are using...)package.json
), this is, what you need.--runInBand
– as said, don't know about your configuration, but if you concentrate on developing/fixing a single test, you rather do not want to deal with web workers...-t
to not run all tests in that file, but only a single one (here: the one, that has something with ‘show expanded
’ in its name). Same effect can be achieved by glueing .only()
into that file.Upvotes: 4
Reputation: 647
As said a previous answer, you can run the command
jest -t 'fix-order-test'
If you have an it
inside of a describe
block, you have to run
jest -t '<describeString> <itString>'
Upvotes: 17
Reputation: 15045
As mentioned in other answers, test.only
merely filters out other tests in the same file. So tests in other files would still run.
So to run a single test, there are two approaches:
Option 1: If your test name is unique, you can enter t
while in watch mode and enter the name of the test you'd like to run.
Option 2:
p
while in watch mode to enter a regex for the filename you'd like to run. (Relevant commands like this are displayed when you run Jest in watch mode).it
to it.only
on the test you'd like to run.With either of the approaches above, Jest will only run the single test in the file you've specified.
Upvotes: 66
Reputation: 4516
If you have jest
running as a script command, something like npm test
, you need to use the following command to make it work:
npm test -- -t "fix order test"
Upvotes: 50
Reputation: 12814
You can also use f
or x
to focus or exclude a test. For example
fit('only this test will run', () => {
expect(true).toBe(false);
});
it('this test will not run', () => {
expect(true).toBe(false);
});
xit('this test will be ignored', () => {
expect(true).toBe(false);
});
Upvotes: 24