admin管理员组文章数量:1399994
I'm integrating tests into my Node server and am having some trouble with unit testing. When I run npm test
in my root directory, Mocha automatically runs all tests inside of my test
folder. However, the unit tests which are dispersed throughout the project do not run. How can I ensure that Mocha automatically runs them?
I'm integrating tests into my Node server and am having some trouble with unit testing. When I run npm test
in my root directory, Mocha automatically runs all tests inside of my test
folder. However, the unit tests which are dispersed throughout the project do not run. How can I ensure that Mocha automatically runs them?
2 Answers
Reset to default 7You could modify the npm test
mand to find
your test files and run mocha
against them.
Assuming a directory structure like:
project/
src/
test/
main-tests.test.js
things/
test/
thing.test.js
more_things/
more-things.test.js
You would change your package.json
like this:
scripts: {
...
"test": "mocha $(find . -name '*.test.js')"
...
}
There are probably other ways to do this, like specifying the expected glob patterns of the locations of your test files.
Both of these methods require a plain pattern to the names of your test files.
I don't see why you would want to have your tests "dispersed throughout the project". I think you would be much better off keeping all of them in a dedicated test folder. You can mirror the folder structure of your src
folder, if your concern is that you want to easily find the test for a specific source file. Example:
project/
├── src/
│ ├── one/
│ │ └── foo.js
│ └── two/
│ └── bar.js
└── test/
├── one/
│ └── foo.test.js
└── two/
└── bar.test.js
Regardless of whether you decide to reorganise your project, the way to get mocha to execute all test files recursively is with the following:
scripts: {
"test": "mocha \"./**/*.test.js\""
}
You can replace the .
with test
or src
(or any other folder) if you don't want to search the whole project folder.
You can read more about it here: How do I get mocha to execute tests in all subfolders recursively?
本文标签: javascriptHow to get Mocha to execute unit tests in multiple subfolders in NodejsStack Overflow
版权声明:本文标题:javascript - How to get Mocha to execute unit tests in multiple subfolders in Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744160678a2593322.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论