我在 ./test
中有 2 个测试文件.
比方说 test1.js
是一个 Mocha 测试,它启动服务器并针对 URI 运行各种请求,以确定它们的行为是否符合预期。test2.js
是一个 Mocha 测试,它使用 Karma 加载特定脚本(包括 angular-mocks.js
)并在几个浏览器中运行以确保那里的所有功能都按预期运行。
在 package.json
我有 test
属性配置如下:
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec -t 5000"
},
酷,我可以
npm test
从命令行运行Mocha。但是哦不——Mocha 运行我的两个脚本,和 test2.js
当然会崩溃整个事情,因为其中的逻辑假设它在 Karma 的上下文中运行(如果我没有正确描述这一点,请原谅我)。我可以有 2 个测试文件夹,
test
和 test-ng
什么的,但我想最终我希望能够npm test
并运行不同的测试集,即:"./node_modules/.bin/mocha --reporter spec -t 5000"
"./node_modules/.bin/karma start"
并将它们配置为每个运行正确的 js 文件。我一直在疯狂地寻找一个示例,其中客户端和服务器端测试存在于同一个 repo 中,但我只找到教程和博客文章等来演示其中一个。有人可以帮助我朝着正确的方向前进吗?
编辑:我应该像这样考虑/组织我的测试吗?
./tests/server/**.js
./tests/e2e/**.js
./tests/unit/**.js
我可能是基于 npm 默认使用
./test/
的事实做出假设的。文件夹。编辑 2:我现在正在或多或少地做我上面描述的事情,并且在 repo 的自述文件中描述了我的测试,例如:
Angular unit tests can be run via Karma:
./node_modules/.bin/karma start karma.conf.js
Angular end-to-end tests can be run via Protractor:
./node_modules/.bin/protractor protractor.conf.js
Express unit tests can be run via Mocha:
./node_modules/.bin/mocha ...
所以现在我不使用
npm test
根本没有,我想知道使用它有什么好处。
最佳答案
你可以把这样的东西放在你的 package.json
中:
"scripts": {
"unittest": "mocha --reporter spec tests/test1.js",
"browsertest": "mocha --reporter spec -t 5000 tests/test2.js",
"test": "npm run unittest && npm run browsertest"
}
并让 Mocha (或 karma 等)每个都有一个脚本,可以由例如单独运行。
$ npm run browsertest
将构建系统的所有部分直接放入 package.json 中的脚本有几个好处:
./node_modules/.bin/mocha
的咒语。 , 因为npm 已经知道
./node_modules/.bin
. (
start
、 stop
、 test
等),从而更容易与他人协作,人和框架(如 Phusion Passenger )。
您可以使用
npm run
获取脚本列表。有关优点的更详尽描述,请参阅 Keith Cirkel 的
great blog post .
关于mocha.js - 如何在同一个代码库/package.json 中包含 Mocha 和 Karma 测试(服务器端和客户端)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641628/