使用Express Server进行超级测试后,Mocha不会停止

时间:2018-10-29 14:49:39

标签: mocha supertest

我使用像这样的摩卡咖啡进行了一个简单的超级测试...

describe("test", () =>{
  it("Test 1", (done) =>{
    let app = (new App()).express;
    supertest(app).get("/").expect(200, done);
  })
})

测试运行并通过,但从未关闭摩卡。我试过了...

describe("test", () =>{
  it("Test 1", (done) =>{
    let app = (new App()).express;
    supertest(app).get("/").expect(200, ()=>{
      app.close();
      done();
    });
  })
})

但是未声明app.close,并且我没有汇总整个服务器。测试后如何停止摩卡咖啡?

2 个答案:

答案 0 :(得分:0)

这是一个最小的工作示例:

app.js

const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.sendStatus(200);
});

const port = 3000;
const server = app.listen(port, () => {
  console.info(`HTTP server is listening on http://localhost:${port}`);
});

module.exports = server;

app.test.js

const app = require("./app");
const supertest = require("supertest");

describe("test", () => {
  after((done) => {
    app.close(done);
  });

  it("Test 1", (done) => {
    supertest(app)
      .get("/")
      .expect(200, done);
  });
});

具有覆盖率报告的集成测试结果:

HTTP server is listening on http://localhost:3000
  test
    ✓ Test 1


  1 passing (23ms)

-------------|----------|----------|----------|----------|-------------------|
File         |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-------------|----------|----------|----------|----------|-------------------|
All files    |      100 |      100 |      100 |      100 |                   |
 app.js      |      100 |      100 |      100 |      100 |                   |
 app.test.js |      100 |      100 |      100 |      100 |                   |
-------------|----------|----------|----------|----------|-------------------|

启动HTTP服务器:

☁  mocha-chai-sinon-codelab [master] ⚡  node /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/src/stackoverflow/53048031/app.js                 
HTTP server is listening on http://localhost:3000

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/53048031

答案 1 :(得分:0)

使用

$ mocha --exit

或添加

"exit": true

在 .mocharc 中

来源:github link

相关问题