目前我正在做一个项目,我应该在 node.js 中使用 express.js 编写路由。测试和项目的其他部分不是我写的,因此我认为它们没有错误。我遇到的问题是:在程序的当前阶段,当我运行“npm test”时,我的测试挂起并且控制不会返回到命令行。我注意到当我注释掉 server.js、api.js 和 employees.js 中的所有行时,测试运行顺利。另外,有时只执行一部分测试,比如 1/3。知道为什么测试挂起,以及如何修复它们吗?这是 package.json 文件加上目前项目的主要文件。提前致谢。包.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" }}服务器.jsconst 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.jsconst express = require('express');const apiRouter = express.Router();const employeesRouter = require('./employees'); // <<< api/employees >>>apiRouter.use('/employees', employeesRouter);module.exports = apiRouter;
1 回答
ibeautiful
TA贡献1993条经验 获得超5个赞
您必须将标签添加--exit
到scripts -> test
您的package.json
.
结果应该是这样的:
"scripts": { "test": "mocha --exit" }
根据文档
--exit : 测试完成后强制 Mocha 退出
之后,当测试完成时,命令行再次可用。
添加回答
举报
0/150
提交
取消