Repository navigation
Expand file tree
/
Copy pathevaluate.ts
More file actions
121 lines (105 loc) · 2.9 KB
/
Copy pathevaluate.ts
File metadata and controls
121 lines (105 loc) · 2.9 KB
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
import jest from "jest-lite";
import { v4 as uuid } from "uuid";
/** One line of test output, shown to the student and saved as the answer's `context.lines`. */
export type ResultLine = {
success?: boolean;
fail?: boolean;
title?: string;
message: string;
};
export type EvaluationResult = {
/** Whether every assertion passed */
completed: boolean;
lines: ResultLine[];
};
/**
* jest-lite keeps every test it was given and runs them all again on each
* `run()`, and a describe block that throws (e.g. a failing `expect()` outside
* an `it()`) stays open, so later ones nest inside it. It doesn't export a
* way to reset that, so this empties its root describe block directly.
*/
const resetJestState = () => {
const key = Object.getOwnPropertySymbols(globalThis).find(
(symbol) => symbol.description === "JEST_STATE_SYMBOL",
);
const state = key && (globalThis as any)[key];
if (!state?.rootDescribeBlock) return;
Object.assign(state.rootDescribeBlock, { children: [], hooks: [], tests: [] });
state.currentDescribeBlock = state.rootDescribeBlock;
state.hasFocusedTests = false;
};
/**
* Runs the student's code followed by the question's Jest-style assertions,
* in the browser. `code` runs as-is in the page, see the README.
*/
export const evaluate = async (
code: string,
assertions: string,
): Promise<EvaluationResult> => {
const id = uuid();
const js = `
let ____xe = '';
with(jest) {
(() => {
${code}
;
describe('${id}', () => {
${assertions}
});
____xe = '${id}';
})();
}
return [____xe, await jest.run()];
`;
try {
resetJestState();
jest.jest.resetAllMocks();
jest.jest.clearAllMocks();
let response = await Object.getPrototypeOf(
async function () {}
).constructor(
"jest",
js
)(jest);
if (!response || response[0] !== id) {
throw new Error(
"Did not complete running - are you calling `return` in your code?"
);
}
const res = response[1].filter((r: any) => r.testPath.includes(id));
if (!res.length) {
return {
completed: true,
lines: [
{
success: true,
title: "Passed",
message: "Ran without errors",
},
],
};
}
const completed = res.every((r: any) => r.status === "pass");
return {
completed,
lines: res.map((r: any) => ({
success: r.status === "pass",
fail: r.status === "fail",
// The test's name, and those of describe blocks inside the assertions
message: r.testPath.slice(r.testPath.indexOf(id) + 1).join(" - "),
})),
};
} catch (e: any) {
console.log({ e });
return {
completed: false,
lines: [
{
fail: true,
title: "Error",
message: e.message,
},
],
};
}
};