[英]Mocha tests in node.js hanging or missing
At the moment I am working on a project for which I am supposed to write the routing using express.js in node.js.目前我正在开发一个项目,我应该在 node.js 中使用 express.js 编写路由。
The tests and the other parts of the project were not written by me, therefore I assume there is no error in them.测试和项目的其他部分不是我编写的,因此我认为它们没有错误。
The problem I am having is: At the current stage of the program when I run "npm test" I get the tests hanging and control doesn't get back to the command line.我遇到的问题是:在程序的当前阶段,当我运行“npm test”时,测试挂起并且控制没有回到命令行。 I have noticed that when I comment out all the lines in server.js, api.js and employees.js the tests run smoothly.
我注意到,当我注释掉 server.js、api.js 和 employees.js 中的所有行时,测试运行顺利。
Plus, sometimes only a part of the tests is executed, like 1/3 of them.另外,有时只执行一部分测试,比如其中的 1/3。
Any idea why are the tests hanging, and how to fix them?知道为什么测试会挂起,以及如何修复它们吗?
Here is the package.json file plus the main files of the project so far.这里是 package.json 文件以及到目前为止项目的主要文件。
Thanks in advance.提前致谢。
package.json package.json
{
"name": "expresso",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"chai": "^4.1.2",
"cors": "^2.8.5",
"errorhandler": "^1.5.1",
"express": "^4.17.1",
"mocha": "^6.1.4",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-router-dom": "^4.2.2",
"sqlite3": "^4.0.6",
"supertest": "^3.0.0",
"whatwg-fetch": "^2.0.3"
},
"devDependencies": {
"babel": "^6.23.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"webpack": "^3.5.5"
}
}
server.js服务器.js
const bodyParser = require('body-parser');
const cors = require('cors');
const errorHandler = require('errorhandler');
const express = require('express');
const apiRouter = require('./api/api');
const app = express();
const PORT = process.env.PORT || 4000;
app.use(bodyParser.json());
app.use(cors());
app.use('/api', apiRouter);
app.use(errorHandler());
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
module.exports = app;
api.js api.js
const express = require('express');
const apiRouter = express.Router();
const employeesRouter = require('./employees');
// <<< api/employees >>>
apiRouter.use('/employees', employeesRouter);
module.exports = apiRouter;
employees.js员工.js
const express = require('express');
const employeesRouter = express.Router();
const sqlite = require('sqlite3');
const db = new sqlite.Database('./database.sqlite');
// <<< api/employees >>>
employeesRouter.get('/', (req, res, next) => {
const sql = 'SELECT * FROM Employee WHERE is_current_employee = 1';
db.all(sql, (error, employees) => {
if (error) {
next(error);
} else {
res.status(200).json({ employees: employees });
}
});
});
module.exports = employeesRouter;
You have to add the tag --exit
in scripts -> test
into your package.json
.您必须在脚本中添加标签
--exit
scripts -> test
到您的package.json
中。
The result should be something like:结果应该是这样的:
"scripts": {
"test": "mocha --exit"
}
--exit: Force Mocha to quit after tests complete
--exit:测试完成后强制 Mocha 退出
After that, when tests finish, command line is available again.之后,当测试完成时,命令行再次可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.