我使用像这样的摩卡咖啡进行了一个简单的超级测试...
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,并且我没有汇总整个服务器。测试后如何停止摩卡咖啡?
答案 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)