Skip to content

Commit

Permalink
test(e2e): Add nestjs e2e test documenting errors not being properly …
Browse files Browse the repository at this point in the history
…caught in submodules (#12868)
  • Loading branch information
nicohrubec authored Jul 11, 2024
1 parent 00fabe5 commit 0e6d802
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { AppController1, AppController2 } from './app.controller';
import { AppService1, AppService2 } from './app.service';
import { TestModule } from './test-module/test.module';

@Module({
imports: [ScheduleModule.forRoot()],
imports: [ScheduleModule.forRoot(), TestModule],
controllers: [AppController1],
providers: [AppService1],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { TestException } from './test.exception';

@Controller('test-module')
export class TestController {
constructor() {}

@Get()
getTest(): string {
throw new TestException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class TestException extends Error {
constructor() {
super('Something went wrong in the test module!');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import { TestException } from './test.exception';

@Catch(TestException)
export class TestExceptionFilter extends BaseExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
if (exception instanceof TestException) {
return super.catch(new BadRequestException(exception.message), host);
}
return super.catch(exception, host);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { TestController } from './test.controller';
import { TestExceptionFilter } from './test.filter';

@Module({
imports: [],
controllers: [TestController],
providers: [
{
provide: APP_FILTER,
useClass: TestExceptionFilter,
},
],
})
export class TestModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,32 @@ test('Does not send expected exception to Sentry', async ({ baseURL }) => {

expect(errorEventOccurred).toBe(false);
});

test('Does not handle expected exception if exception is thrown in module', async ({ baseURL }) => {
const errorEventPromise = waitForError('nestjs', event => {
return !event.type && event.exception?.values?.[0]?.value === 'Something went wrong in the test module!';
});

const response = await fetch(`${baseURL}/test-module`);
expect(response.status).toBe(500); // should be 400

// should never arrive, but does because the exception is not handled properly
const errorEvent = await errorEventPromise;

expect(errorEvent.exception?.values).toHaveLength(1);
expect(errorEvent.exception?.values?.[0]?.value).toBe('Something went wrong in the test module!');

expect(errorEvent.request).toEqual({
method: 'GET',
cookies: {},
headers: expect.any(Object),
url: 'http://localhost:3030/test-module',
});

expect(errorEvent.transaction).toEqual('GET /test-module');

expect(errorEvent.contexts?.trace).toEqual({
trace_id: expect.any(String),
span_id: expect.any(String),
});
});

0 comments on commit 0e6d802

Please sign in to comment.