Skip to content

Commit

Permalink
test: support function responses in fetchMock (#151)
Browse files Browse the repository at this point in the history
Needed to fix the typecheck so we can release the previous build
  • Loading branch information
Cysword authored Jan 4, 2024
1 parent 5e81e18 commit 14ddd2c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion test/fetch/createFetchMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import {type MockedResponse} from '@Test/fetch/defineMockResponse';
import {getAutoImplementation} from './getAutoImplementation';

export const createFetchMock = <T>(
implementation?: MockedResponse<T>,
implementation?: MockedResponse<T> | (() => Promise<MockedResponse<T>>),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: Record<string, unknown>,
): 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);
}
Expand Down
2 changes: 1 addition & 1 deletion test/fetch/defineMockResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface MockedResponse<E = unknown> extends ResponseInit {

export interface MockResponse<E> {
match(url: string, init?: RequestInit): boolean;
response(): MockedResponse<E>;
response(): MockedResponse<E> | Promise<MockedResponse<E>>;
}

export const defineMockResponse = <E>(responseObject: MockResponse<E>): MockResponse<E> => {
Expand Down
4 changes: 2 additions & 2 deletions test/fetch/examples/get-extended-timeout.ts
Original file line number Diff line number Diff line change
@@ -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<MockedResponse>((resolve) => {
setTimeout(() => {
resolve({
headers: {'Content-Type': 'application/json'},
Expand Down

0 comments on commit 14ddd2c

Please sign in to comment.