使用 Fastify 进行端到端测试 NestJS:“@nestjs/platform-express”包丢失”错误

Dan*_*cal 8 node.js typescript nestjs fastify nestjs-fastify

我有使用 Fastify 的新 NestJS 应用程序。当尝试时,npm run test:e2e我收到以下错误:

\n
[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.\n  \xe2\x97\x8f  process.exit called with "1"\n\n      12 |     }).compile();\n      13 | \n    > 14 |     app = moduleFixture.createNestApplication();\n         |                         ^\n      15 |     await app.init();\n      16 |   });\n      17 | \n\n      at Object.loadPackage (../node_modules/@nestjs/common/utils/load-package.util.js:13:17)\n      at TestingModule.createHttpAdapter (../node_modules/@nestjs/testing/testing-module.js:25:56)\n      at TestingModule.createNestApplication (../node_modules/@nestjs/testing/testing-module.js:13:43)\n      at Object.<anonymous> (app.e2e-spec.ts:14:25)\n\n RUNS  test/app.e2e-spec.ts\n\nProcess finished with exit code 1\n\n
Run Code Online (Sandbox Code Playgroud)\n

看起来很奇怪,因为为什么基于 fastify 的应用程序需要 platform-express?

\n

Dan*_*cal 16

看来切换到 Fastify 后我们还需要进行test/app.e2e-spec.ts相应的更新:

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

Run Code Online (Sandbox Code Playgroud)