Jest doesn't find the route in nestjs

Viewed 258

I'm new with nest and jest. I'm trying to create a database for each e2e test. the first route is correct, the second one /api/v1/auth/email/register is 404 (it works on my code)

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);
  });


})

I imported all my modules and I'm sure that the route POST 'http://127.0.0.1:3001/api/v1/auth/email/register' exists

1 Answers

The reason you are facing this issue is API versioning.

Those versioning-related things are described in the bootstrap file and if you want to have exactly the same effect here then you have to add those options in E2E tests as well.

So, attach versioning options in app object

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);
});

Or

Instead of '/api/v1/auth/email/register' just use '/auth/email/register'. Remove /api/v1 prefixes everywhere in E2E tests requests and it will work as it is.

Related