我用Mocha来测试我的JavaScript东西。我的测试文件包含5个测试。是否可以运行一个特定的测试(或一组测试),而不是文件中的所有测试?
当前回答
根据您的使用模式,您可能只喜欢使用。我们使用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.
只在“describe”,“it”或“context”之前使用。我使用“$npm run test:unit”运行,它只执行带有.only的单元。
describe.only('get success', function() {
// ...
});
it.only('should return 1', function() {
// ...
});
嗨,上面的解决方案对我不起作用。 运行单个测试的另一种方法是
mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'
这有助于从单个文件和特定名称运行测试用例。
不知道为什么grep方法在使用npm测试时不适合我。不过这是可行的。出于某种原因,我还需要指定测试文件夹。
npm test -- test/sometest.js
如果你使用npm测试(使用包。Json脚本)使用一个额外的——通过mocha传递参数
例如NPM test -- --grep“my second test”
编辑:看起来——grep可能有点麻烦(可能取决于其他参数)。您可以:
修改package.json:
"test:mocha": "mocha --grep \"<DealsList />\" .",
或者使用保释金,这似乎不那么麻烦
npm test -- --bail
推荐文章
- 如何让一个按钮将我的页面重定向到另一个页面?
- 如何让元素被点击(对于整个文档)?
- 我如何检查如果一个变量是JavaScript字符串?
- 如何检测如果多个键被按下一次使用JavaScript?
- 如何通过history. pushstate获得历史变化的通知?
- 使用jQuery改变输入字段的类型
- 在JavaScript中,什么相当于Java的Thread.sleep() ?
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 如何在AngularJS中观察路由变化?