摩卡咖啡不退出

时间:2020-02-14 13:38:22

标签: javascript mocha

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

2 个答案:

答案 0 :(得分:3)

尝试使用--exit标志运行测试。这将“强制摩卡在测试完成后退出” ref

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

答案 1 :(得分:1)

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)