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
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
.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
./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.-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.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);
});
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.
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>'