运行 mocha 排除路径
Run mocha excluding paths
我有这个(在 gulpfile.js):
var gulp = require("gulp");
var mocha = require("gulp-mocha");
gulp.task("test", function() {
gulp
.src(["./**/*_test.js", "!./node_modules/**/*.js"]);
});
而且有效。
我想复制相同的行为,不包括 "node_modules" 文件夹,来自 mocha 命令,运行 npm test(在package.json):
"scripts": {
"test": "mocha **\*_test.js !./node_modules/**/*.js*",
}
而且它不起作用。
我正在使用 Windows。
有什么建议吗?
我不是 mocha 或 ant-style 模式的专家,但也许无法在 mocha 命令行中排除特定路径。
您可以将所有测试文件放在一个测试文件夹下,并像这样设置您的 package.json:
"scripts": {
"test": "mocha ./test/**/*_test.js"
}
您也可以提供多个起始文件夹:
"scripts": {
"test": "mocha ./test/**/*_test.js ./another_test_folder/**/*_test.js"
}
正如@thebearingedge 在评论中所建议的那样,最后 我将所有源文件(以及相关测试文件)放在新的 "src" 目录中 。
通过这种方式,我可以使用默认排除 "node_modules" 文件夹的路径定义测试的根目录。
.
├── src
├── fileA.js
├── fileA_test.js
├── fileB.js
├── fileB_test.js
├── node_modules
├── ...
我必须更新 package.json、gulpfile.js 和我用作实用程序的一些批处理文件中的路径。
gulpfile.js的变化:
.src(["./src/**/*_test.js"]);
在package.json中:
"test": "mocha src\**\*_test.js",
简单的改变,它的工作。
- 我可以自由选择我喜欢的任何命名约定。
- 每个测试文件都保持接近相关的 JS 文件。
我有一个包含我所有规格的 spec
目录。在该目录中,我有几个 sub-directories,其中之一是 e2e
specs 目录。在那种情况下,我使用 mocha specs $(find specs -name '*.js' -not -path "specs/e2e/*")
命令来 运行 我所有的测试,忽略了 e2e
目录中的那些。
我能够在 mocha
的参数中使用通配模式来解决这个问题。像你一样,我不想将我所有的测试都放在一个 tests
文件夹下。我希望它们与正在测试的 class 位于同一目录中。我的文件结构如下所示:
project
|- lib
|- class1.js
|- class1.test.js
|- node_modules
|- lots of stuff...
运行 project
文件夹中的这个对我有用:
mocha './{,!(node_modules)/**}/*.test.js'
匹配树中的任何 *.test.js
文件,只要它的路径不以 ./node_modules/
.
为根
这是一个 online tool 用于测试 glob 模式的工具,我发现它很有用。
Windows 用户
此脚本将 运行 完美地
"test": "mocha \"./{,!(node_modules)/**/}*.test.js\"",
希望对您有所帮助。
干杯!
您可以通过传递选项排除 mocha 中的文件
mocha -h|grep -i exclude
--exclude <file> a file or glob pattern to ignore (default: )
mocha --exclude **/*-.jest.js
此外,您还可以创建一个 test/mocha.opts
文件并将其添加到那里
# test/mocha.opts
--exclude **/*-test.jest.js
--require ./test/setup.js
如果你想排除特定的文件类型,你可以这样做
// test/setup.js
require.extensions['.graphql'] = function() {
return null
}
这在使用 mocha 不理解的模块加载器(例如 webpack)处理扩展时很有用。
截至 2019 年,Node 下 configuring Mocha 的现代方式是通过项目根目录中的配置文件(例如通过 .mocharc.js
)。
这里是 .mocharc.js
的例子
- 重新定义默认测试目录(
spec
键)和
- 从整个套件(
exclude
键)中排除示例(或者可以是任何实验测试)。
module.exports = {
'spec': 'src/front/js/tests/**/*.spec.js',
'exclude': 'src/front/js/tests/examples/*.spec.js',
'reporter': 'dot'
};
如您所见,配置中可以使用更多选项。在某种程度上,它们只是 Mocha CLI options. Just look up ones what you like and try to use within .mocharc.js
(use camelCase for dash-comprising CLI options). Or see the config examples.
的复制品
我有这个(在 gulpfile.js):
var gulp = require("gulp");
var mocha = require("gulp-mocha");
gulp.task("test", function() {
gulp
.src(["./**/*_test.js", "!./node_modules/**/*.js"]);
});
而且有效。
我想复制相同的行为,不包括 "node_modules" 文件夹,来自 mocha 命令,运行 npm test(在package.json):
"scripts": {
"test": "mocha **\*_test.js !./node_modules/**/*.js*",
}
而且它不起作用。
我正在使用 Windows。
有什么建议吗?
我不是 mocha 或 ant-style 模式的专家,但也许无法在 mocha 命令行中排除特定路径。
您可以将所有测试文件放在一个测试文件夹下,并像这样设置您的 package.json:
"scripts": {
"test": "mocha ./test/**/*_test.js"
}
您也可以提供多个起始文件夹:
"scripts": {
"test": "mocha ./test/**/*_test.js ./another_test_folder/**/*_test.js"
}
正如@thebearingedge 在评论中所建议的那样,最后 我将所有源文件(以及相关测试文件)放在新的 "src" 目录中 。
通过这种方式,我可以使用默认排除 "node_modules" 文件夹的路径定义测试的根目录。
.
├── src
├── fileA.js
├── fileA_test.js
├── fileB.js
├── fileB_test.js
├── node_modules
├── ...
我必须更新 package.json、gulpfile.js 和我用作实用程序的一些批处理文件中的路径。
gulpfile.js的变化:
.src(["./src/**/*_test.js"]);
在package.json中:
"test": "mocha src\**\*_test.js",
简单的改变,它的工作。
- 我可以自由选择我喜欢的任何命名约定。
- 每个测试文件都保持接近相关的 JS 文件。
我有一个包含我所有规格的 spec
目录。在该目录中,我有几个 sub-directories,其中之一是 e2e
specs 目录。在那种情况下,我使用 mocha specs $(find specs -name '*.js' -not -path "specs/e2e/*")
命令来 运行 我所有的测试,忽略了 e2e
目录中的那些。
我能够在 mocha
的参数中使用通配模式来解决这个问题。像你一样,我不想将我所有的测试都放在一个 tests
文件夹下。我希望它们与正在测试的 class 位于同一目录中。我的文件结构如下所示:
project
|- lib
|- class1.js
|- class1.test.js
|- node_modules
|- lots of stuff...
运行 project
文件夹中的这个对我有用:
mocha './{,!(node_modules)/**}/*.test.js'
匹配树中的任何 *.test.js
文件,只要它的路径不以 ./node_modules/
.
这是一个 online tool 用于测试 glob 模式的工具,我发现它很有用。
Windows 用户 此脚本将 运行 完美地
"test": "mocha \"./{,!(node_modules)/**/}*.test.js\"",
希望对您有所帮助。
干杯!
您可以通过传递选项排除 mocha 中的文件
mocha -h|grep -i exclude
--exclude <file> a file or glob pattern to ignore (default: )
mocha --exclude **/*-.jest.js
此外,您还可以创建一个 test/mocha.opts
文件并将其添加到那里
# test/mocha.opts
--exclude **/*-test.jest.js
--require ./test/setup.js
如果你想排除特定的文件类型,你可以这样做
// test/setup.js
require.extensions['.graphql'] = function() {
return null
}
这在使用 mocha 不理解的模块加载器(例如 webpack)处理扩展时很有用。
截至 2019 年,Node 下 configuring Mocha 的现代方式是通过项目根目录中的配置文件(例如通过 .mocharc.js
)。
这里是 .mocharc.js
的例子
- 重新定义默认测试目录(
spec
键)和 - 从整个套件(
exclude
键)中排除示例(或者可以是任何实验测试)。
module.exports = {
'spec': 'src/front/js/tests/**/*.spec.js',
'exclude': 'src/front/js/tests/examples/*.spec.js',
'reporter': 'dot'
};
如您所见,配置中可以使用更多选项。在某种程度上,它们只是 Mocha CLI options. Just look up ones what you like and try to use within .mocharc.js
(use camelCase for dash-comprising CLI options). Or see the config examples.