Skip to content

Commit

Permalink
test: Add test for unparseable JSON error being catchable
Browse files Browse the repository at this point in the history
shrink committed Nov 14, 2024
1 parent 1f03fe2 commit 2011bae
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions __tests__/ipinfoWrapper.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit 2011bae

Please sign in to comment.