【发布时间】:2019-09-16 00:44:05
【问题描述】:
我正在尝试编写一个简单的案例,我正在点击路线并检查响应。
我的测试有效,但每次它也会抛出这个消息:
Jest did not exit one second after the test run has completed.This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
这是我的单元测试:
// we will use supertest to test HTTP requests/responses
import request = require('supertest');
import bodyParser1 = require('body-parser');
// we also need our app for the correct routes!
const index = require('./index');
index.use(bodyParser1.json());
index.use(bodyParser1.urlencoded({ extended: true }));
describe('GET / ', () => {
test('Test sample get', async done => {
const response: any = await request(index).get('/');
expect(response.text).toEqual('Welcome to Minos');
expect(response.statusCode).toBe(200);
done();
});
});
afterAll(async done => {
done();
});
这是我的 index.ts:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
//res.setHeader('Content-Type', 'text/html');
res.send('Welcome to Minos');
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
module.exports = app;
【问题讨论】:
-
你找到答案了吗?因为我面临同样的问题。 Jest 版本是“24.8.0”。
-
@jan 不,还在等待:(
标签: node.js unit-testing express jestjs