【发布时间】:2021-11-13 04:10:47
【问题描述】:
我正在尝试使用jest 和supertest 测试快速应用的端点。这是我的文件:
// backend/tests/integration.test.ts
const server = require('../server');
const supertest = require('supertest');
const appTest = supertest(server);
describe('Integration tests', () => {
it('GET /api/ping', async () => {
const expected = { status: 200, body: { success: true } }
const res = await appTest.get('/api/ping');
expect(res.status).toEqual(expected.status);
expect(res.body).toEqual(expected.body);
});
});
// backend/server.ts
import app from './src/app';
const PORT = 3000;
const server = app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
export default server;
// backend/src/app.ts
const express = require('express');
const app = express();
app.get('/api/ping', (req, res) => {
res.status(200).json({
'success': true
});
});
export default app;
我不断收到错误:
TypeError: app.address is not a function
并且堆栈跟踪说错误就行了:
8 | it('GET /api/ping', async () => {
9 | const expected = { status: 200, body: { success: true } }
> 10 | const res = await appTest.get('/api/ping');
| ^
11 | expect(res.status).toEqual(expected.status);
12 | expect(res.body).toEqual(expected.body);
13 | });
我的配置如下:
// package.json
...
"scripts": {
"test": "jest"
}
// jest.config.js
module.exports = {
preset: 'ts-jest',
modulePathIgnorePatterns: ["<rootDir>/dist/"],
transform: {
'^.+\/*.(ts|tsx)?$': 'ts-jest'
}
};
// babel.config.js
module.exports = {
presets: ['@babel/preset-env']
};
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist"
},
"lib": ["es2015"],
"exclude": [
"../node_modules"
],
"include": [
"./**/*.ts"
]
}
我不知道为什么会这样。已尝试谷歌搜索和阅读有关堆栈溢出超时错误的信息,但无济于事。
【问题讨论】:
标签: node.js typescript express jestjs ts-jest