Skip to content
Draft
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
27 changes: 27 additions & 0 deletions lib/instrumentation/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProxyTracerProvider } from '@opentelemetry/api';
import * as api from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import type { Response, SimpleGit } from 'simple-git';
import {
disableInstrumentations,
getTracerProvider,
Expand Down Expand Up @@ -148,5 +149,31 @@
}),
).rejects.toThrow(error);
});

describe('should return result for async fn with intersection type of Promise', async () => {

Check failure on line 153 in lib/instrumentation/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Async arrow function has no 'await' expression. (@typescript-eslint/require-await)

Check failure on line 153 in lib/instrumentation/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Async arrow function has no 'await' expression. (require-await)
const simpleGitMock = {
status: vi.fn(),
} as Partial<SimpleGit>;

const value = 'testResult';

it('when promise is first type', async () => {
const promise = Promise.resolve(value);
// this copies `simpleGitMock` properties onto `promise`
const prom = Object.assign(promise, simpleGitMock) as Response<string>;

const result = await instrument('test', () => prom);
expect(result).toStrictEqual(value);
});

it('when promise is second type', async () => {
const promise = Promise.resolve(value);
// this copies `promise` properties onto `simpleGitMock`
const prom = Object.assign(simpleGitMock, promise) as Response<string>;

const result = await instrument('test', () => prom);
expect(result).toStrictEqual(value);

Check failure on line 175 in lib/instrumentation/index.spec.ts

View workflow job for this annotation

GitHub Actions / test (1/1)

lib/instrumentation/index.spec.ts > instrumentation/index > instrument > should return result for async fn with intersection type of Promise > when promise is second type

AssertionError: expected { status: [Function Mock] } to strictly equal 'testResult' - Expected: "testResult" + Received: { "status": [Function Mock], } ❯ lib/instrumentation/index.spec.ts:175:24
});
});
});
});
3 changes: 2 additions & 1 deletion lib/instrumentation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
ATTR_SERVICE_NAME,
ATTR_SERVICE_VERSION,
} from '@opentelemetry/semantic-conventions';
import { isPromise } from '@sindresorhus/is';
import { pkg } from '../expose.cjs';
import { getEnv } from '../util/env';
import type { RenovateSpanOptions } from './types';
Expand Down Expand Up @@ -182,7 +183,7 @@ export function instrument<F extends () => ReturnType<F>>(
return getTracer().startActiveSpan(name, options, context, (span: Span) => {
try {
const ret = fn();
if (ret instanceof Promise) {
if (isPromise(ret)) {
return ret
.catch((e) => {
span.recordException(e);
Expand Down
Loading