node.js 如何为 mocha 指定测试目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10753288/
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-09-02 15:45:49  来源:igfitidea点击:

How to specify test directory for mocha?

unit-testingnode.jsmocha

提问by Freewind

Mochatries to find test files under testby default, how do I specify another dir, e.g. server-test?

test默认情况下,Mocha尝试查找测试文件,我如何指定另一个目录,例如server-test

采纳答案by Jeff Dickey

Edit : This option is deprecated : https://mochajs.org/#mochaopts

编辑:不推荐使用此选项:https: //mochajs.org/#mochaopts



If you want to do it by still just running mochaon the command line, but wanted to run the tests in a folder ./server-testsinstead of ./test, create a file at ./test/mocha.optswith just this in the file:

如果你想通过仍然只mocha在命令行上运行来做到这一点,但想在一个文件夹./server-tests而不是 中运行测试,请在文件中./test创建一个./test/mocha.opts文件:

server-tests

If you wanted to run everything in that folder and subdirectories, put this into test/mocha.opts

如果您想运行该文件夹和子目录中的所有内容,请将其放入 test/mocha.opts

server-tests
--recursive

mocha.optsare the arguments passed in via the command line, so making the first line just the directory you want to change the tests too will redirect from ./test/

mocha.opts是通过命令行传入的参数,因此将第一行设置为您想要更改测试的目录也将从 ./test/

回答by Behrang

Use this:

用这个:

mocha server-test

Or if you have subdirectories use this:

或者,如果您有子目录,请使用以下命令:

mocha "server-test/**/*.js"

Note the use of double quotes. If you omit them you may not be able to run tests in subdirectories.

注意双引号的使用。如果省略它们,则可能无法在子目录中运行测试。

回答by 250R

Here's one way, if you have subfolders in your test folder e.g.

这是一种方法,如果您的测试文件夹中有子文件夹,例如

/test
/test/server-test
/test/other-test

Then in linux you can use the find command to list all *.js files recursively and pass it to mocha:

然后在 linux 中,您可以使用 find 命令递归列出所有 *.js 文件并将其传递给 mocha:

mocha $(find test -name '*.js')

回答by Michael Bushe

The nice way to do this is to add a "test" npm script in package.json that calls mocha with the right arguments. This way your package.json also describes your test structure. It also avoids all these cross-platform issues in the other answers (double vs single quotes, "find", etc.)

这样做的好方法是在 package.json 中添加一个“测试”npm 脚本,该脚本使用正确的参数调用 mocha。这样你的 package.json 也描述了你的测试结构。它还避免了其他答案中的所有这些跨平台问题(双引号与单引号、“查找”等)

To have mocha run all js files in the "test" directory:

要让 mocha 运行“test”目录中的所有 js 文件:

"scripts": {
    "start": "node ./bin/www", -- not required for tests, just here for context
    "test": "mocha test/**/*.js"
  },

Then to run only the smoke tests call:

然后只运行烟雾测试调用:

npm test

You can standardize the running of all tests in all projects this way, so when a new developer starts on your project or another, they know "npm test" will run the tests. There is good historical precedence for this (Maven, for example, most old school "make" projects too). It sure helps CI when all projects have the same test command.

您可以通过这种方式标准化所有项目中所有测试的运行,因此当新开发人员开始您的项目或其他项目时,他们知道“npm test”将运行测试。这有很好的历史先例(例如,Maven,大多数老派“制作”项目也是如此)。当所有项目具有相同的测试命令时,它肯定有助于 CI。

Similarly, you might have a subset of faster "smoke" tests that you might want mocha to run:

同样,您可能希望 mocha 运行更快的“冒烟”测试的子集:

"scripts": {
    "test": "mocha test/**/*.js"
    "smoketest": "mocha smoketest/**/*.js"
  },

Then to run only the smoke tests call:

然后只运行烟雾测试调用:

npm smoketest

Another common pattern is to place your tests in the same directory as the source that they test, but call the test files *.spec.js. For example: src/foo/foo.js is tested by src/foo/foo.spec.js.

另一种常见模式是将您的测试放在与它们测试的源相同的目录中,但调用测试文件 *.spec.js。例如:src/foo/foo.js 由 src/foo/foo.spec.js 测试。

To run all the tests named *.spec.js by convention:

