Skip to content

Commit 0e6d802

Browse files
authored
test(e2e): Add nestjs e2e test documenting errors not being properly caught in submodules (#12868)
1 parent 00fabe5 commit 0e6d802

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

dev-packages/e2e-tests/test-applications/nestjs/src/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
22
import { ScheduleModule } from '@nestjs/schedule';
33
import { AppController1, AppController2 } from './app.controller';
44
import { AppService1, AppService2 } from './app.service';
5+
import { TestModule } from './test-module/test.module';
56

67
@Module({
7-
imports: [ScheduleModule.forRoot()],
8+
imports: [ScheduleModule.forRoot(), TestModule],
89
controllers: [AppController1],
910
providers: [AppService1],
1011
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import { TestException } from './test.exception';
3+
4+
@Controller('test-module')
5+
export class TestController {
6+
constructor() {}
7+
8+
@Get()
9+
getTest(): string {
10+
throw new TestException();
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class TestException extends Error {
2+
constructor() {
3+
super('Something went wrong in the test module!');
4+
}
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common';
2+
import { BaseExceptionFilter } from '@nestjs/core';
3+
import { TestException } from './test.exception';
4+
5+
@Catch(TestException)
6+
export class TestExceptionFilter extends BaseExceptionFilter {
7+
catch(exception: unknown, host: ArgumentsHost) {
8+
if (exception instanceof TestException) {
9+
return super.catch(new BadRequestException(exception.message), host);
10+
}
11+
return super.catch(exception, host);
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Module } from '@nestjs/common';
2+
import { APP_FILTER } from '@nestjs/core';
3+
import { TestController } from './test.controller';
4+
import { TestExceptionFilter } from './test.filter';
5+
6+
@Module({
7+
imports: [],
8+
controllers: [TestController],
9+
providers: [
10+
{
11+
provide: APP_FILTER,
12+
useClass: TestExceptionFilter,
13+
},
14+
],
15+
})
16+
export class TestModule {}

dev-packages/e2e-tests/test-applications/nestjs/tests/errors.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,32 @@ test('Does not send expected exception to Sentry', async ({ baseURL }) => {
5353

5454
expect(errorEventOccurred).toBe(false);
5555
});
56+
57+
test('Does not handle expected exception if exception is thrown in module', async ({ baseURL }) => {
58+
const errorEventPromise = waitForError('nestjs', event => {
59+
return !event.type && event.exception?.values?.[0]?.value === 'Something went wrong in the test module!';
60+
});
61+
62+
const response = await fetch(`${baseURL}/test-module`);
63+
expect(response.status).toBe(500); // should be 400
64+
65+
// should never arrive, but does because the exception is not handled properly
66+
const errorEvent = await errorEventPromise;
67+
68+
expect(errorEvent.exception?.values).toHaveLength(1);
69+
expect(errorEvent.exception?.values?.[0]?.value).toBe('Something went wrong in the test module!');
70+
71+
expect(errorEvent.request).toEqual({
72+
method: 'GET',
73+
cookies: {},
74+
headers: expect.any(Object),
75+
url: 'http://localhost:3030/test-module',
76+
});
77+
78+
expect(errorEvent.transaction).toEqual('GET /test-module');
79+
80+
expect(errorEvent.contexts?.trace).toEqual({
81+
trace_id: expect.any(String),
82+
span_id: expect.any(String),
83+
});
84+
});

0 commit comments

Comments
 (0)