move json syntax error setting to top-level method

This commit is contained in:
2025-01-22 14:59:15 -08:00
parent df0e1d3d0f
commit 81d0bb9e01

View File

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