-
-
Notifications
You must be signed in to change notification settings - Fork 498
/
EleventyTest.js
502 lines (418 loc) · 13.6 KB
/
EleventyTest.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
const test = require("ava");
const Eleventy = require("../src/Eleventy");
const EleventyWatchTargets = require("../src/EleventyWatchTargets");
const TemplateConfig = require("../src/TemplateConfig");
const DateGitLastUpdated = require("../src/Util/DateGitLastUpdated");
test("Eleventy, defaults inherit from config", async (t) => {
let elev = new Eleventy();
let config = new TemplateConfig().getConfig();
t.truthy(elev.input);
t.truthy(elev.outputDir);
t.is(elev.input, config.dir.input);
t.is(elev.outputDir, config.dir.output);
});
test("Eleventy, get version", (t) => {
let elev = new Eleventy();
t.truthy(elev.getVersion());
});
test("Eleventy, get help", (t) => {
let elev = new Eleventy();
t.truthy(elev.getHelp());
});
test("Eleventy, set is verbose", (t) => {
let elev = new Eleventy();
elev.setIsVerbose(true);
t.true(elev.verboseMode);
});
test("Eleventy set input/output", async (t) => {
let elev = new Eleventy("./test/stubs", "./test/stubs/_site");
t.is(elev.input, "./test/stubs");
t.is(elev.outputDir, "./test/stubs/_site");
await elev.init();
t.truthy(elev.templateData);
t.truthy(elev.writer);
});
test("Eleventy process.ENV", async (t) => {
delete process.env.ELEVENTY_ROOT;
t.falsy(process.env.ELEVENTY_ROOT);
let elev = new Eleventy("./test/stubs", "./test/stubs/_site");
await elev.init();
t.truthy(process.env.ELEVENTY_ROOT);
// all ELEVENTY_ env variables are also available on eleventy.env
let globals = await elev.templateData.getInitialGlobalData();
t.truthy(globals.eleventy.env.root);
});
test("Eleventy file watching", async (t) => {
let elev = new Eleventy("./test/stubs", "./test/stubs/_site");
elev.setFormats("njk");
await elev.init();
await elev.eleventyFiles.getFiles();
await elev.initWatch();
t.deepEqual(await elev.getWatchedFiles(), [
"./test/stubs/**/*.njk",
"./test/stubs/_includes/**",
"./test/stubs/_data/**",
"./.eleventy.js",
"./test/stubs/**/*.json",
"./test/stubs/**/*.11tydata.cjs",
"./test/stubs/**/*.11tydata.js",
"./test/stubs/deps/dep1.js",
"./test/stubs/deps/dep2.js",
]);
});
test("Eleventy file watching (don’t watch deps of passthrough copy .js files)", async (t) => {
let elev = new Eleventy("./test/stubs-1325", "./test/stubs-1325/_site");
elev.setFormats("11ty.js,js");
await elev.init();
await elev.eleventyFiles.getFiles();
await elev.initWatch();
t.deepEqual(await elev.eleventyFiles.getWatchPathCache(), [
"./test/stubs-1325/test.11ty.js",
]);
});
test("Eleventy file watching (no JS dependencies)", async (t) => {
let elev = new Eleventy("./test/stubs", "./test/stubs/_site");
elev.setFormats("njk");
let wt = new EleventyWatchTargets();
wt.watchJavaScriptDependencies = false;
elev.setWatchTargets(wt);
await elev.init();
await elev.initWatch();
t.deepEqual(await elev.getWatchedFiles(), [
"./test/stubs/**/*.njk",
"./test/stubs/_includes/**",
"./test/stubs/_data/**",
"./.eleventy.js",
"./test/stubs/**/*.json",
"./test/stubs/**/*.11tydata.cjs",
"./test/stubs/**/*.11tydata.js",
]);
});
test("Eleventy set input/output, one file input", async (t) => {
let elev = new Eleventy("./test/stubs/index.html", "./test/stubs/_site");
t.is(elev.input, "./test/stubs/index.html");
t.is(elev.inputDir, "./test/stubs");
t.is(elev.outputDir, "./test/stubs/_site");
});
test("Eleventy set input/output, one file input, deeper subdirectory", async (t) => {
let elev = new Eleventy(
"./test/stubs/subdir/index.html",
"./test/stubs/_site"
);
elev.setInputDir("./test/stubs");
t.is(elev.input, "./test/stubs/subdir/index.html");
t.is(elev.inputDir, "./test/stubs");
t.is(elev.outputDir, "./test/stubs/_site");
});
test("Eleventy set input/output, one file input root dir", async (t) => {
let elev = new Eleventy("./README.md", "./test/stubs/_site");
t.is(elev.input, "./README.md");
t.is(elev.inputDir, ".");
t.is(elev.outputDir, "./test/stubs/_site");
});
test("Eleventy set input/output, one file input root dir without leading dot/slash", async (t) => {
let elev = new Eleventy("README.md", "./test/stubs/_site");
t.is(elev.input, "README.md");
t.is(elev.inputDir, ".");
t.is(elev.outputDir, "./test/stubs/_site");
});
test("Eleventy set input/output, one file input exitCode", async (t) => {
let previousExitCode = process.exitCode;
let elev = new Eleventy(
"./test/stubs/exitCode/failure.njk",
"./test/stubs/exitCode/_site"
);
elev.setIsVerbose(false);
elev.disableLogger();
// await elev.init(); // no longer necessary
await elev.write();
t.is(process.exitCode, 1);
process.exitCode = previousExitCode;
});
test("Eleventy to json", async (t) => {
let elev = new Eleventy("./test/stubs--to/");
elev.setIsVerbose(false);
// await elev.init(); // no longer necessary
let result = await elev.toJSON();
t.deepEqual(
result.filter((entry) => entry.url === "/test/"),
[
{
url: "/test/",
inputPath: "./test/stubs--to/test.md",
outputPath: "_site/test/index.html",
content: "<h1>hi</h1>\n",
},
]
);
t.deepEqual(
result.filter((entry) => entry.url === "/test2/"),
[
{
url: "/test2/",
inputPath: "./test/stubs--to/test2.liquid",
outputPath: "_site/test2/index.html",
content: "hello",
},
]
);
});
test("Eleventy to ndjson", async (t) => {
let elev = new Eleventy("./test/stubs--to/");
elev.setIsVerbose(false);
// await elev.init(); // no longer necessary
let stream = await elev.toNDJSON();
let count = 0;
await new Promise((resolve) => {
stream.on("data", function (buf) {
count++;
let jsonObj = JSON.parse(buf.toString());
if (jsonObj.url === "/test/") {
t.deepEqual(jsonObj, {
url: "/test/",
inputPath: "./test/stubs--to/test.md",
outputPath: "_site/test/index.html",
content: "<h1>hi</h1>\n",
});
}
if (jsonObj.url === "/test2/") {
t.deepEqual(jsonObj, {
url: "/test2/",
inputPath: "./test/stubs--to/test2.liquid",
outputPath: "_site/test2/index.html",
content: "hello",
});
}
if (count >= 2) {
resolve();
}
});
});
});
test("Eleventy to ndjson (returns a stream)", async (t) => {
let elev = new Eleventy("./test/stubs--to/");
elev.setIsVerbose(false);
let stream = await elev.toNDJSON();
await new Promise((resolve) => {
let results = [];
stream.on("data", function (entry) {
let jsonObj = JSON.parse(entry);
if (jsonObj.url === "/test/") {
t.deepEqual(jsonObj, {
url: "/test/",
inputPath: "./test/stubs--to/test.md",
outputPath: "_site/test/index.html",
content: "<h1>hi</h1>\n",
});
}
if (jsonObj.url === "/test2/") {
t.deepEqual(jsonObj, {
url: "/test2/",
inputPath: "./test/stubs--to/test2.liquid",
outputPath: "_site/test2/index.html",
content: "hello",
});
}
results.push(jsonObj);
if (results.length >= 2) {
resolve();
}
});
});
});
test("Two Eleventies, two configs!!! (config used to be a global)", async (t) => {
let elev1 = new Eleventy();
t.is(elev1.eleventyConfig, elev1.eleventyConfig);
t.is(elev1.config, elev1.config);
t.is(JSON.stringify(elev1.config), JSON.stringify(elev1.config));
let elev2 = new Eleventy();
t.not(elev1.eleventyConfig, elev2.eleventyConfig);
elev1.config.benchmarkManager = null;
elev2.config.benchmarkManager = null;
t.is(JSON.stringify(elev1.config), JSON.stringify(elev2.config));
});
test("Config propagates to other instances correctly", async (t) => {
let elev = new Eleventy();
await elev.init();
t.is(elev.eleventyServe.config, elev.config);
t.is(elev.extensionMap.eleventyConfig, elev.eleventyConfig);
t.is(elev.eleventyFiles.eleventyConfig, elev.eleventyConfig);
t.is(elev.templateData.eleventyConfig, elev.eleventyConfig);
t.is(elev.writer.eleventyConfig, elev.eleventyConfig);
});
test("Eleventy programmatic API without init", async (t) => {
let elev = new Eleventy("./test/stubs--to/");
elev.setIsVerbose(false);
let result = await elev.toJSON();
t.deepEqual(
result.filter((entry) => entry.url === "/test/"),
[
{
url: "/test/",
inputPath: "./test/stubs--to/test.md",
outputPath: "_site/test/index.html",
content: "<h1>hi</h1>\n",
},
]
);
t.deepEqual(
result.filter((entry) => entry.url === "/test2/"),
[
{
url: "/test2/",
inputPath: "./test/stubs--to/test2.liquid",
outputPath: "_site/test2/index.html",
content: "hello",
},
]
);
});
test("Can Eleventy run two executeBuilds in parallel?", async (t) => {
let elev = new Eleventy("./test/stubs--to/");
elev.setIsVerbose(false);
let p1 = elev.toJSON();
let p2 = elev.toJSON();
let [result1, result2] = await Promise.all([p1, p2]);
let test1Result = [
{
url: "/test/",
inputPath: "./test/stubs--to/test.md",
outputPath: "_site/test/index.html",
content: "<h1>hi</h1>\n",
},
];
let test2Result = [
{
url: "/test2/",
inputPath: "./test/stubs--to/test2.liquid",
outputPath: "_site/test2/index.html",
content: "hello",
},
];
t.deepEqual(
result1.filter((entry) => entry.url === "/test/"),
test1Result
);
t.deepEqual(
result1.filter((entry) => entry.url === "/test2/"),
test2Result
);
t.deepEqual(
result2.filter((entry) => entry.url === "/test/"),
test1Result
);
t.deepEqual(
result2.filter((entry) => entry.url === "/test2/"),
test2Result
);
});
test("Eleventy addGlobalData should run once", async (t) => {
let count = 0;
let elev = new Eleventy("./test/stubs-noop/", "./test/stubs-noop/_site", {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("count", () => {
count++;
return count;
});
},
});
let results = await elev.toJSON();
t.is(count, 1);
});
test("Eleventy addGlobalData can feed layouts to populate data cascade with layout data, issue #1245", async (t) => {
let count = 0;
let elev = new Eleventy("./test/stubs-2145/", "./test/stubs-2145/_site", {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("layout", () => "layout.njk");
eleventyConfig.dataFilterSelectors.add("LayoutData");
},
});
let [result] = await elev.toJSON();
t.deepEqual(result.data, { LayoutData: 123 });
t.is(result.content.trim(), "FromLayoutlayout.njk");
});
test("Unicode in front matter `tags`, issue #670", async (t) => {
let elev = new Eleventy("./test/stubs-670/", "./test/stubs-670/_site");
let results = await elev.toJSON();
results.sort((a, b) => {
if (a.inputPath > b.inputPath) {
return -1;
}
return 1;
});
t.is(results[0].content.trim(), "2,all,Cañon City,");
});
test("#142: date 'git Last Modified' populates page.date", async (t) => {
let elev = new Eleventy("./test/stubs-142/", "./test/stubs-142/_site");
let results = await elev.toJSON();
let [result] = results;
// This doesn’t test the validity of the function, only that it populates page.date.
let comparisonDate = DateGitLastUpdated("./test/stubs-142/index.njk");
t.is(result.content.trim(), "" + comparisonDate.getTime());
});
test("#2167: Pagination with permalink: false", async (t) => {
let elev = new Eleventy("./test/stubs-2167/", "./test/stubs-2167/_site");
elev.setDryRun(true);
let [passthroughCopy, pages] = await elev.write();
t.is(pages.length, 5);
for (let j = 0, k = pages.length; j < k; j++) {
// falsy if not writeable or is not renderable
t.is(pages[j], undefined);
}
});
test("Pagination over collection using eleventyComputed (liquid)", async (t) => {
t.plan(5);
let elev = new Eleventy(
"./test/stubs-pagination-computed-quotes/",
"./test/stubs-pagination-computed-quotes/_site",
{
config: function (eleventyConfig) {
eleventyConfig.addFilter("selectRandomFromArray", (arr) => {
t.true(Array.isArray(arr));
t.deepEqual(arr, ["The person that shared this is awesome"]);
return arr[0];
});
},
}
);
let results = await elev.toJSON();
t.is(results.length, 2);
let content = results.map((entry) => entry.content).sort();
t.is(content[0], "No");
t.is(content[1], "The person that shared this is awesome");
});
test("Pagination over collection using eleventyComputed (njk)", async (t) => {
t.plan(5);
let elev = new Eleventy(
"./test/stubs-pagination-computed-quotes-njk/",
"./test/stubs-pagination-computed-quotes-njk/_site",
{
config: function (eleventyConfig) {
eleventyConfig.addFilter("selectRandomFromArray", (arr) => {
t.true(Array.isArray(arr));
t.deepEqual(arr, ["The person that shared this is awesome"]);
return arr[0];
});
},
}
);
let results = await elev.toJSON();
t.is(results.length, 2);
let content = results.map((entry) => entry.content).sort();
t.is(content[0], "No");
t.is(content[1], "The person that shared this is awesome");
});
test("Paginated template uses proxy and global data", async (t) => {
let elev = new Eleventy(
"./test/proxy-pagination-globaldata/",
"./test/proxy-pagination-globaldata/_site",
{
config: function (eleventyConfig) {},
}
);
let results = await elev.toJSON();
let allContentMatches = results.filter((entry) => {
return entry.content.trim() === "BANNER TEXT";
});
t.is(results.length, allContentMatches.length);
});