摩卡不退出

Mocha not exiting

我 运行 遇到 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);
    });
});

使用 --exit 标志尝试 运行 测试。这将 "Force Mocha to quit after tests complete" ref

$ mocha --exit ./test.test.js

app.listen(3000) 的调用正在阻止进程退出。

导入 app 对象而不在 运行 测试时调用 app.listen(3000)

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)