Skip to content

Commit b3574d8

Browse files
Fix failing tests by mocking Unipept API responses with strict input validation
Tests were failing because they relied on live Unipept API data which could change over time. This change introduces a robust mocking mechanism for `fetch` in the test environment. - Created `tests/mocks/apiData.ts` with static responses for Unipept API endpoints. - Responses for `pept2ec`, `pept2go`, `pept2interpro` are structured as nested objects to correctly trigger the `CSVFormatter` flattening logic and match expected output headers (e.g., `ec_protein_count`). - Created `tests/mocks/mockFetch.ts` to intercept fetch requests. - Implements strict validation of the request body, verifying the `input` array and options (`equate_il`, `extra`) match expected test values. This ensures that regressions in parameter passing by the CLI are caught. - Uses `eslint-disable` for `any` types required for flexible body parsing. - Updated all relevant tests (`pept2prot.test.ts`, `pept2taxa.test.ts`, `pept2lca.test.ts`, `peptinfo.test.ts`, `pept2ec.test.ts`, `pept2go.test.ts`, `pept2interpro.test.ts`, `pept2funct.test.ts`, `taxa2lca.test.ts`, `taxonomy.test.ts`, `protinfo.test.ts`) to use `setupMockFetch()` in `beforeAll`. - All tests now pass and linting is successful.
1 parent 3786ffb commit b3574d8

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

tests/mocks/mockFetch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { jest } from '@jest/globals';
22
import * as apiData from './apiData';
33

44
export function setupMockFetch() {
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56
(global as any).fetch = jest.fn((url: string | URL, options?: RequestInit) => {
67
const urlString = url.toString();
78

9+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
810
let bodyInput: any = [];
911
let bodyEquate: boolean = false;
1012
let bodyExtra: boolean = false;

0 commit comments

Comments
 (0)