Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ jobs:
- test-application: 'nestjs-11'
build-command: 'test:build-latest'
label: 'nestjs-11 (latest)'
- test-application: 'nestjs-websockets'
build-command: 'test:build-latest'
label: 'nestjs-websockets (latest)'

steps:
- name: Check out current commit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "nestjs-websockets",
"version": "0.0.1",
"private": true,
"scripts": {
"build": "nest build",
"start": "nest start",
"test": "playwright test",
"test:build": "pnpm install && pnpm build",
"test:build-latest": "pnpm install && pnpm add @nestjs/common@latest @nestjs/core@latest @nestjs/platform-express@latest @nestjs/websockets@latest @nestjs/platform-socket.io@latest && pnpm add -D @nestjs/cli@latest && pnpm build",
"test:assert": "pnpm test"
},
"dependencies": {
"@nestjs/common": "^11.0.0",
"@nestjs/core": "^11.0.0",
"@nestjs/platform-express": "^11.0.0",
"@nestjs/websockets": "^11.0.0",
"@nestjs/platform-socket.io": "^11.0.0",
"@sentry/nestjs": "latest || *",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@playwright/test": "~1.56.0",
"@sentry-internal/test-utils": "link:../../../test-utils",
"@nestjs/cli": "^11.0.0",
"@types/node": "^18.19.1",
"socket.io-client": "^4.0.0",
"typescript": "~5.0.0"
},
"volta": {
"extends": "../../package.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getPlaywrightConfig } from '@sentry-internal/test-utils';

const config = getPlaywrightConfig({
startCommand: `pnpm start`,
});

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
@Get('/test-transaction')
testTransaction() {
return { message: 'ok' };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SubscribeMessage, WebSocketGateway, MessageBody } from '@nestjs/websockets';
import * as Sentry from '@sentry/nestjs';

@WebSocketGateway()
export class AppGateway {
@SubscribeMessage('test-exception')
handleTestException() {
throw new Error('This is an exception in a WebSocket handler');
}

@SubscribeMessage('test-manual-capture')
handleManualCapture() {
try {
throw new Error('Manually captured WebSocket error');
} catch (e) {
Sentry.captureException(e);
}
return { event: 'capture-response', data: { success: true } };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { SentryGlobalFilter, SentryModule } from '@sentry/nestjs/setup';
import { AppController } from './app.controller';
import { AppGateway } from './app.gateway';

@Module({
imports: [SentryModule.forRoot()],
controllers: [AppController],
providers: [
{
provide: APP_FILTER,
useClass: SentryGlobalFilter,
},
AppGateway,
],
})
export class AppModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Sentry from '@sentry/nestjs';

Sentry.init({
environment: 'qa',
dsn: process.env.E2E_TEST_DSN,
tunnel: `http://localhost:3031/`,
tracesSampleRate: 1,
transportOptions: {
bufferSize: 1000,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Import this first
import './instrument';

// Import other modules
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

const PORT = 3030;

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(PORT);
}

bootstrap();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { startEventProxyServer } from '@sentry-internal/test-utils';

startEventProxyServer({
port: 3031,
proxyServerName: 'nestjs-websockets',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { expect, test } from '@playwright/test';
import { waitForError } from '@sentry-internal/test-utils';
import { io } from 'socket.io-client';

test('Captures manually reported error in WebSocket gateway handler', async ({ baseURL }) => {
const errorPromise = waitForError('nestjs-websockets', event => {
return event.exception?.values?.[0]?.value === 'Manually captured WebSocket error';
});

const socket = io(baseURL!);
await new Promise<void>(resolve => socket.on('connect', resolve));

socket.emit('test-manual-capture', {});

const error = await errorPromise;

expect(error.exception?.values?.[0]).toMatchObject({
type: 'Error',
value: 'Manually captured WebSocket error',
});

socket.disconnect();
});

// There is no good mechanism to verify that an event was NOT sent to Sentry.
// The idea here is that we first send a message that triggers an exception which won't be auto-captured,
// and then send a message that triggers a manually captured error which will be sent to Sentry.
// If the manually captured error arrives, we can deduce that the first exception was not sent,
// because Socket.IO guarantees message ordering: https://socket.io/docs/v4/delivery-guarantees
test('Does not automatically capture exceptions in WebSocket gateway handler', async ({ baseURL }) => {
let errorEventOccurred = false;

waitForError('nestjs-websockets', event => {
if (!event.type && event.exception?.values?.[0]?.value === 'This is an exception in a WebSocket handler') {
errorEventOccurred = true;
}

return false;
});

const manualCapturePromise = waitForError('nestjs-websockets', event => {
return event.exception?.values?.[0]?.value === 'Manually captured WebSocket error';
});

const socket = io(baseURL!);
await new Promise<void>(resolve => socket.on('connect', resolve));

socket.emit('test-exception', {});
socket.emit('test-manual-capture', {});
await manualCapturePromise;

expect(errorEventOccurred).toBe(false);

socket.disconnect();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test('Sends an HTTP transaction', async ({ baseURL }) => {
const txPromise = waitForTransaction('nestjs-websockets', tx => {
return tx?.contexts?.trace?.op === 'http.server' && tx?.transaction === 'GET /test-transaction';
});

await fetch(`${baseURL}/test-transaction`);

const tx = await txPromise;

expect(tx.contexts?.trace).toEqual(
expect.objectContaining({
op: 'http.server',
}),
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"moduleResolution": "Node16"
}
}
Loading