Javascript 无法解析 Mocha 中的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26182161/
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
Cannot resolve path in Mocha
提问by purewisdom1
I am currently using Nitrous, which shouldn't matter, but I needed to install mocha so I typed in:
我目前正在使用 Nitrous,这无关紧要,但我需要安装 mocha,所以我输入:
npm install - g mocha.
Everything installed and when I try to run mocha in my command line I get the following error:
已安装所有内容,当我尝试在命令行中运行 mocha 时,出现以下错误:
/home/action/.parts/lib/node_modules/mocha/bin/_mocha:454
if (!files.length) throw new Error("cannot resolve path (or pattern) '"Error: cannot resolve path (or pattern) 'test/unit'
at lookupFiles (/home/action/.parts/lib/node_modules/mocha/bin/_mocha:454:32)
at runAgain (/home/action/.parts/lib/node_modules/mocha/bin/_mocha:305:24)
at Array.forEach (native)
at Object. (/home/action/.parts/lib/node_modules/mocha/bin/_mocha:304:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
/home/action/.parts/lib/node_modules/mocha/bin/_mocha:454
if (!files.length) throw new Error("cannot resolve path (or pattern) '"错误:无法解析路径(或模式)'test/unit'
at lookupFiles (/home/action/.parts/lib/node_modules/mocha/bin/_mocha:454:32)
at runAgain (/home/action/.parts/ lib/node_modules/mocha/bin/_mocha:305:24)
在 Array.forEach(本机)
在 Object。(/home/action/.parts/lib/node_modules/mocha/bin/_mocha:304:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js: 474:10)
在 Module.load (module.js:356:32)
在 Function.Module._load (module.js:312:12)
在 Function.Module.runMain (module.js:497:10)
在启动时 ( node.js:119:16)
在 node.js:902:3
Does anyone know how to solve this?
有谁知道如何解决这个问题?
回答by mantoni
By default, mocha includes ./test/*.js. So if that is where your tests live, just running mochais all you need.
默认情况下,摩卡咖啡包括./test/*.js. 因此,如果那是您的测试所在,mocha那么您只需要运行即可。
If you have your tests in test/unit, you might want to run mocha ./test/unit/*.js.
如果你有你的测试test/unit,你可能想要运行mocha ./test/unit/*.js.
回答by Louis
The error you are getting is consistent with not having a filenamed test/unitand doing this:
您收到的错误与没有命名文件test/unit并执行此操作一致:
$ mocha test/unit
Check that the file exists. (Actually, without a .jsextension, I'd expect a directory rather than a file.) Check that you are in the right location when you issue your command.
检查文件是否存在。(实际上,如果没有.js扩展名,我希望是目录而不是文件。)当您发出命令时,请检查您是否位于正确的位置。
回答by Sam Berry
For anyone who gets this error when trying to run:
对于在尝试运行时遇到此错误的任何人:
> npm test
Make sure that your package.json file includes a test script in the format of:
确保您的 package.json 文件包含以下格式的测试脚本:
<project-root>/<custom-path-to-test>/*.js
<project-root>/<custom-path-to-test>/*.js
For example:
例如:
{
"name": "Project Name",
...
"scripts": {
"test": "mocha ./node/test/*.js"
}
...
}
回答by Combine
For those having mocha installed and have makefile like this:
对于那些安装了 mocha 并且有这样的 makefile 的人:
test:
@./node_modules/.bin/mocha -u tdd
.PHONY: test
but getting this error:
但收到此错误:
Error: cannot resolve path (or pattern) 'test'
you need to add folder "test" in the project folder. And your test files in that testfolder
您需要在项目文件夹中添加文件夹“test”。以及该测试文件夹中的测试文件
E.G.
/home/you/nodejs/chapter3
├── lib
├── makefile
├── node_modules
├── package.json
└── test
├── db.test.js
回答by Shahin
npm install -g mocha
npm install mocha --save-dev
write your test : test.jsadd below script in your package json file
编写您的测试:test.js在您的包 json 文件中添加以下脚本
"scripts": {
"api:test": "node_modules/.bin/mocha --timeout 10000 --recursive test/"
},
go to your test directory : npm run api:test
转到您的测试目录: npm run api:test
回答by Ignatius Andrew
Just move all the tests to the testfolder, If you dont have create one.
只需将所有测试移动到test文件夹中,如果您没有创建一个。
and in the package.json file just enter 'mocha'
并在 package.json 文件中输入“mocha”
"scripts":{
"test": "mocha"
},
and run
并运行
npm test
in the terminal.
在终端。
回答by Devdutta Goyal
If you run npm testit will execute the *.jsfiles only in test directory not in sub directories.
如果你运行npm test它*.js只会在测试目录而不是在子目录中执行文件。
If you also want to execute the *.jstest files inside sub directories of test folder.
如果您还想在*.js测试文件夹的子目录中执行测试文件。
Follow the following steps :
请按照以下步骤操作:
- Create a mocha.opts file in test directory
- Add following line in mocha.opts file
--recursive
- 在 test 目录下创建一个 mocha.opts 文件
- 在 mocha.opts 文件中添加以下行
--recursive

