diff --git a/test/fetch/createFetchMock.ts b/test/fetch/createFetchMock.ts index 3d74eb8..4e4daa4 100644 --- a/test/fetch/createFetchMock.ts +++ b/test/fetch/createFetchMock.ts @@ -3,13 +3,17 @@ import {type MockedResponse} from '@Test/fetch/defineMockResponse'; import {getAutoImplementation} from './getAutoImplementation'; export const createFetchMock = ( - implementation?: MockedResponse, + implementation?: MockedResponse | (() => Promise>), // eslint-disable-next-line @typescript-eslint/no-unused-vars options?: Record, ): FetchMock => { return fetchMock.mockImplementation(async (info, init) => { let resolvedImplementation = implementation; + if (typeof implementation === 'function') { + resolvedImplementation = await implementation(); + } + if (!implementation && info) { resolvedImplementation = await getAutoImplementation(info, init); } diff --git a/test/fetch/defineMockResponse.ts b/test/fetch/defineMockResponse.ts index ebf03f4..39e738a 100644 --- a/test/fetch/defineMockResponse.ts +++ b/test/fetch/defineMockResponse.ts @@ -10,7 +10,7 @@ export interface MockedResponse extends ResponseInit { export interface MockResponse { match(url: string, init?: RequestInit): boolean; - response(): MockedResponse; + response(): MockedResponse | Promise>; } export const defineMockResponse = (responseObject: MockResponse): MockResponse => { diff --git a/test/fetch/examples/get-extended-timeout.ts b/test/fetch/examples/get-extended-timeout.ts index b5d1227..6134d40 100644 --- a/test/fetch/examples/get-extended-timeout.ts +++ b/test/fetch/examples/get-extended-timeout.ts @@ -1,12 +1,12 @@ // noinspection JSUnusedGlobalSymbols -import {defineMockResponse} from '@Test/fetch/defineMockResponse'; +import {type MockedResponse, defineMockResponse} from '@Test/fetch/defineMockResponse'; export default defineMockResponse({ match: (path: string, init?: RequestInit) => init?.method === 'GET' && path === '/timeout', response: () => { - return new Promise((resolve) => { + return new Promise((resolve) => { setTimeout(() => { resolve({ headers: {'Content-Type': 'application/json'},