-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
E2E tests fail when using exception filter extended from BaseExceptionFilter #1160
Comments
I have just encountered this issue. I believe it may have something to do with how
I don't know what to make of it, but posting just in case it helps anyone. |
I worked around the problem by using import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
app = await NestFactory.create(AppModule, {
logger: false,
});
await app.init();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(400)
.expect(/User not found/);
});
}); Although, I think you lose the benefit of being able to substitute modules. |
Should be fixed in 5.4.1, just ensure to entirely remove this: constructor(@Inject(HTTP_SERVER_REF) appRef: HttpServer) {
super(appRef); // appRef is {}
} |
@kamilmysliwiec Confirmed, removing the constructor fixed the problem. |
I'm not sure why, but in v5.7.3 I'm trying: `
and What am I missing? |
@nelsonec87 the exception filter has access to the |
That was it. |
Hi! I've started getting this error for some reason on all my E2E tests with Nest 6. It used to work until 2-3 weeks ago. I did not change anything... This is how my code looks like. describe('E2E - authorization flows', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
}, 15000);
afterAll(async () => {
await app.close();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
}); |
But how should one proceed if they want to fallback to the base exception filter inside a global application filter on certain conditions? |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm submitting a...
Current behavior
Setting up Exception Filter by extending from BaseExceptionFilter will fail E2E tests setup using a testing module.
I've been trying to set up an exception filter on the controller to map domain-level errors to their
HttpException
counterparts.I set up the exception filter as follows:
and introduced it into the controller:
This solution works on the actual server but fails during E2E tests with the follwing error:
Expected behavior
It should be possible to extend
BaseExceptionFilter
and test its functionality.Minimal reproduction of the problem with instructions
https://github.com/mpontus/nest-exception-filter-example
yarn install
yarn test:e2e
to observe the erroryarn start
and visithttp://localhost:3000
to observe correct behaviorWhat is the motivation / use case for changing the behavior?
My initial thought when coming up with this solution, was to rethrow errors from the custom exception filter. Similar approach was tested by @quezak in this issue, but the error shown there still persists.
My understandign from the linked PR is that subclassing
BaseExceptionFilter
is the recommended solution for this problem. If I made a mistake somewhere in my implementation, I would appreciate the correction.Otherwise, I think it would be important to ensure that error handling, achieved through the use of exception filter, can be expressed through the context of E2E tests.
Environment
The text was updated successfully, but these errors were encountered: