Mis*_*hko 263 javascript mocha.js
我用Mocha来测试我的JavaScript东西.我的测试文件包含5个测试.是否可以运行特定测试(或测试集)而不是文件中的所有测试?
Ash*_*she 327
尝试使用mocha的--grep选项:
-g, --grep <pattern> only run tests matching <pattern>
Run Code Online (Sandbox Code Playgroud)
您可以使用任何有效的JavaScript正则表达式<pattern>.例如,如果我们有test/mytest.js:
it('logs a', function(done) {
console.log('a');
done();
});
it('logs b', function(done) {
console.log('b');
done();
});
Run Code Online (Sandbox Code Playgroud)
然后:
$ mocha -g 'logs a'
Run Code Online (Sandbox Code Playgroud)
运行单个测试.请注意,这会覆盖所有describe(name, fn)和it(name, fn)调用的名称.
考虑describe()对命名空间使用嵌套调用,以便于查找和选择特定集合.
J B*_*ett 151
根据您的使用模式,你可能只是想用只.我们使用TDD风格; 它看起来像这样:
test.only('Date part of valid Partition Key', function (done) {
//...
}
Run Code Online (Sandbox Code Playgroud)
只有这个测试将从所有文件/套件运行.
Ash*_*man 74
如果您正在使用npm test(使用package.json脚本),请使用extra --将param传递给mocha
例如 npm test -- --grep "my second test"
编辑:看起来--grep可能有点挑剔(可能取决于其他参数).您可以:
修改package.json:
"test:mocha": "mocha --grep \"<DealsList />\" .",
Run Code Online (Sandbox Code Playgroud)
或者使用--bail似乎不那么繁琐的用途
npm test -- --bail
Run Code Online (Sandbox Code Playgroud)
Qau*_*nMZ 37
只需在“描述”、“它”或“上下文”之前使用 .only。我使用“$npm run test:unit”运行,它只执行带有 .only 的单元。
describe.only('get success', function() {
// ...
});
it.only('should return 1', function() {
// ...
});
Run Code Online (Sandbox Code Playgroud)
Fra*_*cke 14
实际上,如果从mocha.opts中删除glob模式(例如),分别创建一个副本,也可以按文件名运行单个mocha测试(不仅仅是"it() - string-grepping")./test/**/*.spec.js,不需要:
node_modules/.bin/mocha --opts test/mocha.single.opts test/self-test.spec.js
Run Code Online (Sandbox Code Playgroud)
这是我的mocha.single.opts(它只是缺少前面提到的glob线)
--require ./test/common.js
--compilers js:babel-core/register
--reporter list
--recursive
Run Code Online (Sandbox Code Playgroud)
背景:虽然您可以从opts-File(从头开始--)覆盖各种开关,但您无法覆盖glob.该链接也有一些解释.
提示:如果node_modules/.bin/mocha困惑你,使用本地包mocha.mocha如果您已全局安装,也可以只编写.
如果您想要以下方面的舒适package.json:静态:**/*从您的中移除-ish glob mocha.opts,将它们插入此处,进行全部测试,将它们留给单个测试:
"test": "mocha ./test/**/*.spec.js",
"test-watch": "mocha -R list -w ./test/**/*.spec.js",
"test-single": "mocha $1",
"test-single-watch": "mocha -R list -w $1",
Run Code Online (Sandbox Code Playgroud)
用法:
> npm run test
Run Code Online (Sandbox Code Playgroud)
分别
> npm run test-single -- test/ES6.self-test.spec.js
Run Code Online (Sandbox Code Playgroud)
(介意--!)
Jit*_*war 10
npm test <filepath>
Run Code Online (Sandbox Code Playgroud)
例如:
npm test test/api/controllers/test.js
Run Code Online (Sandbox Code Playgroud)
这里'test/api/controllers/test.js'是文件路径.
嗨以上解决方案对我不起作用.运行单个测试的另一种方法是
mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'
Run Code Online (Sandbox Code Playgroud)
这有助于从单个文件和特定名称运行测试用例.
小智 6
使用Mocha 的--fgrep(或只是 -f),您可以选择包含字符串的测试,例如:
mocha -f 'my test x'
Run Code Online (Sandbox Code Playgroud)
将运行my test x包含it(),describe()或context()块中的所有测试。
不知道为什么 grep 方法在使用 npm test 时对我不起作用。这虽然有效。由于某种原因,我还需要指定测试文件夹。
npm test -- test/sometest.js
Run Code Online (Sandbox Code Playgroud)
您可以尝试“仅它”
it.only('Test one ', () => {
expect(x).to.equal(y);
});
it('Test two ', () => {
expect(x).to.equal(y);
});
Run Code Online (Sandbox Code Playgroud)
在这第一个只会执行
对于那些想要运行单个文件但无法使其工作的人来说,对我有用的是我需要将测试用例包装在描述套件中,如下所示,然后使用描述标题(例如“我的测试描述”)作为模式。
describe('My Test Description', () => {
it('test case 1', () => {
// My test code
})
it('test case 2', () => {
// My test code
})
})
Run Code Online (Sandbox Code Playgroud)
然后运行
yarn test -g "My Test Description"
或者
npm run test -g "My Test Description"