-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
75 lines (66 loc) · 1.98 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
assertEquals,
assertThrowsAsync,
} from "https://deno.land/std@0.90.0/testing/asserts.ts";
import { mockFetch, unMockFetch } from "./mod.ts";
Deno.test("throws error when requests are not equal", async () => {
const request = new Request("https://github.com/satyarohith", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
message: "i'm different",
}),
});
mockFetch(request, new Response());
assertThrowsAsync<Response>(() =>
fetch("https://github.com/satyarohith", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
message: "i'm different too",
}),
})
);
unMockFetch();
});
Deno.test("mocks fetch() as expected", async () => {
const request1 = new Request("https://example.com");
const expectedResponse1 = new Response("<html> example.com </html>", {
status: 200,
headers: {
"content-type": "text/html",
},
});
const request2 = new Request("https://example.rest");
const expectedResponse2 = new Response(
JSON.stringify({ message: "example" }),
{
status: 200,
headers: {
"content-type": "text/html",
},
},
);
await mockFetch(request1, expectedResponse1.clone());
await mockFetch(request2, expectedResponse2.clone());
const response1 = await fetch(request1);
const response2 = await fetch(request2);
assertEquals(response1.status, expectedResponse1.status);
assertEquals(await response1.text(), await expectedResponse1.text());
assertEquals(
response1.headers.get("content-type"),
expectedResponse1.headers.get("content-type"),
);
assertEquals(response2.status, expectedResponse2.status);
assertEquals(await response2.text(), await expectedResponse2.text());
assertEquals(
response2.headers.get("content-type"),
expectedResponse2.headers.get("content-type"),
);
// Restore original fetch().
unMockFetch();
});