RouterModule mapped paths are lost in e2e tests when closing the app after each test
See original GitHub issueBug 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:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Fixed in this commit https://github.com/nestjs/nest/commit/3657d5fb34cb373aad2d9b887ff13582ff476f4f Check out v8.0.6
@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
andnest-router
you won’t find too many similarities.@jmcdo29 looking into this rn!