question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

e2e testing not working with fastify V3

See original GitHub issue

Bug Report

Original issue here https://github.com/fastify/help/issues/228. While the workaround in that thread works, it leaves open handles and has to hardcode port number. Also, they seem to conclude that something in app.init has to be changed.

Current behavior

    TypeError: Cannot read property 'length' of undefined

      at next (../node_modules/fastify/lib/route.js:374:32)
      at preParsingHookRunner (../node_modules/fastify/lib/route.js:405:3)
      at runPreParsing (../node_modules/fastify/lib/route.js:356:5)
      at Object.routeHandler [as handler] (../node_modules/fastify/lib/route.js:334:7)
      at Router.lookup (../node_modules/find-my-way/index.js:356:14)

Input Code

main.ts

import { NestFactory } from '@nestjs/core';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );
  await app.listen(3000);
}
bootstrap();

app.e2e-spec.ts

import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { FastifyAdapter } from '@nestjs/platform-fastify';

describe('AppController (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication(new FastifyAdapter());
    await app.init();
  });

  it('/ (GET)', () => {
    return request(app.getHttpServer())
      .get('/')
      .expect(200)
      .expect('Hello World!');
  });
});

Expected behavior

Test pass. (It works as expected if using fastify V2 and @nestjs/platform-fastify@7.2)

Possible Solution

Environment


Nest version: X.Y.Z

 
For Tooling issues:
- Node version: 7.4.2
- Platform:   Ubuntu

Others:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcdo29commented, Jan 6, 2021

Use FastifyNestApplication instead of INestApplication. You can cast this type as necessary. You can see an example here

0reactions
sramteare-chwycommented, Jan 6, 2021

@Summon528 Please share an example here. I have similar problem, using fastify.inject is not recognized on INestApplication Property 'inject' does not exist on type 'INestApplication'.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NestJS + Fastify e2e testing: Jest did not exit one second after ...
While running end-to-end tests of NestJS+Fastify application I noticed the following warnings: Jest did not exit one second after the test ...
Read more >
Testing - Fastify
Testing is one of the most important parts of developing an application. Fastify is very flexible when it comes to testing and is...
Read more >
Testing a Fastify CRUD application using mongodb
Learn how to implement tests on a nodejs application within fastify and tap. Read from the local db and assert all your conditions....
Read more >
Test your Fastify REST API With Node Tap - YouTube
In this video, we test a Fastify REST API with Node Tap. You will learn how to mock imports and test against a...
Read more >
Testing | NestJS - A progressive Node.js framework
Nest: automatically scaffolds default unit tests for components and e2e tests for applications; provides default tooling (such as a test runner that builds...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found