[英]How to run mocha and mocha-phantomjs tests from one “npm test” command in node.js?
[英]How to run mocha test file from an npm scripts command?
我正在关注Mocha 测试文档以在某些 API 路由上设置单元测试。 为此,我设置了一个包含 index.test.js 的\\test
目录。 测试文件所需的安装包,包括mocha
。 然后在 package.json 中指定运行 mocha 的命令, npm test
:
"scripts": {
"test": "mocha ./test",
"start": "node index.js"
},
但是当我从包含 index.test.js 的测试目录中运行npm test
时。 测试似乎没有运行,只是告诉我:
我还提到了这个 Stackoverfow 的答案无济于事 - 配置节点 npm package.json 以便“npm test”适用于 unix 和 windows
题:
如何从 npm 脚本运行 mocha 测试文件?
这是测试文件的目录位置 - C:\\Users\\brianj\\Documents\\Projects\\WebService-for-Self-service-Metrics-Portal\\test\\index.test.js
这是来自\\test
名为 index.test.js 的实际测试文件,在调用 mocha 之前已经安装了所需的包:
var chai = require('chai');
var should = chai.should();
var sinon = require('sinon');
var request = require('supertest');
var _ = require('lodash');
var express = require('express');
var app = express();
// Global array to store all relevant args of calls to app.use
var APP_USED = []
// Replace the `use` function to store the routers and the urls they operate on
app.use = function() {
var urlBase = arguments[0];
// Find the router in the args list
_.forEach(arguments, function(arg) {
if (arg.name == 'router') {
APP_USED.push({
urlBase: urlBase,
router: arg
});
}
});
};
// GRAB all the routes from our saved routers:
_.each(APP_USED, function(used) {
// On each route of the router
_.each(used.router.stack, function(stackElement) {
if (stackElement.route) {
var path = stackElement.route.path;
var method = stackElement.route.stack[0].method.toUpperCase();
console.log(method + " -> " + used.urlBase + path);
describe(method + " -> " + used.urlBase + path, function() {
request(app)
.get(used.urlBase + path)
.expect('Content-Type', /json/)
.expect(200, "ok")
.end(function(err, res){
if (err) throw err;
});
}); //
}
});
});
如果您是测试目录的上一级目录,您可以尝试 npm run test 吗? 或者,如果您在测试目录中,则可以尝试npm run ../test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.