Skip to content

Commit

Permalink
fix: enable type-checking in types/test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkluijk committed Oct 23, 2024
1 parent 2cb9219 commit afe69c3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.eslint.json',
project: './tsconfig.json',
// Because we are setting "type": "module" in our package.json, all `.js` files are treated as modules
// Sometimes we will want a commonjs file, like this eslint config, in which case we use the .cjs extension
extraFileExtensions: ['.cjs'],
Expand Down
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Mock } from '@vitest/spy';
export type FetchMock = Mock<typeof global.fetch> & FetchMockObject;

class FetchMockObject {
private readonly isMocking = vitest.fn(always(true));
public readonly isMocking = vitest.fn(always(true));

constructor(
private mockedFetch: Mock<typeof global.fetch>,
Expand Down Expand Up @@ -139,12 +139,16 @@ class FetchMockObject {

// reject (error)
mockReject(error?: ErrorOrFunction): FetchMock {
this.mockedFetch.mockImplementation(() => normalizeError(error));
this.mockedFetch.mockImplementation((input: RequestInput, requestInit?: RequestInit) =>
normalizeError(normalizeRequest(input, requestInit), error)
);
return this.chainingResultProvider();
}

mockRejectOnce(error?: ErrorOrFunction): FetchMock {
this.mockedFetch.mockImplementationOnce(() => normalizeError(error));
this.mockedFetch.mockImplementationOnce((input: RequestInput, requestInit?: RequestInit) =>
normalizeError(normalizeRequest(input, requestInit), error)
);
return this.chainingResultProvider();
}

Expand Down Expand Up @@ -301,7 +305,7 @@ type RequestInput = string | URL | Request;
type ResponseProvider = (request: Request) => ResponseLike | Promise<ResponseLike>;
type ResponseLike = MockResponse | ResponseBody | Response;
type ResponseBody = string;
type ErrorOrFunction = Error | string | ((...args: any[]) => Promise<Response>);
type ErrorOrFunction = Error | ResponseBody | ResponseProvider;

export interface MockParams {
status?: number;
Expand Down Expand Up @@ -411,8 +415,12 @@ function always(toggle: boolean): (input: RequestInput, requestInit?: RequestIni
return () => toggle;
}

const normalizeError = (errorOrFunction?: ErrorOrFunction): Promise<Response> =>
typeof errorOrFunction === 'function' ? errorOrFunction() : Promise.reject(errorOrFunction);
const normalizeError = async (request: Request, errorOrFunction?: ErrorOrFunction): Promise<Response> =>
errorOrFunction instanceof Error
? Promise.reject(errorOrFunction)
: typeof errorOrFunction === 'function'
? buildResponse(request, errorOrFunction)
: Promise.reject(errorOrFunction);

function abortError(): Error {
return new DOMException('The operation was aborted.');
Expand Down
2 changes: 1 addition & 1 deletion tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ describe('request', () => {

try {
const response = await request();
expect(response.type).toBe(contentType);
expect(response).toMatchObject({ message: contentType });
} catch (e) {
console.log(e);
}
Expand Down
4 changes: 0 additions & 4 deletions tsconfig.eslint.json

This file was deleted.

6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"baseUrl": "./",
"paths": {
"vitest-fetch-mock": [
"./src"
"./src/index.ts"
]
},
"strict": true,
Expand All @@ -27,6 +27,8 @@
"noEmit": true
},
"include": [
"./src/**/*.ts"
"./src/**/*.ts",
"./tests/**/*.ts",
"./types/**/*.ts"
]
}
9 changes: 5 additions & 4 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import setupFm, { MockResponseInit } from 'vitest-fetch-mock';
import setupFm, { type MockResponse } from 'vitest-fetch-mock';
import { vi } from 'vitest';

const fetchMock = setupFm();
const fetchMock = setupFm(vi);

fetchMock.mockResponse(JSON.stringify({foo: "bar"}));
fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {
Expand Down Expand Up @@ -85,14 +86,14 @@ fetchMock.doMockOnce();
fetchMock.dontMockOnce();
fetchMock.mockOnce();

async function someAsyncHandler(): Promise<MockResponseInit> {
async function someAsyncHandler(): Promise<MockResponse> {
return {
status: 200,
body: await someAsyncStringHandler()
};
}

function someSyncHandler(): MockResponseInit {
function someSyncHandler(): MockResponse {
return {
status: 200,
body: someSyncStringHandler()
Expand Down

0 comments on commit afe69c3

Please sign in to comment.