我用Mocha来测试我的JavaScript东西。我的测试文件包含5个测试。是否可以运行一个特定的测试(或一组测试),而不是文件中的所有测试?
当前回答
不知道为什么grep方法在使用npm测试时不适合我。不过这是可行的。出于某种原因,我还需要指定测试文件夹。
npm test -- test/sometest.js
其他回答
你可以试试"it.only"
it.only('Test one ', () => {
expect(x).to.equal(y);
});
it('Test two ', () => {
expect(x).to.equal(y);
});
在这种情况下,只执行第一个
只在“describe”,“it”或“context”之前使用。我使用“$npm run test:unit”运行,它只执行带有.only的单元。
describe.only('get success', function() {
// ...
});
it.only('should return 1', function() {
// ...
});
使用Mocha的——fgrep(或者-f)你可以选择包含字符串的测试,例如:
mocha -f 'my test x'
将在it()、describe()或context()块中运行包含我的测试x的所有测试。
执行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脚本
有多种方法可以做到这一点。
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.
推荐文章
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 如何在AngularJS中观察路由变化?
- JavaScript DOM删除元素
- 将dd-mm-yyyy字符串转换为日期
- Javascript复选框onChange
- Javascript函数前导bang !语法
- 如何在页面上遍历所有DOM元素?
- 在JS/jQuery中触发按键/按键/按键事件?
- 如何每5秒重新加载页面?