按照约定运行所有名为 *.spec.js 的测试:

  "scripts": {
    "test": "mocha **/*.spec.js"
  },

Then to run all the tests call:

然后运行所有测试调用:

npm test

See the pattern here? Good. :) Consistency defeats mura.

看到这里的模式了吗?好的。:) 一致性打败mura

回答by gregtczap

Don't use the -g or --grep option, that pattern operates on the name of the test inside of it(), not the filesystem. The current documentation is misleading and/or outright wrong concerning this. To limit the entire command to a portion of the filesystem, you can pass a pattern as the last argument (its not a flag).

不要使用 -g 或 --grep 选项,该模式对 it() 内部的测试名称进行操作,而不是文件系统。当前的文档在这方面具有误导性和/或完全错误。要将整个命令限制为文件系统的一部分,您可以将模式作为最后一个参数(它不是标志)传递。

For example, this command will set your reporter to spec but will only test js files immediately inside of the server-test directory:

例如,此命令会将您的报告器设置为 spec,但只会立即测试 server-test 目录中的 js 文件:

mocha --reporter spec server-test/*.js

This command will do the same as above, plus it will only run the test cases where the it() string/definition of a test begins with "Fnord:":

此命令将执行与上述相同的操作,此外,它只会运行测试的 it() 字符串/定义以“Fnord:”开头的测试用例:

mocha --reporter spec --grep "Fnord:" server-test/*.js

回答by themefield

If in node.js, some new configurations as of Mocha v6:

如果在node.js 中Mocha v6 的一些新配置:

Option 1: Create .mocharc.jsonin project's root directory:

选项1:.mocharc.json在项目的根目录中创建:

{
  "spec": "path/to/test/files"
}

Option 2: add mochaproperty in project's package.json:

选项 2:mocha在项目中添加属性package.json

{
  ...

  "mocha": {
    "spec": "path/to/test/files"
  }
}

More options are here.

更多选项在这里

回答by Anthony Awuley

Run all files in test_directoryincluding sub directories that match test.js

运行test_directory包含匹配的子目录中的所有文件test.js

find ./parent_test_directory -name '*test.js' | xargs mocha -R spec

or use the --recursiveswitch

或使用--recursive开关

mocha --recursive test_directory/

回答by jfunk

I had this problem just now and solved it by removing the --recursiveoption (which I had set) and using the same structure suggested above:

我刚刚遇到了这个问题,并通过删除--recursive选项(我已经设置)并使用上面建议的相同结构来解决它:

mochify "test/unit/**/*.js"

mochify "test/unit/**/*.js"

This ran all tests in all directories under /test/unit/for me while ignoring the other directories within /test/

/test/unit/为我运行了所有目录中的所有测试,同时忽略了其中的其他目录/test/

回答by rajeshchauhan23102008

Now a days(year 2020)you can handle this using mocha configuration file:

现在几天(2020 年),您可以使用mocha 配置文件处理此问题:

Step 1: Create .mocharc.jsfile at the root locationof your application

步骤 1:在应用程序的根位置创建 .mocharc.js文件

Step 2: Addbelow codein mocha config file:

第 2 步:在 mocha 配置文件中添加以下代码

'use strict';

module.exports = {
  spec: 'src/app/**/*.test.js'
};

For More option in config file refer this link: https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.js

有关配置文件中的更多选项,请参阅此链接:https: //github.com/mochajs/mocha/blob/master/example/config/.mocharc.js

回答by Cozzbie

If you are using nodejs, in your package.jsonunder scripts

如果您正在使用nodejs, 在您的package.jsonscripts

  1. For global (-g)installations: "test": "mocha server-test"or "test": "mocha server-test/**/*.js"for subdocuments
  2. For projectinstallations: "test": "node_modules/mocha/bin/mocha server-test"or "test": "node_modules/mocha/bin/mocha server-test/**/*.js"for subdocuments
  1. 对于global (-g)安装:"test": "mocha server-test""test": "mocha server-test/**/*.js"用于子文档
  2. 对于project安装:"test": "node_modules/mocha/bin/mocha server-test""test": "node_modules/mocha/bin/mocha server-test/**/*.js"用于子文档

Then just run your tests normally as npm test

然后只需正常运行您的测试 npm test