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.

INestApplication doesn't resolve close() as a property when running e2e tests

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Running npm run test:e2e throws the following error:

TypeError: Cannot read properties of undefined (reading ‘close’)

this prevents the app from closing. Leaving multiple ports open when running tests, and causing subsequent tests to fail when ran on the same port.

speficially, it cannot read the property close() property from the ‘INestApplication’ interface from @nestjs/testing. This is used to define app.close();

import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { PrismaService } from '../src/prisma/prisma.service';
import * as pactum from 'pactum';
import { AuthDto } from '../src/auth/dto';

describe('App e2e', () => {
  let app: INestApplication;
  let prisma: PrismaService;
  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
        imports: [AppModule],
    }).compile();
    const app = moduleRef.createNestApplication();
    app.useGlobalPipes(
        new ValidationPipe({
          whitelist: true,
        }),
    );
    const port = 3060;
    await app.init();
    await app.listen(port);
    prisma = app.get(PrismaService)
      await prisma.cleanDb();
    pactum.request.setBaseUrl(`http://localhost:${port}`)
  });

  afterAll(() => {
      app.close();
  });

result: image

Minimum reproduction code

https://github.com/nakajimayoshi/auth-api

Steps to reproduce

  1. Download the repo and open in an IDE

  2. Setup env variables

  3. Run the following command:

test coverage

$ npm run test:e2e

Expected behavior

Unit test should pass

Package

Other package

No response

NestJS version

9.0.0

Packages versions

 "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/jwt": "^9.0.0",
    "@nestjs/passport": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@prisma/client": "^4.1.0",
    "argon2": "^0.28.7",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "dotenv-cli": "^6.0.0",
    "passport": "^0.6.0",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "prisma": "^4.1.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },

Node.js version

16.13.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcdo29commented, Jul 25, 2022

This is most likely from never closing the PrimsaClient. You should create an OnApplicationShutdown hook that closes the connection to ensure the server can properly shutdown.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The test with JestJS that closes the application does not work
let app: INestApplication; is upper scope one which you are actually using, since it does not have any value assigned it is undefined...
Read more >
Unable to run tests because Nest can't resolve dependencies ...
Unable to run tests because Nest can't resolve dependencies of a service ... (e2e)', () => { let app: INestApplication; const mockUser =...
Read more >
How To Write End-to-End Tests in Node.js Using Puppeteer ...
In this tutorial, you will write an e2e test that validates that the account creation and login features of a sample webpage work...
Read more >
Unit testing a NestJS App — In shortest steps. | by nish abe
Let's see how we can add unit tests and end-to-end tests to a NestJS ... Go to your folder and run the below...
Read more >
How to write end-to-end tests with Cypress and Node.js
Setting up Cypress. To get started, we'll create a new project and set up Cypress. Initialize a new project by running the following...
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