Skip to content

Commit 9e6aefb

Browse files
committed
test: migrate test files to Vitest and improve assertion patterns
- Switch test runner assertions from Jest to Vitest - Add Vitest imports and remove Jest-specific ones - Replace jest-fetch-mock with msw for HTTP mocking - Use type narrowing with asserts instead of type assertions - Change string payloads to objects, use HttpResponse.json from msw - Refactor .then/.catch chains to async/await with try/catch where applicable - Add assertions for unexpected branches in try/catch blocks
1 parent 07d308d commit 9e6aefb

File tree

5 files changed

+925
-820
lines changed

5 files changed

+925
-820
lines changed

src/hydra/fetchJsonLd.test.ts

Lines changed: 119 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,143 @@
1+
import { http, HttpResponse } from "msw";
2+
import { server } from "../../vitest.setup.js";
13
import fetchJsonLd from "./fetchJsonLd.js";
2-
import type { FetchMock } from "jest-fetch-mock";
3-
import type { ResponseDocument } from "./fetchJsonLd.js";
4+
import { assert, expect, test } from "vitest";
45

5-
const fetchMock = fetch as FetchMock;
6-
7-
test("fetch a JSON-LD document", () => {
8-
fetchMock.mockResponseOnce(
9-
`{
10-
"@context": "http://json-ld.org/contexts/person.jsonld",
11-
"@id": "http://dbpedia.org/resource/John_Lennon",
12-
"name": "John Lennon",
13-
"born": "1940-10-09",
14-
"spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
15-
}`,
16-
{
17-
status: 200,
18-
statusText: "OK",
19-
headers: { "Content-Type": "application/ld+json" },
20-
},
6+
test("fetch a JSON-LD document", async () => {
7+
server.use(
8+
http.get("http://localhost/foo.jsonld", () =>
9+
HttpResponse.json(
10+
{
11+
"@context": "http://json-ld.org/contexts/person.jsonld",
12+
"@id": "http://dbpedia.org/resource/John_Lennon",
13+
name: "John Lennon",
14+
born: "1940-10-09",
15+
spouse: "http://dbpedia.org/resource/Cynthia_Lennon",
16+
},
17+
{
18+
headers: { "Content-Type": "application/ld+json" },
19+
status: 200,
20+
statusText: "OK",
21+
},
22+
),
23+
),
2124
);
22-
23-
return fetchJsonLd("/foo.jsonld").then((data) => {
25+
try {
26+
const data = await fetchJsonLd("http://localhost/foo.jsonld");
2427
expect(data.response.ok).toBe(true);
25-
expect(((data as ResponseDocument).body as { name: string }).name).toBe(
26-
"John Lennon",
28+
expect(data).toHaveProperty("body");
29+
assert(
30+
"body" in data && "name" in data.body,
31+
"Response should contain a body with a name property",
2732
);
28-
});
33+
expect(data.body["name"]).toBe("John Lennon");
34+
} catch {
35+
assert.fail("Should not have thrown an error");
36+
}
2937
});
3038

31-
test("fetch a non JSON-LD document", () => {
32-
fetchMock.mockResponseOnce(`<body>Hello</body>`, {
33-
status: 200,
34-
statusText: "OK",
35-
headers: { "Content-Type": "text/html" },
36-
});
37-
38-
return fetchJsonLd("/foo.jsonld").catch(
39-
(data: { response: Response; body: undefined }) => {
40-
expect(data.response.ok).toBe(true);
41-
expect(typeof data.body).toBe("undefined");
42-
},
39+
test("fetch a non JSON-LD document", async () => {
40+
server.use(
41+
http.get("http://localhost/foo.jsonld", () => {
42+
return new HttpResponse(`<body>Hello</body>`, {
43+
headers: { "Content-Type": "text/html" },
44+
status: 200,
45+
statusText: "OK",
46+
});
47+
}),
4348
);
49+
50+
try {
51+
await fetchJsonLd("http://localhost/foo.jsonld");
52+
assert.fail("Should have thrown an error");
53+
} catch (error) {
54+
const data = error as unknown as { response: Response; body: undefined };
55+
expect(data.response.ok).toBe(true);
56+
expect(typeof data.body).toBe("undefined");
57+
}
4458
});
4559

4660
test("fetch an error with Content-Type application/ld+json", () => {
47-
fetchMock.mockResponseOnce(
48-
`{
49-
"@context": "http://json-ld.org/contexts/person.jsonld",
50-
"@id": "http://dbpedia.org/resource/John_Lennon",
51-
"name": "John Lennon",
52-
"born": "1940-10-09",
53-
"spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
54-
}`,
55-
{
56-
status: 400,
57-
statusText: "Bad Request",
58-
headers: { "Content-Type": "application/ld+json" },
59-
},
61+
server.use(
62+
http.get("http://localhost/foo.jsonld", () => {
63+
return HttpResponse.json(
64+
{
65+
"@context": "http://json-ld.org/contexts/person.jsonld",
66+
"@id": "http://dbpedia.org/resource/John_Lennon",
67+
name: "John Lennon",
68+
born: "1940-10-09",
69+
spouse: "http://dbpedia.org/resource/Cynthia_Lennon",
70+
},
71+
{
72+
status: 400,
73+
statusText: "Bad Request",
74+
headers: { "Content-Type": "application/ld+json" },
75+
},
76+
);
77+
}),
6078
);
6179

62-
return fetchJsonLd("/foo.jsonld").catch(
80+
return fetchJsonLd("http://localhost/foo.jsonld").catch(
6381
({ response }: { response: Response }) => {
64-
void response.json().then((body: { born: string }) => {
65-
expect(response.ok).toBe(false);
66-
expect(body.born).toBe("1940-10-09");
67-
});
82+
response
83+
.json()
84+
.then((body: { born: string }) => {
85+
expect(response.ok).toBe(false);
86+
expect(body.born).toBe("1940-10-09");
87+
})
88+
.catch(() => {
89+
assert.fail("Response should have been JSON parsable");
90+
});
6891
},
6992
);
7093
});
7194

72-
test("fetch an error with Content-Type application/error+json", () => {
73-
fetchMock.mockResponseOnce(
74-
`{
75-
"@context": "http://json-ld.org/contexts/person.jsonld",
76-
"@id": "http://dbpedia.org/resource/John_Lennon",
77-
"name": "John Lennon",
78-
"born": "1940-10-09",
79-
"spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
80-
}`,
81-
{
82-
status: 400,
83-
statusText: "Bad Request",
84-
headers: { "Content-Type": "application/error+json" },
85-
},
95+
test("fetch an error with Content-Type application/error+json", async () => {
96+
server.use(
97+
http.get("http://localhost/foo.jsonld", () => {
98+
return HttpResponse.json(
99+
{
100+
"@context": "http://json-ld.org/contexts/person.jsonld",
101+
"@id": "http://dbpedia.org/resource/John_Lennon",
102+
name: "John Lennon",
103+
born: "1940-10-09",
104+
spouse: "http://dbpedia.org/resource/Cynthia_Lennon",
105+
},
106+
{
107+
status: 400,
108+
statusText: "Bad Request",
109+
headers: { "Content-Type": "application/error+json" },
110+
},
111+
);
112+
}),
86113
);
87114

88-
return fetchJsonLd("/foo.jsonld").catch(
89-
({ response }: { response: Response }) => {
90-
void response.json().then((body: { born: string }) => {
91-
expect(response.ok).toBe(false);
92-
expect(body.born).toBe("1940-10-09");
93-
});
94-
},
95-
);
115+
try {
116+
await fetchJsonLd("http://localhost/foo.jsonld");
117+
assert.fail("Should have thrown an error");
118+
} catch (error) {
119+
const data = error as unknown as { response: Response };
120+
const body = await data.response.json();
121+
expect(data.response.ok).toBe(false);
122+
expect(body.born).toBe("1940-10-09");
123+
}
96124
});
97125

98-
test("fetch an empty document", () => {
99-
fetchMock.mockResponseOnce("", {
100-
status: 204,
101-
statusText: "No Content",
102-
headers: { "Content-Type": "text/html" },
103-
});
104-
105-
return fetchJsonLd("/foo.jsonld").then((data) => {
126+
test("fetch an empty document", async () => {
127+
server.use(
128+
http.get("http://localhost/foo.jsonld", () => {
129+
return new HttpResponse(``, {
130+
status: 204,
131+
statusText: "No Content",
132+
headers: { "Content-Type": "text/html" },
133+
});
134+
}),
135+
);
136+
try {
137+
const data = await fetchJsonLd("http://localhost/foo.jsonld");
106138
expect(data.response.ok).toBe(true);
107-
expect((data as ResponseDocument).body).toBe(undefined);
108-
});
139+
expect(data).not.toHaveProperty("body");
140+
} catch {
141+
assert.fail("Should not have thrown an error");
142+
}
109143
});

0 commit comments

Comments
 (0)