-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmustache.test.ts
More file actions
91 lines (77 loc) · 2.4 KB
/
mustache.test.ts
File metadata and controls
91 lines (77 loc) · 2.4 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
import t from "tap";
import { mustache } from "../lib/mustache";
void t.test("mustache", async (t) => {
await t.test("must include a path", (t) => {
// @ts-expect-error bad data on purpose
t.throws(() => mustache({}));
t.end();
});
await t.test("should generate", async (t) => {
const { mustache } = t.mockRequire<typeof import("../lib/mustache")>("../lib/mustache", {
"node:fs/promises": {
readFile: (path: string) => {
t.equal(path, "path/to/file.txt");
return Promise.resolve("<% title %>");
}
}
});
const variables = {
title: "it generates"
};
const generator = mustache({
path: "path/to/file.txt",
variables,
});
const output = await generator.generate();
t.equal(output, "it generates");
});
await t.test("should validate partials", async (t) => {
const { mustache } = t.mockRequire<typeof import("../lib/mustache")>("../lib/mustache", {
"node:fs/promises": {
readFile: () => Promise.resolve("<% title %>")
}
});
const variables = {
title: "it generates"
};
const generator = mustache({
path: "path/to/file.txt",
variables,
});
const generateSpy = t.sinon.spy(generator, "generate");
const reportSpy = t.sinon.spy(generator, "report");
await generator.validate({
path: "path/to/file.txt",
found: "it generates\nand handles extras"
});
t.equal(generateSpy.callCount, 1);
// Report not called because we passed
t.equal(reportSpy.callCount, 0);
});
await t.test("should fail validation when base template not found", async (t) => {
const { mustache } = t.mockRequire<typeof import("../lib/mustache")>("../lib/mustache", {
"node:fs/promises": {
readFile: () => Promise.resolve("<% title %>")
}
});
const variables = {
title: "it generates"
};
const generator = mustache({
path: "path/to/file.txt",
variables,
});
const generateSpy = t.sinon.spy(generator, "generate");
const reportSpy = t.sinon.spy(generator, "report");
await generator.validate({
path: "path/to/file.txt",
found: "a new file"
});
t.equal(generateSpy.callCount, 1);
t.ok(reportSpy.calledOnceWithExactly({
expected: "it generates",
found: "a new file",
message: "path/to/file.txt does not include the original template"
}));
});
});