forked from sergiodxa/remix-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vitest.setup.ts
140 lines (117 loc) · 2.96 KB
/
vitest.setup.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { installGlobals } from "@remix-run/node";
import { expect } from "vitest";
import "@testing-library/jest-dom";
installGlobals();
function validateIsResponse(response: Response) {
if (response instanceof Response === false) {
return {
pass: false,
message() {
return "The value to assert must be an instance of Response.";
},
};
}
return;
}
function toHaveStatus(response: Response, status: number) {
let isNotResponse = validateIsResponse(response);
if (isNotResponse) return isNotResponse;
let pass = response.status === status;
return {
pass,
message() {
if (pass) {
return `The status code of the Response should not be ${status}.`;
}
return `The status code of the Response should be ${status}, it was ${response.status}.`;
},
};
}
function toRedirect(response: Response | string, path?: string) {
if (typeof response === "string") {
return {
pass: response === path,
message() {
if (response === path) {
return `The Response should not redirect to ${path}`;
}
return `The response should redirect to ${path}`;
},
};
}
let isNotResponse = validateIsResponse(response);
if (isNotResponse) return isNotResponse;
let header = response.headers.get("Location");
let status = response.status;
let pass = status === 302 && path ? header === path : Boolean(header);
return {
pass,
message() {
if (pass) {
return `The Response should not redirect to ${path}`;
}
return `The response should redirect to ${path}`;
},
};
}
function toSetACookie(response: Response) {
let isNotResponse = validateIsResponse(response);
if (isNotResponse) return isNotResponse;
let hasSetCookie = response.headers.has("Set-Cookie");
return {
pass: hasSetCookie,
message() {
if (hasSetCookie) return "Expected the response to not set a cookie.";
return "Expected the response to set a cookie.";
},
};
}
function toBeOk(response: Response) {
let isNotResponse = validateIsResponse(response);
if (isNotResponse) return isNotResponse;
let pass = response.ok;
return {
pass,
message() {
if (pass) return "It should not to be ok.";
return "It should be ok.";
},
};
}
function toHaveHeader(response: Response, name: string, value?: string) {
let isNotResponse = validateIsResponse(response);
if (isNotResponse) return isNotResponse;
let pass = response.headers.has(name);
if (!value) {
return {
pass,
message() {
if (pass) return `It should not have the header ${name}`;
return `It should have the header ${name}`;
},
};
}
if (value) {
pass = response.headers.get(name) === value;
}
return {
pass,
message() {
if (pass) {
return `It should not have the header ${name} with value ${value}, it was ${response.headers.get(
name,
)}`;
}
return `It should have the header ${name} with value ${value}, it was ${response.headers.get(
name,
)}`;
},
};
}
expect.extend({
toRedirect,
toSetACookie,
toHaveStatus,
toBeOk,
toHaveHeader,
});