-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.test.ts
More file actions
53 lines (42 loc) · 1.55 KB
/
Copy pathsync.test.ts
File metadata and controls
53 lines (42 loc) · 1.55 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
import { describe, expect, it, vi } from "vitest";
vi.mock("#wasm/nix_eval.js", () => ({
default: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn((source: string, _location: string, _strict: boolean) =>
JSON.stringify({
errors: "",
warnings: "",
output: source === "6 * 7" ? "42" : "",
bytecode: "bytecode",
trace: "trace",
ast: "ast",
}),
),
}));
import { createEvaluator } from "#src/sync.js";
describe("createEvaluator", () => {
it("returns an evaluator with an eval function", async () => {
const evaluator = await createEvaluator();
expect(evaluator).toHaveProperty("eval");
expect(typeof evaluator.eval).toBe("function");
});
it("initializes WASM only once", async () => {
const mod = await import("#wasm/nix_eval.js");
const init = mod.default as ReturnType<typeof vi.fn>;
await createEvaluator();
await createEvaluator();
expect(init).toHaveBeenCalledTimes(1);
});
it("evaluates Nix source through the WASM binding", async () => {
const evaluator = await createEvaluator();
const result = await evaluator.eval("6 * 7");
expect(result.output).toBe("42");
expect(result.errors).toBe("");
});
it("passes the strict option to the WASM binding", async () => {
const mod = await import("#wasm/nix_eval.js");
const evaluate = mod.evaluate as ReturnType<typeof vi.fn>;
const evaluator = await createEvaluator({ strict: true });
await evaluator.eval("6 * 7");
expect(evaluate).toHaveBeenLastCalledWith("6 * 7", "/input.nix", true);
});
});