node.jstypescriptnestjsfastifynestjs-fastify

End-to-end testing NestJS with Fastify: "@nestjs/platform-express" package is missing" error


I have fresh NestJS application using Fastify. When trying to npm run test:e2e I got the following error:

[Nest] 14894   - 11/19/2021, 10:29:10 PM   [ExceptionHandler] The "@nestjs/platform-express" package is missing. Please, make sure to install this library ($ npm install @nestjs/platform-express) to take advantage of NestFactory.
  ●  process.exit called with "1"

      12 |     }).compile();
      13 | 
    > 14 |     app = moduleFixture.createNestApplication();
         |                         ^
      15 |     await app.init();
      16 |   });
      17 | 

      at Object.loadPackage (../node_modules/@nestjs/common/utils/load-package.util.js:13:17)
      at TestingModule.createHttpAdapter (../node_modules/@nestjs/testing/testing-module.js:25:56)
      at TestingModule.createNestApplication (../node_modules/@nestjs/testing/testing-module.js:13:43)
      at Object.<anonymous> (app.e2e-spec.ts:14:25)

 RUNS  test/app.e2e-spec.ts

Process finished with exit code 1

Seems odd, because why would platform-express be needed for fastify-based app?


Solution

  • Appears that after switching to Fastify we also need to update test/app.e2e-spec.ts accordingly:

    import { Test, TestingModule } from '@nestjs/testing';
    import * as request from 'supertest';
    import { AppModule } from '../src/app.module';
    import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
    
    describe('AppController (e2e)', () => {
      let app: NestFastifyApplication;
    
      beforeEach(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
          imports: [AppModule],
        }).compile();
    
        app = moduleFixture.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
        await app.init();
        await app.getHttpAdapter().getInstance().ready();
      });
    
      afterEach(async () => {
        await app.close();
      });
    
      it('/ (GET)', () => {
        return request(app.getHttpServer()).get('/').expect(200).expect('Hello World!');
      });
    });