我正在使用Jest框架并拥有一个测试套件,我想要关闭/跳过其中的一个测试。
搜索文档并没有给我答案。
您是否知道答案或检查信息来源?
我正在使用Jest框架并拥有一个测试套件,我想要关闭/跳过其中的一个测试。
搜索文档并没有给我答案。
您是否知道答案或检查信息来源?
我在这里找到了答案
test('it is raining', () => {
expect(inchesOfRain()).toBeGreaterThan(0);
});
test.skip('it is not snowing', () => {
expect(inchesOfSnow()).toBe(0);
});
test.only()
- ptimyarn test
时根本无法工作,jest非常烦人。 - marko kraljevic你也可以通过在 x
前缀下排除 test
或 describe
。
个别测试
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);
});
});
在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);
});
});
如果你想要跳过 Jest 中的某个测试,可以使用 test.skip:
test.skip(name, fn)
还可以使用以下别名:
it.skip(name, fn)
xit(name, fn)
xtest(name, fn)
此外,如果您想跳过一个测试套件,可以使用 describe.skip:
describe.skip(name, fn)
也可以使用以下别名:
xdescribe(name, fn)
我想为那些可能会遇到这个问题的人添加这个答案,因为我自己也在搜索。
[仅限、跳过、别名(更多细节)、文档引用、不同变体(每个、并发、失败)、如何忽略测试文件(最后一节)]
你可以略读来获取各个部分。
使用 .skip()
函数来跳过一个测试或测试套件(描述)。
test('something', () => {})
=>
test.skip('something', () => {})
您可以使用别名:
xtest('something', () => {})
it
,它也是一个别名。it.skip('something', () => {})
xit('something', () => {})
关于一个描述
:
describe.skip(() => {})
xdescribe(() => {})
文档参考:
https://jestjs.io/docs/api#testskipname-fn
https://jestjs.io/docs/api#describeskipname-fn
别名可以在元素标题下找到:
你也可以通过编辑器测试是否安装了@types/jest:
ftest
不存在,例如。请参见下一个 only()
部分。
注意:
test.skip.each(table)(name, fn)
针对数据驱动测试,同样适用相同的别名。请查看下面的参考链接。
参考链接:https://jestjs.io/docs/api#testskipeachtablename-fn
同样适用于:
test.concurrent.skip.each(table)(name, fn)
用于并发测试。此测试无 x 别名。请查看下面的参考链接。
参考链接: https://jestjs.io/docs/api#testconcurrentskipeachtablename-fn
还有,
describe.skip.each(table)(name, fn)
参考: https://jestjs.io/docs/api#describeskipeachtablename-fn
test.skip.failing(name, fn, timeout)
参考: https://jestjs.io/docs/api#testskipfailingname-fn-timeout
如果您想仅运行一个测试用例而不是其他内容,请使用 only()
。
您可以将 only()
与多个测试或测试套件(describe)一起使用。这将仅运行已添加 only()
的测试用例。
注意:only仅在文件级别上起作用,而不跨越所有文件。
test('test 1', () => {})
test('test 2', () => {})
test('test 3', () => {})
test.only('test 4', () => {}) // only this would run in this file
多个:
test('test 1', () => {})
test.only('test 2', () => {}) // only this
test('test 3', () => {})
test.only('test 4', () => {}) // and this would run in this file
使用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', () => {}) //
})
添加一个 f
。但要小心,ftest()
不是别名!!!
it.only()
fit()
describe.only()
fdescribe()
ftest() // WRONG ERROR !!! No such an alias SADLY!!!
嗯,这并不令人难过。我个人更喜欢使用only()
,因为它更详细和易读。
文档参考:
https://jestjs.io/docs/api#testonlyname-fn-timeout
https://jestjs.io/docs/api#describeonlyname-fn
也请注意:
describe.only.each(table)(name, fn)
参考: https://jestjs.io/docs/api#describeonlyeachtablename-fn
test.concurrent.only.each(table)(name, fn)
参考: https://jestjs.io/docs/api#testconcurrentonlyeachtablename-fn
test.only.failing(name, fn, timeout)
引用:https://jestjs.io/docs/api#testonlyfailingname-fn-timeout
本节面向那些正在寻找如何忽略整个文件的人。
一个正则表达式(不是 glob)
。如果正则表达式匹配,则忽略测试文件。
https://jestjs.io/docs/configuration#testpathignorepatterns-arraystring
例子:
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;
或者使用命令行界面:
参考:https://jestjs.io/docs/cli#--testpathignorepatternsregexarray
jest --testPathIgnorePatterns=".*/__fixtures__/.*"
对于一个数组(多个正则表达式):
有不同的方法:
# 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
可以是另一种确定应该运行什么的方式(就像only()
一样)
它使用的是globs
而不是regex
参考链接:https://jestjs.io/docs/configuration#testmatch-arraystring
您还可以使用glob negation来忽略某些文件。
例如:
{
// ... rest of the package
"jest": {
"testMatch": [
"**/__tests__/**/!(DISABLED.)*.[jt]s?(x)",
"**/!(DISABLED.)?(*.)+(spec|test).[tj]s?(x)"
]
}
}
还有命令行版本:
https://jestjs.io/docs/cli#--testmatch-glob1--globn
请注意,如果没有任何参数。jest my-test #or
jest path/to/my-test.js
jest **/some/**/*.spec.*
这将只运行与模式匹配的测试文件。
如果你正在调试/编写/扩展测试套件中的一个测试,而该套件包含许多测试,你只需要运行正在处理的测试,但将.skip
添加到每个测试可能很麻烦。
因此,.only
可以帮助解决问题。你可以使用.only
标志可选择跳过其他测试进行测试。对于需要添加或调试测试的大型测试套件,它非常有用。
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!
}
}
如果你想要同时运行多个测试用例,可以在测试套件或文件中为多个测试用例添加.only
标记!
.only
同样也可以和 it()
一起使用,就像 .skip
一样。 - Bilal Shafi