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.

RouterModule mapped paths are lost in e2e tests when closing the app after each test

See original GitHub issue

Bug Report

Current behavior

During e2e testing I call await app.close() after each test run. Before each test I create a new application with the test module. This all works fine for the first test but after await app.close(); has been called the path mapping is lost and RouterModule.register is not being called again (debugged it) and thus all test except for the first one result in a 404 error code.

Input Code

All requests result in 404 except for the first test


import { Module } from '@nestjs/common';
import { RootControllerModule } from './controller/root-controller.module';
import { RouterModule } from 'nest-router';
import { ParentControllerModule } from './controller/parent-controllers.module';

@Module({
	imports: [
		RouterModule.register([
			{
				path: 'api',
				module: RootControllerModule,
				children: [
					{
						path: 'parent_controller_a',
						module: ParentControllerModule, // which has the controller with uri "child_controller_a"
					},
				],
			},
		]),
		ParentControllerModule,
		RootControllerModule,
	],
	controllers: [],
	providers: [],
})
export class AppModule {}


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

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

	afterEach(async () => {
		await app.close();
	});

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

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

        // works
	it('test a', () => {
		return request(app.getHttpServer()).post('/api/parent_controller_a/child_controller_a').expect(400);
	});

        // fails with 404 not found
	it('test b', () => {
		return request(app.getHttpServer()).post('/api/parent_controller_a/child_controller_a').expect(400);
	});
});

Expected behavior

After some debugging It seems that RoutingModule.register is not being called again. I would expect that the paths are registered again with each new test module and app setup. The nest-router from nestjsx seems to do this and works as expected.

Possible Solution

I suppose this will be fixed when nest-router from nestjsx is merged ? For the mean time nest-router can be used or the app has to be reused across all tests.

Environment


Nest version: 8.0.5


    "@nestjs/common": "8.0.5",
    "@nestjs/core": "8.0.5",
    "@nestjs/platform-express": "8.0.5",



 
For Tooling issues:
- Node version: 4.16.1
- Platform:  Mac

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
kamilmysliwieccommented, Aug 5, 2021
2reactions
kamilmysliwieccommented, Aug 5, 2021

Regarding the nest-router library, im confused because according to the creator of that lib the RouterModule in nestjs core is the same and has already been merged:

@elovin I’ve rewritten most of the underlying code (we’re using different path concatenation techniques now so the final behavior may be different), although the public API remains the same. If you compare Nest’s RouterModule and nest-router you won’t find too many similarities.

@jmcdo29 looking into this rn!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Router tutorial: tour of heroes - Angular
Provides a module to replace or remove when testing the application; Provides a well-known location for routing service providers such as guards and...
Read more >
NestJS + Fastify e2e testing: Jest did not exit one second after ...
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to ...
Read more >
Testing | NestJS - A progressive Node.js framework
Nest strives to promote development best practices, including effective testing, so it includes features such as the following to help developers and teams ......
Read more >
Testing Routing • Angular - codecraft.tv
We can test routing in Angular by using RouterTestingModule instead of RouterModule to provide our routes. This uses a spy implementation of Location...
Read more >
What is End To End Testing? - BrowserStack
To build test cases for E2E Testing, keep the following in mind: Create multiple test cases to test every functionality of user functions....
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