Problem
Exception filters does not seem to be used at all when doing E2E testing on a gateway
Code
some-gateway.e2e-spec.ts
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AppModule } from '@/app.module';
import { SomeGateway } from './some.gateway';
describe('SomeGateway', () => {
let app: INestApplication;
let someGateway: SomeGateway;
const mockClient = {
emit: jest.fn(),
};
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
someGateway = app.get(SomeGateway);
});
afterAll(async () => {
await app.close();
});
beforeEach(() => {
jest.resetAllMocks();
});
describe('someEventListener', () => {
it('does not throw an exception because I have a exception filter', async (done) => {
await someGateway.someEventListener(
mockClient
);
expect(mockClient.emit).toHaveBeenCalled();
done();
});
});
});
some-gateway.ts
import { SomeExceptionFilter } from './some-exception-filter';
import { UseFilters } from '@nestjs/common';
import {
SubscribeMessage,
WebSocketGateway,
} from '@nestjs/websockets';
@UseFilters(new SomeExceptionFilter())
@WebSocketGateway({ namespace: 'robot-fleetbridge' })
export class SomeGateway {
@SubscribeMessage('some-event')
async someEventListener(client): Promise<void> {
throw new Error();
}
}
some-exception-filter.ts
import { ArgumentsHost, Catch } from '@nestjs/common';
@Catch()
export class SomeExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
console.log('i should be called :(');
const client = host.switchToWs().getClient();
client.emit('exception', { message: 'not ok' });
}
}
Some thoughts
maybe I should use app.getHttpServer()
to be able to make it work, but how to simulate a socket.io connection using supertest
?
Aucun commentaire:
Enregistrer un commentaire