我遇到了Mocha无法退出的问题。
我读到这可能是因为我有悬而未决的资源,但我不确定在哪里。
我的代码是:
import express from 'express';
let app = express();
app.get('/', (req, res) => {
res.end('Done');
});
app.listen(3000);
export default app;我的测试是:
import { describe, it } from 'mocha';
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import app from '../app';
chai.use(chaiHttp);
describe('Simple test', () => {
it('Should', async () => {
let response = await chai.request(app).get('/');
expect(response).to.have.status(200);
});
});发布于 2020-02-14 21:53:22
对app.listen(3000)的调用阻止了进程退出。
运行测试时,在不调用app.listen(3000)的情况下导入app对象。
app.js
import express from 'express';
let app = express();
app.get('/', (req, res) => {
res.end('Done');
});
export default app;test.js
import chaiHttp from 'chai-http';
import { describe, it } from 'mocha';
import app from './app';
chai.use(chaiHttp);
describe('Simple test', () => {
it('Should', async () => {
let response = await chai.request(app).get('/');
chai.expect(response).to.have.status(200);
});
});在另一个模块中,导入app并启动它,侦听以正常运行您的服务器。
main.js
import app from './app'
app.listen(3000)发布于 2020-02-14 21:40:33
尝试使用--exit标志运行测试。这将“强制Mocha在测试完成后退出”ref
$ mocha --exit ./test.test.js
https://stackoverflow.com/questions/60227453
复制相似问题