diff --git a/test/loaders/ApiBasedRepositoryLoaderTests.test.ts b/test/loaders/ApiBasedRepositoryLoaderTests.test.ts index c891dc7..6a3d615 100644 --- a/test/loaders/ApiBasedRepositoryLoaderTests.test.ts +++ b/test/loaders/ApiBasedRepositoryLoaderTests.test.ts @@ -41,6 +41,17 @@ async function assertAsyncCallbackThrowsApiResponseError(callback: () => Promise await expect(callback).rejects.toThrow(ApiResponseError); } +function updateGlobalFetchMockJsonToThrowSyntaxError() { + // @ts-ignore + global.fetch = jest.fn(() => { + return Promise.resolve({ + json: () => Promise.reject(new SyntaxError("Unable to parse JSON")), + status: 200, + ok: true, + }) + }) as jest.Mock; +} + describe("ApiBasedRepositoryLoader", () => { let loader: ApiBasedRepositoryLoader; @@ -122,14 +133,7 @@ describe("ApiBasedRepositoryLoader", () => { // The Passio API returns some invalid JSON if there is no data, // so simulate a JSON parsing error - // @ts-ignore - global.fetch = jest.fn(() => { - return Promise.resolve({ - json: () => Promise.reject(new SyntaxError("Unable to parse JSON")), - status: 200, - ok: true, - }) - }) as jest.Mock; + updateGlobalFetchMockJsonToThrowSyntaxError(); await assertAsyncCallbackThrowsApiResponseError(async () => { await loader.fetchAndUpdateRouteDataForSystemId("263"); @@ -160,14 +164,7 @@ describe("ApiBasedRepositoryLoader", () => { }); it("throws the correct error if the API response contains no data", async () => { - // @ts-ignore - global.fetch = jest.fn(() => { - return Promise.resolve({ - json: () => Promise.reject(new SyntaxError("Unable to parse JSON")), - status: 200, - ok: true, - }) - }) as jest.Mock; + updateGlobalFetchMockJsonToThrowSyntaxError(); await assertAsyncCallbackThrowsApiResponseError(async () => { await loader.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId("263");