Javascript 如何使用 Mocha 运行单个测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10832031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:06:48  来源:igfitidea点击:

How to run a single test with Mocha?

javascriptmocha

提问by Misha Moroshko

I use Mocha to test my JavaScript stuff. My test file contains 5 tests. Is that possible to run a specific test (or set of tests) rather than all the tests in the file?

我使用 Mocha 来测试我的 JavaScript 内容。我的测试文件包含 5 个测试。是否可以运行特定测试(或一组测试)而不是文件中的所有测试?

回答by Ashe

Try using mocha's --grepoption:

尝试使用mocha 的--grep选项

    -g, --grep <pattern>            only run tests matching <pattern>

You can use any valid JavaScript regex as <pattern>. For instance, if we have test/mytest.js:

您可以使用任何有效的 JavaScript 正则表达式作为<pattern>. 例如,如果我们有test/mytest.js

it('logs a', function(done) {
  console.log('a');
  done();
});

it('logs b', function(done) {
  console.log('b');
  done();
});

Then:

然后:

$ mocha -g 'logs a'

To run a single test. Note that this greps across the names of all describe(name, fn)and it(name, fn)invocations.

运行单个测试。请注意,这会遍历所有describe(name, fn)it(name, fn)调用的名称。

Consider using nested describe()calls for namespacing in order to make it easy to locate and select particular sets.

考虑describe()对命名空间使用嵌套调用,以便轻松定位和选择特定集合。

回答by J Burnett

Depending on your usage pattern, you might just like to use only. We use the TDD style; it looks like this:

根据您的使用模式,你可能只是想用。我们使用 TDD 风格;它看起来像这样:

test.only('Date part of valid Partition Key', function (done) {
    //...
}

Only this test will run from all the files/suites.

仅此测试将从所有文件/套件运行。

回答by Ashley Coolman

If you are using npm test(using package.json scripts) use an extra --to pass the param through to mocha

如果您正在使用npm test(使用 package.json 脚本),请使用额外--的参数将参数传递给 mocha

e.g. npm test -- --grep "my second test"

例如 npm test -- --grep "my second test"

EDIT: Looks like --grepcan be a little fussy (probably depending on the other arguments). You can:

编辑:看起来--grep可能有点挑剔(可能取决于其他参数)。你可以:

Modify the package.json:

修改package.json:

"test:mocha": "mocha --grep \"<DealsList />\" .",

Or alternatively use --bailwhich seems to be less fussy

或者使用--bail似乎不那么挑剔的

npm test -- --bail

回答by Frank Nocke

run single test –by filename–

运行单个测试——按文件名——

Actually, one can also run a single mocha test by filename(not just by ?it()-string-grepping“) if you remove the glob pattern (e.g. ./test/**/*.spec.js) from your mocha.opts, respectively create a copy, without:

实际上,如果您从 mocha.opts 中删除 glob 模式(例如),也可以通过文件名(不仅仅是通过 ?it()-string-grepping“)运行单个 mocha 测试./test/**/*.spec.js,分别创建一个副本,而无需:

node_modules/.bin/mocha --opts test/mocha.single.opts test/self-test.spec.js

Here's my mocha.single.opts (it's only different in missing the aforementioned glob line)

这是我的 mocha.single.opts (只是缺少上述 glob 行不同)

--require ./test/common.js
--compilers js:babel-core/register
--reporter list
--recursive

Background:While you can override the various switches from the opts-File (starting with --) you can't override the glob. That link also has some explanations.

背景:虽然您可以覆盖 opts-File 中的各种开关(以 开头--),但您不能覆盖 glob。该链接也有一些解释。

Hint:if node_modules/.bin/mochaconfuses you, to use the local package mocha. You can also write just mocha, if you have it installed globally.

提示:如果node_modules/.bin/mocha让您感到困惑,请使用本地包 mocha。你也可以只写mocha,如果你全局安装了它。



And if you want the comforts of package.json: Still: remove the **/*-ish glob from your mocha.opts, insert them here, for the all-testing, leave them away for the single testing:

如果您想要舒适的package.json:仍然:**/*从您的 中删除-ish glob mocha.opts,将它们插入此处,进行所有测试,将它们留在单独测试中:

"test": "mocha ./test/**/*.spec.js",
"test-watch": "mocha -R list -w ./test/**/*.spec.js",
"test-single": "mocha ",
"test-single-watch": "mocha -R list -w ",

usage:

用法:

> npm run test

respectively

分别

> npm run test-single -- test/ES6.self-test.spec.js 

(mind the --!)

(注意--!)

回答by QauseenMZ

Just use .only before 'describe', 'it' or 'context'. I run using "$npm run test:unit", and it executes only units with .only.

只需在“描述”、“它”或“上下文”之前使用 .only。我使用“$npm run test:unit”运行,它只执行带有 .only 的单元。

describe.only('get success', function() {
 // ...
});

it.only('should return 1', function() {
  // ...
});

回答by Ruthreshwar

Hi above solutions didn't work for me. The other way of running a single test is

嗨,上述解决方案对我不起作用。运行单个测试的另一种方法是

mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'

This helps to run a test case from a single file and with specific name.

这有助于从单个文件和特定名称运行测试用例。

回答by Jitendra Pawar

npm test <filepath>

eg :

例如:

npm test test/api/controllers/test.js

here 'test/api/controllers/test.js' is filepath.

这里 'test/api/controllers/test.js' 是文件路径。

回答by Bikesh M

You can try "it.only"

你可以试试“it.only”

 it.only('Test one ', () => {

            expect(x).to.equal(y);
        });
it('Test two ', () => {

            expect(x).to.equal(y);
        });

in this the first one only will execute

在这第一个只会执行

回答by Dave

Looking into https://mochajs.org/#usagewe see that simply use

查看https://mochajs.org/#usage我们看到只需使用

mocha test/myfile

摩卡测试/我的文件

will work. You can omit the '.js' at the end.

将工作。您可以省略末尾的“.js”。

回答by Shahin

Consolidate all your tests in one test.js file & your package json add scripts as:

将所有测试合并到一个 test.js 文件中,并将您的包 json 添加脚本为:

  "scripts": {
  "api:test": "node_modules/.bin/mocha --timeout 10000 --recursive api_test/"
},

Type command on your test directory:

在您的测试目录中键入命令:

npm run api:test