Jest 在 nestjs 中找不到路线

Jest doesn't find the route in nestjs

我是 nest 和 jest 的新手。 我正在尝试为每个 e2e 测试创建一个数据库。 第一条路线是正确的,第二条路线 /api/v1/auth/email/register 是 404(它适用于我的代码)

import { Test } from '@nestjs/testing'
import * as request from 'supertest'
import { AppModule } from './../../src/app.module'
import { Connection } from 'mongoose';
import {
  TESTER_EMAIL,
  TESTER_PASSWORD,
  MAIL_HOST,
  MAIL_PORT,
} from '../utils/constants';
import { getConnectionToken, MongooseModule } from '@nestjs/mongoose';
import { NestExpressApplication } from '@nestjs/platform-express';
import { UsersModule } from '../../src/users/users.module';
import supertest = require('supertest');
import { AuthModule } from '../../src/auth/auth.module';

describe('Authentication (e2e)', () => {
  let app: NestExpressApplication;
  const mail = `http://${MAIL_HOST}:${MAIL_PORT}`;
  const newUserName = `Tester${Date.now()}`;
  const newUsername = `E2E.${Date.now()}`;
  const newUserEmail = `User.${Date.now()}@example.com`;
  const newUserPassword = `secret`;
  const apiClient = () => {
    return supertest(app.getHttpServer());
  };

  beforeAll(async() => {
    const moduleRef = await Test.createTestingModule({
      imports: [
        MongooseModule.forRoot('mongodb://127.0.0.1:27017', { dbName: 'test' }), // we use Mongoose here, but you can also use TypeORM
        AuthModule,
        UsersModule,
        AppModule,

      ],
    }).compile();

    app = moduleRef.createNestApplication<NestExpressApplication>();
    await app.listen(3001);
  })

  beforeEach(async () => {

  })

  afterAll(async () => {
    await (app.get(getConnectionToken()) as Connection).db.dropDatabase();
    await app.close();
  });


  it('/ (GET)', () => {
    return request(app.getHttpServer())
      .get('/')
      .expect(200)
      .expect('{"message":"This is a simple example of item returned by your APIs."}')
  })


  it('Register a default user: /api/v1/auth/email/register (POST)', async () => {
    return request(app.getHttpServer())
      .post('/api/v1/auth/email/register')
      .send({
        "name": newUserName,
        "username": newUsername,
        "email": TESTER_EMAIL,
        "password" : TESTER_PASSWORD
      })
      .expect(201);
  });


})

我导入了我所有的模块,我确定路由 POST 'http://127.0.0.1:3001/api/v1/auth/email/register' 存在

您遇到此问题的原因是 API 版本控制。

那些 versioning-related 的东西在 bootstrap 文件中有描述,如果你想在这里有完全相同的效果,那么你也必须在 E2E 测试中添加这些选项。

因此,在 app 对象中附加版本控制选项

beforeAll(async() => {
  const moduleRef = await Test.createTestingModule({
    imports: [
      MongooseModule.forRoot('mongodb://127.0.0.1:27017', { dbName: 'test' }),
      AuthModule,
      UsersModule,
      AppModule,
    ],
  }).compile();

  app = moduleRef.createNestApplication<NestExpressApplication>();
  app.setGlobalPrefix('/api');
  app.enableVersioning({
    type: VersioningType.URI,
    defaultVersion: '1',
  });
  await app.listen(3001);
});

或者

而不是 '/api/v1/auth/email/register' 只需使用 '/auth/email/register'。删除 E2E 测试请求中所有地方的 /api/v1 前缀,它将照原样工作。