Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Added test spec for helpers/jsonParse.js

Andrew Bastin73dc0b1cfa56
diff --git a/helpers/__tests__/jsonParse.spec.js b/helpers/__tests__/jsonParse.spec.js
new file mode 100644
index 000000000..e7379a11d
--- /dev/null
+++ b/helpers/__tests__/jsonParse.spec.js
@@ -0,0 +1,34 @@
+import jsonParse from "../jsonParse"
+
+describe("jsonParse", () => {
+ test("parses without errors for valid JSON", () => {
+ const testJSON = JSON.stringify({
+ name: "hoppscotch",
+ url: "https://hoppscotch.io",
+ awesome: true,
+ when: 2019,
+ })
+
+ expect(() => jsonParse(testJSON)).not.toThrow()
+ })
+
+ test("throws error for invalid JSON", () => {
+ const testJSON = '{ "name": hopp "url": true }'
+
+ expect(() => jsonParse(testJSON)).toThrow()
+ })
+
+ test("thrown error has proper info fields", () => {
+ expect.assertions(3)
+
+ const testJSON = '{ "name": hopp "url": true }'
+
+ try {
+ jsonParse(testJSON)
+ } catch (e) {
+ expect(e).toHaveProperty("start")
+ expect(e).toHaveProperty("end")
+ expect(e).toHaveProperty("message")
+ }
+ })
+})