Skip to content

Commit e48ed70

Browse files
author
Guy Bedford
authored
Refactor tests to use throwing assertions (#975)
1 parent 4344c59 commit e48ed70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+6037
-12602
lines changed

integration-tests/js-compute/fixtures/app/src/assertions-throwing.js

Lines changed: 0 additions & 197 deletions
This file was deleted.

integration-tests/js-compute/fixtures/app/src/assertions.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function pass(message = "") {
3737
}
3838

3939
export function fail(message = "") {
40-
return new Response(message, { status: 500 });
40+
throw new Response(message, { status: 500 });
4141
}
4242

4343
function prettyPrintSymbol(a) {
@@ -48,17 +48,27 @@ function prettyPrintSymbol(a) {
4848
}
4949
export function assert(actual, expected, code) {
5050
if (!deepEqual(actual, expected)) {
51-
return fail(
51+
fail(
5252
`Expected \`${code}\` to equal \`${JSON.stringify(prettyPrintSymbol(expected))}\` - Found \`${JSON.stringify(prettyPrintSymbol(actual))}\``,
5353
);
5454
}
5555
}
5656

57+
export { assert as strictEqual }
58+
59+
export function ok(truthy, code) {
60+
if (!truthy) {
61+
fail(
62+
`Expected ${code ? ' ' + code : ''}to be truthy - Found \`${JSON.stringify(prettyPrintSymbol(truthy))}\``,
63+
);
64+
}
65+
}
66+
5767
export async function assertResolves(func) {
5868
try {
5969
await func();
6070
} catch (error) {
61-
return fail(
71+
fail(
6272
`Expected \`${func.toString()}\` to resolve - Found it rejected: ${error.name}: ${error.message}`,
6373
);
6474
}
@@ -67,63 +77,65 @@ export async function assertResolves(func) {
6777
export async function assertRejects(func, errorClass, errorMessage) {
6878
try {
6979
await func();
70-
return fail(
71-
`Expected \`${func.toString()}\` to reject - Found it did not reject`,
72-
);
7380
} catch (error) {
7481
if (errorClass) {
7582
if (error instanceof errorClass === false) {
76-
return fail(
83+
fail(
7784
`Expected \`${func.toString()}\` to reject instance of \`${errorClass.name}\` - Found instance of \`${error.name}\``,
7885
);
7986
}
8087
}
8188

8289
if (errorMessage) {
8390
if (error.message !== errorMessage) {
84-
return fail(
91+
fail(
8592
`Expected \`${func.toString()}\` to reject error message of \`${errorMessage}\` - Found \`${error.message}\``,
8693
);
8794
}
8895
}
96+
97+
return;
8998
}
99+
fail(`Expected \`${func.toString()}\` to reject - Found it did not reject`);
90100
}
91101

92102
export function assertThrows(func, errorClass, errorMessage) {
93103
try {
94104
func();
95-
return fail(
96-
`Expected \`${func.toString()}\` to throw - Found it did not throw`,
97-
);
98105
} catch (error) {
99106
if (errorClass) {
100107
if (error instanceof errorClass === false) {
101-
return fail(
108+
fail(
102109
`Expected \`${func.toString()}\` to throw instance of \`${errorClass.name}\` - Found instance of \`${error.name}\`: ${error.message}\n${error.stack}`,
103110
);
104111
}
105112
}
106113

107114
if (errorMessage) {
108115
if (error.message !== errorMessage) {
109-
return fail(
116+
fail(
110117
`Expected \`${func.toString()}\` to throw error message of \`${errorMessage}\` - Found \`${error.message}\``,
111118
);
112119
}
113120
}
121+
122+
return;
114123
}
124+
fail(`Expected \`${func.toString()}\` to throw - Found it did not throw`);
115125
}
116126

117127
export function assertDoesNotThrow(func) {
118128
try {
119129
func();
120130
} catch (error) {
121-
return fail(
131+
fail(
122132
`Expected \`${func.toString()}\` to not throw - Found it did throw: ${error.name}: ${error.message}`,
123133
);
124134
}
125135
}
126136

137+
export { deepEqual as deepStrictEqual }
138+
127139
export function deepEqual(a, b) {
128140
var aKeys;
129141
var bKeys;

integration-tests/js-compute/fixtures/app/src/async-select.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { routes } from "./routes";
1+
import { routes } from './routes';
22

3-
routes.set("/async-select/hello", () => {
3+
routes.set('/async-select/hello', () => {
44
let requestsData = [
55
{
6-
url: "https://compute-sdk-test-backend.edgecompute.app/async_select_1",
7-
backend: "TheOrigin",
8-
header: "fooname",
6+
url: 'https://compute-sdk-test-backend.edgecompute.app/async_select_1',
7+
backend: 'TheOrigin',
8+
header: 'fooname',
99
},
1010
{
11-
url: "https://compute-sdk-test-backend.edgecompute.app/async_select_2",
12-
backend: "TheOrigin2",
13-
header: "barname",
11+
url: 'https://compute-sdk-test-backend.edgecompute.app/async_select_2',
12+
backend: 'TheOrigin2',
13+
header: 'barname',
1414
},
1515
];
1616

@@ -36,17 +36,17 @@ async function processUpstreamRequests(requests, requestsData) {
3636
let headerValue = response.headers.get(header);
3737
if (headerValue == null) {
3838
throw new Error(
39-
"No header value on the response from the request with url " +
39+
'No header value on the response from the request with url ' +
4040
response.url +
41-
" and with the header name: " +
41+
' and with the header name: ' +
4242
header,
4343
);
4444
}
4545
responseHeaders.set(header, headerValue);
4646
}
4747

4848
// Send our response downstream
49-
return new Response("pong", {
49+
return new Response('pong', {
5050
headers: responseHeaders,
5151
});
5252
}

0 commit comments

Comments
 (0)