Skip to content

Commit

Permalink
tests(e2e): Refactor nestjs e2e applications into multiple smaller te…
Browse files Browse the repository at this point in the history
…st applications (#12948)

Refactor of the existing nestjs test applications. Before we had one
sample application testing everything nest-related. This PR splits them
up into three applications to make it more readable and easier to
understand what is being tested. It also allows for iterating a bit
quicker in local development.

No new functionality was added. Will add more tests in a follow-up.

The three new services are:
- nestjs-basic: Simple nestjs application with no submodules and tests
for basic functionality of the SDK like error monitoring and span
instrumentation.
- nestjs-with-submodules: NestJS application that is bit more complex
including a submodule (and potentially multiple in the future) to have a
more realistic setup for more advanced testing.
- nestjs-distributed-tracing: Includes tests for trace propagation with
multiple services.
  • Loading branch information
nicohrubec authored Jul 18, 2024
1 parent 935bb61 commit d629991
Show file tree
Hide file tree
Showing 60 changed files with 690 additions and 290 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,9 @@ jobs:
'generic-ts3.8',
'node-fastify',
'node-hapi',
'nestjs',
'nestjs-basic',
'nestjs-distributed-tracing',
'nestjs-with-submodules',
'node-exports-test-app',
'node-koa',
'node-connect',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "nestjs",
"name": "nestjs-basic",
"version": "0.0.1",
"private": true,
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Controller, Get, Param } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get('test-transaction')
testTransaction() {
return this.appService.testTransaction();
}

@Get('test-exception/:id')
async testException(@Param('id') id: string) {
return this.appService.testException(id);
}

@Get('test-expected-exception/:id')
async testExpectedException(@Param('id') id: string) {
return this.appService.testExpectedException(id);
}

@Get('test-span-decorator-async')
async testSpanDecoratorAsync() {
return { result: await this.appService.testSpanDecoratorAsync() };
}

@Get('test-span-decorator-sync')
async testSpanDecoratorSync() {
return { result: await this.appService.testSpanDecoratorSync() };
}

@Get('kill-test-cron')
async killTestCron() {
this.appService.killTestCron();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
imports: [ScheduleModule.forRoot()],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Cron, SchedulerRegistry } from '@nestjs/schedule';
import * as Sentry from '@sentry/nestjs';
import { SentryCron, SentryTraced } from '@sentry/nestjs';
import type { MonitorConfig } from '@sentry/types';
import { makeHttpRequest } from './utils';

const monitorConfig: MonitorConfig = {
schedule: {
Expand All @@ -13,53 +12,15 @@ const monitorConfig: MonitorConfig = {
};

@Injectable()
export class AppService1 {
export class AppService {
constructor(private schedulerRegistry: SchedulerRegistry) {}

testSuccess() {
return { version: 'v1' };
}

testParam(id: string) {
return {
paramWas: id,
};
}

testInboundHeaders(headers: Record<string, string>, id: string) {
return {
headers,
id,
};
}

async testOutgoingHttp(id: string) {
const data = await makeHttpRequest(`http://localhost:3030/test-inbound-headers/${id}`);

return data;
}

async testOutgoingFetch(id: string) {
const response = await fetch(`http://localhost:3030/test-inbound-headers/${id}`);
const data = await response.json();

return data;
}

testTransaction() {
Sentry.startSpan({ name: 'test-span' }, () => {
Sentry.startSpan({ name: 'child-span' }, () => {});
});
}

async testError() {
const exceptionId = Sentry.captureException(new Error('This is an error'));

await Sentry.flush(2000);

return { exceptionId };
}

testException(id: string) {
throw new Error(`This is an exception with id ${id}`);
}
Expand All @@ -68,26 +29,6 @@ export class AppService1 {
throw new HttpException(`This is an expected exception with id ${id}`, HttpStatus.FORBIDDEN);
}

async testOutgoingFetchExternalAllowed() {
const fetchResponse = await fetch('http://localhost:3040/external-allowed');

return fetchResponse.json();
}

async testOutgoingFetchExternalDisallowed() {
const fetchResponse = await fetch('http://localhost:3040/external-disallowed');

return fetchResponse.json();
}

async testOutgoingHttpExternalAllowed() {
return makeHttpRequest('http://localhost:3040/external-allowed');
}

async testOutgoingHttpExternalDisallowed() {
return makeHttpRequest('http://localhost:3040/external-disallowed');
}

@SentryTraced('wait and return a string')
async wait() {
await new Promise(resolve => setTimeout(resolve, 500));
Expand Down Expand Up @@ -124,20 +65,3 @@ export class AppService1 {
this.schedulerRegistry.deleteCronJob('test-cron-job');
}
}

@Injectable()
export class AppService2 {
externalAllowed(headers: Record<string, string>) {
return {
headers,
route: 'external-allowed',
};
}

externalDisallowed(headers: Record<string, string>) {
return {
headers,
route: 'external-disallowed',
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Sentry from '@sentry/nestjs';

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
tunnel: `http://localhost:3031/`, // proxy server
tracesSampleRate: 1,
});
20 changes: 20 additions & 0 deletions dev-packages/e2e-tests/test-applications/nestjs-basic/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Import this first
import './instrument';

// Import other modules
import { BaseExceptionFilter, HttpAdapterHost, NestFactory } from '@nestjs/core';
import * as Sentry from '@sentry/nestjs';
import { AppModule } from './app.module';

const PORT = 3030;

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

const { httpAdapter } = app.get(HttpAdapterHost);
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));

await app.listen(PORT);
}

bootstrap();
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,3 @@ 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),
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# compiled output
/dist
/node_modules
/build

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# temp directory
.temp
.tmp

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@sentry:registry=http://127.0.0.1:4873
@sentry-internal:registry=http://127.0.0.1:4873
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,47 @@
{
"name": "nestjs-distributed-tracing",
"version": "0.0.1",
"private": true,
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"clean": "npx rimraf node_modules pnpm-lock.yaml",
"test": "playwright test",
"test:build": "pnpm install",
"test:assert": "pnpm test"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@sentry/nestjs": "latest || *",
"@sentry/types": "latest || *",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@playwright/test": "^1.44.1",
"@sentry-internal/test-utils": "link:../../../test-utils",
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/node": "18.15.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-loader": "^9.4.3",
"tsconfig-paths": "^4.2.0",
"typescript": "^4.9.5"
}
}
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,25 @@
// Import this first
import './instrument';

// Import other modules
import { BaseExceptionFilter, HttpAdapterHost, NestFactory } from '@nestjs/core';
import * as Sentry from '@sentry/nestjs';
import { TraceInitiatorModule } from './trace-initiator.module';
import { TraceReceiverModule } from './trace-receiver.module';

const TRACE_INITIATOR_PORT = 3030;
const TRACE_RECEIVER_PORT = 3040;

async function bootstrap() {
const trace_initiator_app = await NestFactory.create(TraceInitiatorModule);

const { httpAdapter } = trace_initiator_app.get(HttpAdapterHost);
Sentry.setupNestErrorHandler(trace_initiator_app, new BaseExceptionFilter(httpAdapter));

await trace_initiator_app.listen(TRACE_INITIATOR_PORT);

const trace_receiver_app = await NestFactory.create(TraceReceiverModule);
await trace_receiver_app.listen(TRACE_RECEIVER_PORT);
}

bootstrap();
Loading

0 comments on commit d629991

Please sign in to comment.