当我使用应用程序时,使用 supertest 和 Jest 测试 Express、MongoDB 和 TypeScript 失败
Testing Express, MongoDB & TypeScript with supertest and Jest fails when I use app
我想测试我的 Express,MongoDB,TypeScript REST-Api,但是我在 supertest 将 app
作为参数的代码行上遇到困难。测试失败,我得到 404 状态代码而不是 200。
const resVar = await supertest(app).get(`/`);
expect(resVar.statusCode).toBe(200);
然而,当我用 http://localhost:${EXPRESS_APP_PORT}/api/v1/users
替换参数 app 时,测试通过并且我得到 200 状态代码。
这是一个小项目,您可以获取并克隆此 Github repository 上的所有代码。
期待您的帮助。
server.ts
(服务器初始化location/file)⬇
import express, {Application} from 'express';
import cors from 'cors';
import {deserializeUserFunc} from './middleware/deserializeUser';
import {routes} from './routes/index';
const initServer = () => {
const app: Application = express();
app.use(cors());
app.use(express.json());
app.use(deserializeUserFunc);
app.use('/api/v1/users', routes);
return app;
};
export {initServer}
index.ts
(此处连接到 MonGODB 并监听端口,但不适用于测试环境。)⬇
import {initServer} from './server';
import {EXPRESS_APP_PORT, MONGO_DB_USERNAME, MONGO_DB_DATABASE} from './config';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();
const mongoDbPassword = process.env.MONGODB_PASSWORD;
import pinoLogger from './logger';
// connect to online mongoDB
mongoose.connect(`mongodb+srv://${MONGO_DB_USERNAME}:${mongoDbPassword}@emmethubclusterone.disfr.mongodb.net/${MONGO_DB_DATABASE}?retryWrites=true&w=majority` )
.then(() => {
pinoLogger.info(`Connected to ${MONGO_DB_DATABASE} database.`);
})
.catch((err) => {
pinoLogger.error(`${err} Error connecting to ${MONGO_DB_DATABASE} database!`);
process.exit(1);
})
const app = initServer();
app.listen(EXPRESS_APP_PORT, () => {
pinoLogger.info(`App server is listening at http://localhost:${EXPRESS_APP_PORT}`);
})
product.test.ts
(我的测试失败的文件。)⬇
import supertest from 'supertest';
import {initServer} from "../server" ;
import {MongoMemoryServer} from 'mongodb-memory-server';
import mongoose from 'mongoose';
const app = initServer();
describe('product', () => {
beforeAll( async() => {
const mongoServer = await MongoMemoryServer.create();
await mongoose.connect(mongoServer.getUri())
})
afterAll(async() => {
await mongoose.disconnect();
await mongoose.connection.close();
})
describe('get product route', () => {
describe('given the product exists', () => {
it('should return a 200 and the product', async() => {
const resVar = await supertest(app).get(`/`);
expect(resVar.statusCode).toBe(200);
})
});
})
});
可能是因为您在product.test.ts
中指定的路线。在 index.ts
中你声明 app.use('/api/v1/users', routes);
但在 product.test.ts
中你的目标是:
await supertest(app).get(`/`);
试试
await supertest(app).get(`/api/v1/users`);
我想测试我的 Express,MongoDB,TypeScript REST-Api,但是我在 supertest 将 app
作为参数的代码行上遇到困难。测试失败,我得到 404 状态代码而不是 200。
const resVar = await supertest(app).get(`/`);
expect(resVar.statusCode).toBe(200);
然而,当我用 http://localhost:${EXPRESS_APP_PORT}/api/v1/users
替换参数 app 时,测试通过并且我得到 200 状态代码。
这是一个小项目,您可以获取并克隆此 Github repository 上的所有代码。
期待您的帮助。
server.ts
(服务器初始化location/file)⬇
import express, {Application} from 'express';
import cors from 'cors';
import {deserializeUserFunc} from './middleware/deserializeUser';
import {routes} from './routes/index';
const initServer = () => {
const app: Application = express();
app.use(cors());
app.use(express.json());
app.use(deserializeUserFunc);
app.use('/api/v1/users', routes);
return app;
};
export {initServer}
index.ts
(此处连接到 MonGODB 并监听端口,但不适用于测试环境。)⬇
import {initServer} from './server';
import {EXPRESS_APP_PORT, MONGO_DB_USERNAME, MONGO_DB_DATABASE} from './config';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();
const mongoDbPassword = process.env.MONGODB_PASSWORD;
import pinoLogger from './logger';
// connect to online mongoDB
mongoose.connect(`mongodb+srv://${MONGO_DB_USERNAME}:${mongoDbPassword}@emmethubclusterone.disfr.mongodb.net/${MONGO_DB_DATABASE}?retryWrites=true&w=majority` )
.then(() => {
pinoLogger.info(`Connected to ${MONGO_DB_DATABASE} database.`);
})
.catch((err) => {
pinoLogger.error(`${err} Error connecting to ${MONGO_DB_DATABASE} database!`);
process.exit(1);
})
const app = initServer();
app.listen(EXPRESS_APP_PORT, () => {
pinoLogger.info(`App server is listening at http://localhost:${EXPRESS_APP_PORT}`);
})
product.test.ts
(我的测试失败的文件。)⬇
import supertest from 'supertest';
import {initServer} from "../server" ;
import {MongoMemoryServer} from 'mongodb-memory-server';
import mongoose from 'mongoose';
const app = initServer();
describe('product', () => {
beforeAll( async() => {
const mongoServer = await MongoMemoryServer.create();
await mongoose.connect(mongoServer.getUri())
})
afterAll(async() => {
await mongoose.disconnect();
await mongoose.connection.close();
})
describe('get product route', () => {
describe('given the product exists', () => {
it('should return a 200 and the product', async() => {
const resVar = await supertest(app).get(`/`);
expect(resVar.statusCode).toBe(200);
})
});
})
});
可能是因为您在product.test.ts
中指定的路线。在 index.ts
中你声明 app.use('/api/v1/users', routes);
但在 product.test.ts
中你的目标是:
await supertest(app).get(`/`);
试试
await supertest(app).get(`/api/v1/users`);