I'm using Jest framework and have a test suite. I want to turn off/skip one of my tests.
Googling documentation doesn't give me answers.
Do you know the answer or source of information to check?
I'm using Jest framework and have a test suite. I want to turn off/skip one of my tests.
Googling documentation doesn't give me answers.
Do you know the answer or source of information to check?
I found the answer here
test('it is raining', () => {
expect(inchesOfRain()).toBeGreaterThan(0);
});
test.skip('it is not snowing', () => {
expect(inchesOfSnow()).toBe(0);
});
You can also exclude test
or describe
by prefixing them with an x
.
Individual Tests
describe('All Test in this describe will be run', () => {
xtest('Except this test- This test will not be run', () => {
expect(true).toBe(true);
});
test('This test will be run', () => {
expect(true).toBe(true);
});
});
Multiple tests inside a describe
xdescribe('All tests in this describe will be skipped', () => {
test('This test will be skipped', () => {
expect(true).toBe(true);
});
test('This test will be skipped', () => {
expect(true).toBe(true);
});
});
If you'd like to skip a test in Jest, you can use test.skip:
test.skip(name, fn)
Which is also under the following aliases:
it.skip(name, fn)
or xit(name, fn)
or xtest(name, fn)
Additionally, if you'd like to skip a test suite, you can use describe.skip:
describe.skip(name, fn)
Which is also under the following alias:
xdescribe(name, fn)
I would add this answer for the people that may fall here. As i was searching myself.
[only, skip, aliases (more details), doc refs, different variants (each, concurrent, failing), how to ignore test files (last section)]
You can skim. To get the sections.
Use .skip()
function to skip a test or a test suite (describe)
test('something', () => {})
=>
test.skip('something', () => {})
You can use aliases:
xtest('something', () => {})
The same applies to it
which is an alias as well.
it.skip('something', () => {})
xit('something', () => {})
For a descripe
:
describe.skip(() => {})
xdescribe(() => {})
doc refs:
https://jestjs.io/docs/api#testskipname-fn
https://jestjs.io/docs/api#describeskipname-fn
Aliases are to be found under the element title:
You can also test when @types/jest is installed through your editor:
ftest
doesn't exit for example. See the next only()
section.
To note:
test.skip.each(table)(name, fn)
For Data driven tests. Same aliases apply as well. Check the ref bellow.
ref: https://jestjs.io/docs/api#testskipeachtablename-fn
Same with:
test.concurrent.skip.each(table)(name, fn)
for concurrent tests. No x alias for this one. Check the ref bellow.
ref: https://jestjs.io/docs/api#testconcurrentskipeachtablename-fn
And,
describe.skip.each(table)(name, fn)
ref: https://jestjs.io/docs/api#describeskipeachtablename-fn
test.skip.failing(name, fn, timeout)
ref: https://jestjs.io/docs/api#testskipfailingname-fn-timeout
Use only()
if you want to run only that test and nothing else.
You can use only()
with multiple tests or test suites (describe). That would run only the one that have only()
added to them
Note: only will act only at the file level. And not cross all files.
test('test 1', () => {})
test('test 2', () => {})
test('test 3', () => {})
test.only('test 4', () => {}) // only this would run in this file
multiple ones:
test('test 1', () => {})
test.only('test 2', () => {}) // only this
test('test 3', () => {})
test.only('test 4', () => {}) // and this would run in this file
With describe:
describe(() => {
test('test 1', () => {}) // nothing will run here
test.only('test 2', () => {}) //
})
describe.only(() => {
test('test 1', () => {})
test.only('test 2', () => {}) // only this test
test('test 3', () => {})
test.only('test 4', () => {}) // and this one would be run (because this is the only active describe block)
})
describe(() => {
test('test 1', () => {}). // No test will run here
test.only('test 2', () => {}) //
})
adding an f
. But be careful ftest()
is not an alias !!!!
it.only()
fit()
describe.only()
fdescribe()
ftest() // WRONG ERROR !!! No such an alias SADLY!!!
Well, it's not sad. I personally prefer to use only()
as it's more verbose and readable.
doc refs:
https://jestjs.io/docs/api#testonlyname-fn-timeout
https://jestjs.io/docs/api#describeonlyname-fn
Note too:
describe.only.each(table)(name, fn)
ref: https://jestjs.io/docs/api#describeonlyeachtablename-fn
test.concurrent.only.each(table)(name, fn)
ref: https://jestjs.io/docs/api#testconcurrentonlyeachtablename-fn
test.only.failing(name, fn, timeout)
ref: https://jestjs.io/docs/api#testonlyfailingname-fn-timeout
This section is for people who were searching for how to ignore full files.
A regex (regex not glob)
. Of test files to ignore if the regex match them.
https://jestjs.io/docs/configuration#testpathignorepatterns-arraystring
example:
jest.config.ts
import type { Config } from '@jest/types';
const config: Config.InitialOptions = {
preset: 'ts-jest',
testEnvironment: 'node',
verbose: true,
automock: false,
testPathIgnorePatterns: ['.*/__fixtures__/.*'] // here
};
export default config;
Or using the cli:
ref: https://jestjs.io/docs/cli#--testpathignorepatternsregexarray
jest --testPathIgnorePatterns=".*/__fixtures__/.*"
For an array (multiple regex):
There are different ways:
# one regex with or operator `|`
jest --testPathIgnorePatterns=".*/__fixtures__/.*|<rootDir>/src/someDir/"
# paranthesis with spaces
jest --testPathIgnorePatterns="\(.*/__fixtures__/.* <rootDir>/src/someDir/\)"
# using the argument multiple times
jest --testPathIgnorePatterns=".*/__fixtures__/.*" --testPathIgnorePatterns="<rootDir>/src/someDir/"
testMatch can be another way to determine what should run (just like only()
)
It does use globs
and not regex
ref: https://jestjs.io/docs/configuration#testmatch-arraystring
You can use the glob negation to ignore some files as well.
Example:
{
// ... rest of the package
"jest": {
"testMatch": [
"**/__tests__/**/!(DISABLED.)*.[jt]s?(x)",
"**/!(DISABLED.)?(*.)+(spec|test).[tj]s?(x)"
]
}
}
There is the cli version as well:
https://jestjs.io/docs/cli#--testmatch-glob1--globn
Note that without any arguments
jest my-test #or
jest path/to/my-test.js
jest **/some/**/*.spec.*
That would only run the test files that are matched by the patterns.
ref: https://jestjs.io/docs/cli#running-from-the-command-line
If you are debugging/writing/extending a test in a test suite which has many tests you just need to run the one you are working on but adding .skip
to everyone can be painful.
So .only
comes to the rescue. You can optionally skip others during testing using .only
flag, it can be super useful for big suites where you need to add or debug a test.
describe('some amazing test suite', () => {
test('number one', () => {
// some testing logic
}
test('number two', () => {
// some testing logic
}
...
test('number x', () => {
// some testing logic
}
test.only('new test or the one needs debugging', () => {
// Now when you run this suite only this test will run
// so now you are free to debug or work on business logic
// instead to wait for other tests to pass every time you run jest
// You can remove `.only` flag once you're done!
}
}
You can also add .only
to more than one tests in a test suite or file if you want to run multiple in conjunction!