简体   繁体   English

node.js 中的摩卡测试挂起或丢失

[英]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"
}

According to docs根据文档

--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.

相关问题 将代码覆盖率添加到WebStorm中的异步node.js mocha测试 - Adding code coverage to async node.js mocha tests in WebStorm Node.js Istanbul / Mocha单元测试未打印详细信息 - Node.js Istanbul/Mocha Unit Tests not printing details 如何在mocha测试中使用Node.js全局变量 - How to use Node.js global variable in mocha tests Istanbul和Mocha在我的Node.JS项目测试中给出“参考错误:未定义套件” - Istanbul and Mocha give 'Reference Error: suite is not defined' with my Node.JS project tests 导入用于在浏览器和Node.JS中用Mocha / Chai / TypeScript编写的测试的文件 - Importing files for tests written on Mocha/Chai/TypeScript both in browser and Node.JS 使用我的Node.js REST API设置Mocha / ChaiHttp单元测试时遇到问题 - Trouble Setting Up Mocha/ChaiHttp Unit Tests With My Node.js REST API 如何让Mocha在Node.js中的多个子文件夹中执行单元测试 - How to get Mocha to execute unit tests in multiple subfolders in Node.js Node.js + Chai / Mocha / Should:针对同一响应运行多个测试 - Node.js + Chai/Mocha/Should: running multiple tests against the same response Node.js不断在本地主机上挂起/加载 - Node.js keeps hanging/loading on localhost Coursera Node.js Fibonacci 实现挂起 - Coursera Node.js Fibonacci implementation hanging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM