Skip to content

Commit 2011bae

Browse files
committed
test: Add test for unparseable JSON error being catchable
1 parent 1f03fe2 commit 2011bae

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

__tests__/ipinfoWrapper.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ let ipinfoWrapper: IPinfoWrapper;
77
beforeEach(() => {
88
dotenv.config();
99
const token = process.env.IPINFO_TOKEN || "";
10+
11+
if (!token) {
12+
throw new Error(
13+
"Tests require a token in the IPINFO_TOKEN Environment Variable."
14+
);
15+
}
16+
1017
ipinfoWrapper = new IPinfoWrapper(token);
1118
});
1219

@@ -189,4 +196,20 @@ describe("IPinfoWrapper", () => {
189196
const ipinfo = new IPinfoWrapper("invalid-token");
190197
await expect(ipinfo.lookupIp("1.2.3.4")).rejects.toThrow();
191198
});
199+
200+
test("Error is thrown when response cannot be parsed", async () => {
201+
const baseUrlWithUnparseableResponse =
202+
"https://ipinfo.io/developers?path=";
203+
const ipinfo = new IPinfoWrapper(
204+
"token",
205+
baseUrlWithUnparseableResponse
206+
);
207+
await expect(ipinfo.lookupIp("1.2.3.4")).rejects.toThrow();
208+
209+
const status = await ipinfo
210+
.lookupIp("1.2.3.4")
211+
.then((_) => "parseable")
212+
.catch((_) => "unparseable");
213+
expect(status).toEqual("unparseable");
214+
});
192215
});

0 commit comments

Comments
 (0)