我用Mocha来测试我的JavaScript东西。我的测试文件包含5个测试。是否可以运行一个特定的测试(或一组测试),而不是文件中的所有测试?
当前回答
只在“describe”,“it”或“context”之前使用。我使用“$npm run test:unit”运行,它只执行带有.only的单元。
describe.only('get success', function() {
// ...
});
it.only('should return 1', function() {
// ...
});
其他回答
尝试使用摩卡的——grep选项:
-g, --grep <pattern> only run tests matching <pattern>
您可以使用任何有效的JavaScript正则表达式作为<pattern>。例如,如果我们有test/mytest.js:
it('logs a', function(done) {
console.log('a');
done();
});
it('logs b', function(done) {
console.log('b');
done();
});
然后:
$ mocha -g 'logs a'
运行单个测试。注意,这个greps对所有describe(name, fn)和it(name, fn)调用的名称都适用。
考虑对命名空间使用嵌套的describe()调用,以便便于定位和选择特定的集合。
对于那些希望运行单个文件但又不能使其工作的人来说,对我有用的是,我需要将我的测试用例包装在如下所示的描述套件中,然后使用描述标题,例如。'My Test Description'作为模式。
describe('My Test Description', () => {
it('test case 1', () => {
// My test code
})
it('test case 2', () => {
// My test code
})
})
然后运行
纱线测试-g“我的测试描述”
or
运行我的测试说明
执行single test - by filename -
实际上,如果你从你的mocha中删除glob模式(例如./test/**/*.spec.js),你也可以通过文件名(而不仅仅是通过" it()-string-grepping ")来运行一个单独的mocha测试。选项,分别创建一个副本,没有:
node_modules/.bin/mocha --opts test/mocha.single.opts test/self-test.spec.js
这是我的mocha.single.opts(唯一的不同是错过了前面提到的glob行)
--require ./test/common.js
--compilers js:babel-core/register
--reporter list
--recursive
背景:虽然你可以覆盖options - file中的各种开关(从——开始),但你不能覆盖glob。这种联系也有 一些解释。
提示:如果node_modules/.bin/mocha让你感到困惑,请使用本地包mocha。你也可以只写mocha,如果你全局安装了它。
如果你想要舒适的包装。仍然:从摩卡中删除**/*-ish glob。选项,将它们插入这里,用于所有测试,将它们留给单个测试:
"test": "mocha ./test/**/*.spec.js",
"test-watch": "mocha -R list -w ./test/**/*.spec.js",
"test-single": "mocha",
"test-single-watch": "mocha -R list -w",
用法:
> npm run test
分别
> npm run test-single -- test/ES6.self-test.spec.js
注意——它将后面的任何文本链接到NPM脚本
根据您的使用模式,您可能只喜欢使用。我们使用TDD风格;它是这样的:
test.only('Date part of valid Partition Key', function (done) {
//...
}
只有这个测试将从所有文件/套件中运行。
有多种方法可以做到这一点。
If you just want to run one test from your entire list of test cases then, you can write only ahead of your test case. it.only('<test scenario name>', function() { // ... }); or you can also execute the mocha grep command as below mocha -g <test-scenario-name> If you want to run all the test cases which are inside one describe section, then you can also write only to describe as well. describe.only('<Description of the tests under this section>', function() { // ... }); If you have multiple test files & you wanted to run only one of then you can follow the below command. npm test <filepath> eg : npm test test/api/controllers/test.js here 'test/api/controllers/test.js' is filepath.
推荐文章
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 如何在摩卡编程跳过测试?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?