diff --git a/lib/instrumentation/index.spec.ts b/lib/instrumentation/index.spec.ts index b3a68eea589..510cd376fd7 100644 --- a/lib/instrumentation/index.spec.ts +++ b/lib/instrumentation/index.spec.ts @@ -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, @@ -148,5 +149,31 @@ describe('instrumentation/index', () => { }), ).rejects.toThrow(error); }); + + describe('should return result for async fn with intersection type of Promise', async () => { + const simpleGitMock = { + status: vi.fn(), + } as Partial; + + 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; + + 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; + + const result = await instrument('test', () => prom); + expect(result).toStrictEqual(value); + }); + }); }); }); diff --git a/lib/instrumentation/index.ts b/lib/instrumentation/index.ts index f68d7d55b45..17a7720dcec 100644 --- a/lib/instrumentation/index.ts +++ b/lib/instrumentation/index.ts @@ -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'; @@ -182,7 +183,7 @@ export function instrument ReturnType>( 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);