【发布时间】:2021-09-10 08:23:26
【问题描述】:
我正在后端编写一个测试来测试我的 API 的简单路由。我正在使用 Typescript 和 Jest 编写测试,这一切都发生在一个 docker 容器内,我首先使用 docker-compose。
我有一个用于我的测试的辅助类,它通过 init 函数创建一个快速网络服务器的实例和一个数据库连接。在关闭功能中,它关闭连接等。 所以我的测试看起来像这样:
import { Helper } from "./helper";
import request from 'supertest'
describe('article', () => {
const helper = new Helper();
beforeAll(async () => {
await helper.init();
});
afterAll(async () => {
await helper.shutdown();
});
it('should test if reaching the api is possible', async (done) => {
request(helper.app)
.get('/test')
.send()
.set('Accept', 'application/json')
.expect(200)
.end( (error, response) => {
if(error) throw error;
expect(response.body.message).toBe("Hello");
});
});
为了执行测试,我使用这个脚本"test": "jest --verbose --forceExit --runInBand --detectOpenHandles"
如果我这样执行测试,我会得到以下输出:
我按照建议更改了超时,但没有任何区别。 现在出于测试目的,我让我的路线返回“Helloo”而不是预期的“Hello”。 这是我收到的输出:
所以它显然得到了回应。我的问题是,为什么它要运行两次测试,为什么在第二次测试时总是超时?
这是我的jest.config.js
module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"js"
],
}
【问题讨论】:
标签: typescript testing jestjs ts-jest