Mocha不退出

问题描述 投票:0回答:2

我遇到了摩卡无法退出的问题,我不确定为什么。

我听说这可能是因为我有很多资源,但是我不确定在哪里。

我的代码是:

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);
    });
});
```

I must be missing something stupid, but I can't see it.
javascript mocha
2个回答
1
投票

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

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


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)
© www.soinside.com 2019 - 2024. All rights reserved.