提问人:Gleichmut 提问时间:1/6/2018 最后编辑:Seth McClaineGleichmut 更新时间:3/22/2023 访问量:142491
跳过测试文件 Jest 中的一个测试
Skip one test in test file Jest
答:
我在这里找到了答案
test('it is raining', () => {
expect(inchesOfRain()).toBeGreaterThan(0);
});
test.skip('it is not snowing', () => {
expect(inchesOfSnow()).toBe(0);
});
评论
test.only()
yarn test
您也可以排除或在它们前面加上 .test
describe
x
个人测试
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);
});
});
描述中的多个测试
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
所以来救援了。您可以选择在测试期间使用 flag 跳过其他人,这对于需要添加或调试测试的大型套件非常有用。.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
我会为可能跌倒在这里的人添加这个答案。当我在寻找自己时。
[仅、跳过、别名(更多详细信息)、文档引用、不同的变体(每个、并发、失败)、如何忽略测试文件(最后一节)]
你可以略读。获取部分。
从文件中跳过或仅运行一个
skip() [包括文档引用]
使用函数跳过测试或测试套件(描述).skip()
test('something', () => {})
=>
test.skip('something', () => {})
您可以使用别名:
xtest('something', () => {})
这同样适用于别名。it
it.skip('something', () => {})
xit('something', () => {})
对于一个 :descripe
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()
可以与多个测试或测试套件一起使用(描述)。这将只运行添加到它们的那个only()
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(() => {
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', () => {}) //
})
only() 的别名:
添加 .但要小心不是别名!!!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
使用 config 或 cli 忽略测试
本节适用于正在寻找如何忽略完整文件的人。
testPathIgnorePatterns
正则表达式 .的测试文件,如果正则表达式匹配,则忽略它们。(regex not 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;
或者使用 cli:
编号: 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/"
test匹配
testMatch 可以是确定应该运行的内容的另一种方法(就像only()
)
它确实使用而不是globs
regex
编号: https://jestjs.io/docs/configuration#testmatch-arraystring
您也可以使用 glob 否定来忽略某些文件。
例:
{
// ... rest of the package
"jest": {
"testMatch": [
"**/__tests__/**/!(DISABLED.)*.[jt]s?(x)",
"**/!(DISABLED.)?(*.)+(spec|test).[tj]s?(x)"
]
}
}
还有 cli 版本:
https://jestjs.io/docs/cli#--testmatch-glob1--globn
请注意,没有任何参数
jest my-test #or
jest path/to/my-test.js
jest **/some/**/*.spec.*
这将仅运行与模式匹配的测试文件。
编号: https://jestjs.io/docs/cli#running-from-the-command-line
评论
it.only()
test.only()
x
评论