INestApplication doesn't resolve close() as a property when running e2e tests
See original GitHub issueIs 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:
Minimum reproduction code
https://github.com/nakajimayoshi/auth-api
Steps to reproduce
-
Download the repo and open in an IDE
-
Setup env variables
-
Run the following command:
test coverage
$ npm run test:e2e
Expected behavior
Unit test should pass
Package
- I don’t know. Or some 3rd-party package
-
@nestjs/common
-
@nestjs/core
-
@nestjs/microservices
-
@nestjs/platform-express
-
@nestjs/platform-fastify
-
@nestjs/platform-socket.io
-
@nestjs/platform-ws
-
@nestjs/testing
-
@nestjs/websockets
- Other (see below)
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:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
https://docs.nestjs.com/fundamentals/lifecycle-events#lifecycle-events-1
This is most likely from never closing the
PrimsaClient
. You should create anOnApplicationShutdown
hook that closes the connection to ensure the server can properly shutdown.