From 2011bae66f93d0e598de8e9c1b061ce66f92d8f3 Mon Sep 17 00:00:00 2001 From: Samuel Ryan Date: Thu, 14 Nov 2024 13:59:32 +0800 Subject: [PATCH] test: Add test for unparseable JSON error being catchable --- __tests__/ipinfoWrapper.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/__tests__/ipinfoWrapper.test.ts b/__tests__/ipinfoWrapper.test.ts index 668c9ec..1ec8301 100644 --- a/__tests__/ipinfoWrapper.test.ts +++ b/__tests__/ipinfoWrapper.test.ts @@ -7,6 +7,13 @@ let ipinfoWrapper: IPinfoWrapper; beforeEach(() => { dotenv.config(); const token = process.env.IPINFO_TOKEN || ""; + + if (!token) { + throw new Error( + "Tests require a token in the IPINFO_TOKEN Environment Variable." + ); + } + ipinfoWrapper = new IPinfoWrapper(token); }); @@ -189,4 +196,20 @@ describe("IPinfoWrapper", () => { const ipinfo = new IPinfoWrapper("invalid-token"); await expect(ipinfo.lookupIp("1.2.3.4")).rejects.toThrow(); }); + + test("Error is thrown when response cannot be parsed", async () => { + const baseUrlWithUnparseableResponse = + "https://ipinfo.io/developers?path="; + const ipinfo = new IPinfoWrapper( + "token", + baseUrlWithUnparseableResponse + ); + await expect(ipinfo.lookupIp("1.2.3.4")).rejects.toThrow(); + + const status = await ipinfo + .lookupIp("1.2.3.4") + .then((_) => "parseable") + .catch((_) => "unparseable"); + expect(status).toEqual("unparseable"); + }); });