我目前正在制作带有打字稿,节点,表达和测试的API,并带有jest和supertest。使用Javascript时没有问题,但是最近我将项目文件从JS更改为TS,包括测试文件,并且当我开始测试时,我在超级测试请求部分的所有测试套件中都收到以下错误,这是其中之一当我开始测试时,我在终端上的测试套件。
TypeError: app.address is not a function
37 | it("should return 400 if email is invalid", async () => {
38 | const res = await request(server)
> 39 | .post("/api/users/auth/register")
| ^
40 | .send({
41 | email: "nomail",
42 | password: "validpassword123",
这是我的测试文件 auth.test.ts :
import * as request from 'supertest';
import { User } from '../../../../src/models/User';
import * as mongoose from 'mongoose';
import getKeys from '../../../../src/config/keys';
describe("/api/users/auth", () => {
let server;
let accessToken = "Bearer accessToken";
let email;
let password;
beforeAll(async () => {
server = import('../../../../src/index')
await mongoose.connect(getKeys().mongoURI);
})
afterAll(async () => {
await server.close();
await mongoose.disconnect();
})
it("should return 400 if email is invalid", async () => {
const res = await request(server)
.post("/api/users/auth/register")
.send({
email: "nomail",
password: "validpassword123",
name: "name"
});
expect(res.status).toBe(400);
expect(res.body).toHaveProperty('errArray')
});
}
这是我的src / index.ts文件,它是入口点。
import * as express from 'express';
import * as passport from 'passport';
import * as bodyParser from 'body-parser';
import * as path from 'path';
import * as session from 'express-session';
import getKeys from './config/keys';
const port = 3001 || process.env.PORT;
const server = app.listen(port, () =>
console.log(`Server running on port ${port}`)
);
export default server;
我尝试将导出和导入服务器语法更改为所有commonjs语法,并安装和设置与此相关的所有依赖项,包括@ types / supertest,@ types / jest,ts-jest,这是我的设置 jest.config.js
module.exports = {
verbose: true,
testURL: 'http://localhost',
testEnvironment: "node",
roots: [
"<rootDir>"
],
transform: {
"^.+\\.tsx?$": "ts-jest"
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
globals: {
"ts-jest": {
"tsConfigFile": "tsconfig.json"
}
},
moduleFileExtensions: [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
}
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist",
"moduleResolution": "node",
"sourceMap": true,
"module": "commonjs",
"allowJs": true,
"target": "es5",
"noUnusedParameters": false,
"allowUnreachableCode": true,
"allowUnusedLabels": true,
"types": [
"jest",
"node",
"express",
"mongoose",
"body-parser",
"supertest"
],
"lib": [
"es2015"
]
},
"include": [
"./src/**/*",
"index.ts"
],
"exclude": [
"./node_modules",
"**/*.spec.ts",
"**/*.test.ts"
]
}
package.json文件的脚本和开发依赖性
"scripts": {
"test": "jest --watchAll --runInBand",
"coverage": "jest --coverage",
"start": "ts-node src/index.ts",
"server": "./node_modules/nodemon/bin/nodemon.js",
"client": "npm start --prefix ../client",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
"devDependencies": {
"@types/body-parser": "^1.17.0",
"@types/express": "^4.16.0",
"@types/jest": "^23.3.12",
"@types/mongoose": "^5.3.7",
"@types/node": "^10.12.18",
"@types/supertest": "^2.0.7",
"jest": "^23.6.0",
"nodemon": "^1.18.9",
"supertest": "^3.3.0",
"ts-jest": "^23.10.5",
"ts-node": "^7.0.1",
"typescript": "^3.2.2"
}
答案 0 :(得分:0)
原因是您传递到server
的{{1}}是supertest
。 undefined
将在内部使用supertest
,请参阅此line。这就是为什么它会引发错误:
TypeError:app.address不是函数
如果要动态导入app.address()
,则应更改:
server
收件人:
let server;
beforeAll(async () => {
server = import('../../../../src/index')
})
例如
let server;
beforeAll(async () => {
const mod = await import('../../../../src/index');
server = (mod as any).default;
});
:
index.ts
import express from 'express';
const app = express();
app.post('/api/users/auth/register', (req, res) => {
res.status(400).json({ errArray: [] });
});
const port = 3001 || process.env.PORT;
const server = app.listen(port, () => console.log(`Server running on port ${port}`));
export default server;
:
index.test.ts
具有覆盖率报告的集成测试结果:
import request from 'supertest';
describe('/api/users/auth', () => {
let server;
beforeAll(async () => {
const mod = await import('./');
server = (mod as any).default;
});
afterAll((done) => {
if (server) {
server.close(done);
}
});
it('should return 400 if email is invalid', async () => {
const res = await request(server)
.post('/api/users/auth/register')
.send({
email: 'nomail',
password: 'validpassword123',
name: 'name',
});
expect(res.status).toBe(400);
expect(res.body).toHaveProperty('errArray');
});
});
源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/54230886