From e4ffdfd9493fbb1a7d34d3a8004906e9d7af3240 Mon Sep 17 00:00:00 2001 From: nicohrubec Date: Thu, 4 Jul 2024 14:43:23 +0200 Subject: [PATCH] Check return value in async test --- .../test-applications/nestjs/src/app.controller.ts | 4 ++-- .../test-applications/nestjs/src/app.service.ts | 9 +++++---- .../nestjs/tests/span-decorator.test.ts | 14 +++++++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/nestjs/src/app.controller.ts b/dev-packages/e2e-tests/test-applications/nestjs/src/app.controller.ts index 8bd641b6f253..5ba6bcb2a68e 100644 --- a/dev-packages/e2e-tests/test-applications/nestjs/src/app.controller.ts +++ b/dev-packages/e2e-tests/test-applications/nestjs/src/app.controller.ts @@ -72,12 +72,12 @@ export class AppController1 { @Get('test-span-decorator-async') async testSpanDecoratorAsync() { - return this.appService.testSpanDecoratorAsync(); + return { result: await this.appService.testSpanDecoratorAsync() }; } @Get('test-span-decorator-sync') async testSpanDecoratorSync() { - return { "result": await this.appService.testSpanDecoratorSync() }; + return { result: await this.appService.testSpanDecoratorSync() }; } } diff --git a/dev-packages/e2e-tests/test-applications/nestjs/src/app.service.ts b/dev-packages/e2e-tests/test-applications/nestjs/src/app.service.ts index 72f6823b59ce..7e0df6b7e1c8 100644 --- a/dev-packages/e2e-tests/test-applications/nestjs/src/app.service.ts +++ b/dev-packages/e2e-tests/test-applications/nestjs/src/app.service.ts @@ -77,18 +77,19 @@ export class AppService1 { return makeHttpRequest('http://localhost:3040/external-disallowed'); } - @SentryTraced('wait function') + @SentryTraced('wait and return a string') async wait() { - return new Promise(resolve => setTimeout(resolve, 500)); + await new Promise(resolve => setTimeout(resolve, 500)); + return 'test'; } async testSpanDecoratorAsync() { - await this.wait(); + return await this.wait(); } @SentryTraced('return a string') getString(): string { - return "test"; + return 'test'; } async testSpanDecoratorSync() { diff --git a/dev-packages/e2e-tests/test-applications/nestjs/tests/span-decorator.test.ts b/dev-packages/e2e-tests/test-applications/nestjs/tests/span-decorator.test.ts index eadb531a8b6f..3efdfb979d73 100644 --- a/dev-packages/e2e-tests/test-applications/nestjs/tests/span-decorator.test.ts +++ b/dev-packages/e2e-tests/test-applications/nestjs/tests/span-decorator.test.ts @@ -1,7 +1,7 @@ import { expect, test } from '@playwright/test'; import { waitForTransaction } from '@sentry-internal/test-utils'; -test('Transaction includes span for decorated async function', async ({ baseURL }) => { +test('Transaction includes span and correct value for decorated async function', async ({ baseURL }) => { const transactionEventPromise = waitForTransaction('nestjs', transactionEvent => { return ( transactionEvent?.contexts?.trace?.op === 'http.server' && @@ -9,7 +9,10 @@ test('Transaction includes span for decorated async function', async ({ baseURL ); }); - await fetch(`${baseURL}/test-span-decorator-async`); + const response = await fetch(`${baseURL}/test-span-decorator-async`); + const body = await response.json(); + + expect(body.result).toEqual('test'); const transactionEvent = await transactionEventPromise; @@ -20,14 +23,14 @@ test('Transaction includes span for decorated async function', async ({ baseURL trace_id: expect.any(String), data: { 'sentry.origin': 'manual', - 'sentry.op': 'wait function', + 'sentry.op': 'wait and return a string', 'otel.kind': 'INTERNAL', }, description: 'wait', parent_span_id: expect.any(String), start_timestamp: expect.any(Number), status: 'ok', - op: 'wait function', + op: 'wait and return a string', origin: 'manual', }), ]), @@ -49,8 +52,6 @@ test('Transaction includes span and correct value for decorated sync function', const transactionEvent = await transactionEventPromise; - console.log(transactionEvent); - expect(transactionEvent.spans).toEqual( expect.arrayContaining([ expect.objectContaining({ @@ -71,4 +72,3 @@ test('Transaction includes span and correct value for decorated sync function', ]), ); }); -