Skip to content

Commit

Permalink
chore(nestjs): fix tests & apply linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjoertjuh committed Sep 13, 2024
1 parent 7367fb3 commit 1cba377
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Logger, Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { GraphQLModule } from '@nestjs/graphql';
import { SentryGlobalGenericFilter, SentryModule } from '@sentry/nestjs/setup';
import { AppResolver } from './app.resolver';
import { AppController } from './app.controller';
import { AppResolver } from './app.resolver';

@Module({
imports: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
import { Cron, SchedulerRegistry } from '@nestjs/schedule';
import * as Sentry from '@sentry/nestjs';
import { SentryCron, SentryTraced } from '@sentry/nestjs';
import type { MonitorConfig } from '@sentry/types';

const monitorConfig: MonitorConfig = {
schedule: {
type: 'crontab',
value: '* * * * *',
},
};

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

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

testSpan() {
// span that should not be a child span of the middleware span
Sentry.startSpan({ name: 'test-controller-span' }, () => {});
}

testException(id: string) {
throw new Error(`This is an exception with id ${id}`);
}
Expand All @@ -38,60 +13,4 @@ export class AppService {
testExpected500Exception(id: string) {
throw new HttpException(`This is an expected 500 exception with id ${id}`, HttpStatus.INTERNAL_SERVER_ERROR);
}

testExpectedRpcException(id: string) {
throw new RpcException(`This is an expected RPC exception with id ${id}`);
}

@SentryTraced('wait and return a string')
async wait() {
await new Promise(resolve => setTimeout(resolve, 500));
return 'test';
}

async testSpanDecoratorAsync() {
return await this.wait();
}

@SentryTraced('return a string')
getString(): { result: string } {
return { result: 'test' };
}

async testSpanDecoratorSync() {
const returned = this.getString();
// Will fail if getString() is async, because returned will be a Promise<>
return returned.result;
}

/*
Actual cron schedule differs from schedule defined in config because Sentry
only supports minute granularity, but we don't want to wait (worst case) a
full minute for the tests to finish.
*/
@Cron('*/5 * * * * *', { name: 'test-cron-job' })
@SentryCron('test-cron-slug', monitorConfig)
async testCron() {
console.log('Test cron!');
}

async killTestCron() {
this.schedulerRegistry.deleteCronJob('test-cron-job');
}

use() {
console.log('Test use!');
}

transform() {
console.log('Test transform!');
}

intercept() {
console.log('Test intercept!');
}

canActivate() {
console.log('Test canActivate!');
}
}
2 changes: 1 addition & 1 deletion packages/nestjs/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type {
OnModuleInit,
} from '@nestjs/common';
import { Catch, Global, HttpException, Injectable, Logger, Module } from '@nestjs/common';
import { APP_INTERCEPTOR, BaseExceptionFilter } from '@nestjs/core';
import type { HttpServer } from '@nestjs/common';
import { APP_INTERCEPTOR, BaseExceptionFilter } from '@nestjs/core';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
Expand Down

0 comments on commit 1cba377

Please sign in to comment.