Skip to content

Commit

Permalink
Check return value in async test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicohrubec committed Jul 4, 2024
1 parent 17739bb commit e4ffdfd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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' &&
transactionEvent?.transaction === 'GET /test-span-decorator-async'
);
});

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;

Expand All @@ -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',
}),
]),
Expand All @@ -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({
Expand All @@ -71,4 +72,3 @@ test('Transaction includes span and correct value for decorated sync function',
]),
);
});

0 comments on commit e4ffdfd

Please sign in to comment.