-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
EleventyTest.js
1347 lines (1129 loc) · 41.8 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import test from "ava";
import fs from "node:fs";
import path from "node:path";
import lodash from "@11ty/lodash-custom";
import { rimrafSync } from "rimraf";
import { z } from "zod";
import { fromZodError } from "zod-validation-error";
import { marked } from "marked";
import eventBus from "../src/EventBus.js";
import Eleventy, { HtmlBasePlugin } from "../src/Eleventy.js";
import TemplateContent from "../src/TemplateContent.js";
import TemplateMap from "../src/TemplateMap.js";
import TemplateConfig from "../src/TemplateConfig.js";
import DateGitFirstAdded from "../src/Util/DateGitFirstAdded.js";
import DateGitLastUpdated from "../src/Util/DateGitLastUpdated.js";
import PathNormalizer from "../src/Util/PathNormalizer.js";
import { normalizeNewLines, localizeNewLines } from "./Util/normalizeNewLines.js";
const fsp = fs.promises;
const lodashGet = lodash.get;
test("Eleventy, defaults inherit from config", async (t) => {
let elev = new Eleventy();
let eleventyConfig = new TemplateConfig();
await eleventyConfig.init();
await elev.initializeConfig();
let config = eleventyConfig.getConfig();
t.truthy(elev.input);
t.truthy(elev.outputDir);
t.is(config.dir.input, ".");
t.is(elev.input, "./");
t.is(config.dir.output, "_site");
t.is(elev.outputDir, "./_site/");
});
test("Eleventy, null output directory should default to _site", async (t) => {
let elev = new Eleventy(".", null);
let eleventyConfig = new TemplateConfig();
await eleventyConfig.init();
await elev.initializeConfig();
let config = eleventyConfig.getConfig();
t.is(config.dir.input, ".");
t.is(elev.input, "./");
t.is(config.dir.output, "_site");
t.is(elev.outputDir, "./_site/");
});
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 (before config init)", async (t) => {
let elev = new Eleventy();
elev.setIsVerbose(true);
await elev.initializeConfig();
t.true(elev.verboseMode);
});
test("Eleventy, set is verbose (after config init)", async (t) => {
let elev = new Eleventy();
await elev.initializeConfig();
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();
let globalData = await elev.templateData.getGlobalData();
await elev.eleventyFiles.getFiles();
await elev.initWatch();
t.deepEqual(await elev.getWatchedFiles(), [
"./package.json",
"./test/stubs/**/*.njk",
"./test/stubs/_includes/**",
"./test/stubs/_data/**",
"./.gitignore",
"./.eleventyignore",
"./test/stubs/.eleventyignore",
"./.eleventy.js",
"./eleventy.config.js",
"./eleventy.config.mjs",
"./eleventy.config.cjs",
"./test/stubs/**/*.{json,11tydata.mjs,11tydata.cjs,11tydata.js}",
"./test/stubs/deps/dep1.cjs",
"./test/stubs/deps/dep2.cjs",
]);
});
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", {
config: eleventyConfig => {
eleventyConfig.setWatchJavaScriptDependencies(false);
}
});
elev.setFormats("njk");
await elev.init();
await elev.initWatch();
t.deepEqual(await elev.getWatchedFiles(), [
"./package.json",
"./test/stubs/**/*.njk",
"./test/stubs/_includes/**",
"./test/stubs/_data/**",
"./.gitignore",
"./.eleventyignore",
"./test/stubs/.eleventyignore",
"./.eleventy.js",
"./eleventy.config.js",
"./eleventy.config.mjs",
"./eleventy.config.cjs",
"./test/stubs/**/*.{json,11tydata.mjs,11tydata.cjs,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.inputFile, "./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", {
inputDir: "./test/stubs"
});
t.is(elev.input, "./test/stubs/subdir/index.html");
t.is(elev.inputFile, "./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.inputFile, "./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 (script)", async (t) => {
let previousExitCode = process.exitCode;
let elev = new Eleventy("./test/stubs/exitCode/failure.njk", "./test/stubs/exitCode/_site", {
source: "script",
});
elev.setIsVerbose(false);
elev.disableLogger();
await t.throwsAsync(async () => {
await elev.write();
});
// no change to the exit code when running script
t.is(process.exitCode, previousExitCode);
});
test("Eleventy set input/output, one file input exitCode (cli)", async (t) => {
let previousExitCode = process.exitCode;
let elev = new Eleventy("./test/stubs/exitCode/failure.njk", "./test/stubs/exitCode/_site", {
source: "cli",
});
elev.setIsVerbose(false);
elev.disableLogger();
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);
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",
rawInput: localizeNewLines("# hi\n"),
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",
rawInput: "{{ hi }}",
content: "hello",
},
]
);
});
test("Eleventy to ndjson", async (t) => {
let elev = new Eleventy("./test/stubs--to/");
elev.setIsVerbose(false);
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",
rawInput: localizeNewLines("# hi\n"),
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",
rawInput: `{{ hi }}`,
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",
rawInput: localizeNewLines("# hi\n"),
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",
rawInput: "{{ hi }}",
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();
await elev1.initializeConfig();
t.is(elev1.eleventyConfig, elev1.eleventyConfig);
t.is(elev1.config, elev1.config);
delete elev1.config.uses;
t.is(JSON.stringify(elev1.config), JSON.stringify(elev1.config));
let elev2 = new Eleventy();
await elev2.initializeConfig();
t.not(elev1.eleventyConfig, elev2.eleventyConfig);
elev1.config.benchmarkManager = null;
elev2.config.benchmarkManager = null;
delete elev2.config.uses;
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",
rawInput: localizeNewLines("# hi\n"),
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",
rawInput: `{{ hi }}`,
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",
rawInput: localizeNewLines("# hi\n"),
content: "<h1>hi</h1>\n",
},
];
let test2Result = [
{
url: "/test2/",
inputPath: "./test/stubs--to/test2.liquid",
outputPath: "./_site/test2/index.html",
rawInput: "{{ hi }}",
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-addglobaldata/", "./test/stubs-addglobaldata/_site", {
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("count", () => {
count++;
return count;
});
},
});
let results = await elev.toJSON();
t.is(count, 1);
});
test("Eleventy addGlobalData shouldn’t run if no input templates match!", async (t) => {
let count = 0;
let elev = new Eleventy(
"./test/stubs-addglobaldata-noop/",
"./test/stubs-addglobaldata-noop/_site",
{
config: function (eleventyConfig) {
eleventyConfig.addGlobalData("count", () => {
count++;
return count;
});
},
}
);
let results = await elev.toJSON();
t.is(count, 0);
});
test("Eleventy addGlobalData can feed layouts to populate data cascade with layout data, issue #1245", async (t) => {
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("DateGitLastUpdated returns undefined on nonexistent path", (t) => {
t.is(DateGitLastUpdated("./test/invalid.invalid"), undefined);
});
test("#2167: Pagination with permalink: false", async (t) => {
let elev = new Eleventy("./test/stubs-2167/", "./test/stubs-2167/_site");
elev.disableLogger();
elev.setDryRun(true);
let [,pages] = await elev.write();
t.is(pages.length, 0);
let results = await elev.toJSON();
t.is(results.length, 5);
});
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);
});
test("Liquid shortcode with multiple arguments(issue #2348)", async (t) => {
// NOTE issue #2348 was only active when you were processing multiple templates at the same time.
let elev = new Eleventy("./test/stubs-2367/", "./test/stubs-2367/_site", {
config: function (eleventyConfig) {
eleventyConfig.addShortcode("simplelink", function (...args) {
return JSON.stringify(args);
});
},
});
let arr = ["layout", "/mylayout", "layout", "/mylayout", "layout", "/mylayout"];
let str = normalizeNewLines(`${JSON.stringify(arr)}
${JSON.stringify(arr)}`);
let results = await elev.toJSON();
t.is(results.length, 2);
let content = results.map((entry) => entry.content).sort();
t.is(normalizeNewLines(content[0]), str);
t.is(normalizeNewLines(content[1]), str);
});
test("#2224: date 'git created' populates page.date", async (t) => {
let elev = new Eleventy("./test/stubs-2224/", "./test/stubs-2224/_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 = DateGitFirstAdded("./test/stubs-2224/index.njk");
t.is(result.content.trim(), "" + comparisonDate.getTime());
});
test("DateGitFirstAdded returns undefined on nonexistent path", async (t) => {
t.is(DateGitFirstAdded("./test/invalid.invalid"), undefined);
});
test("Does pathPrefix affect page URLs", async (t) => {
let elev = new Eleventy("./README.md", "./_site", {
config: function (eleventyConfig) {
return {
pathPrefix: "/testdirectory/",
};
},
});
let results = await elev.toJSON();
let [result] = results;
t.is(result.url, "/README/");
});
test("Improvements to custom template syntax APIs (includes a layout file) #2258", async (t) => {
let elev = new Eleventy("./test/stubs-2258/", "./test/stubs-2258/_site", {
configPath: "./test/stubs-2258/eleventy.config.cjs",
});
// Restore previous contents
let includeFilePath = "./test/stubs-2258/_includes/_code.scss";
let previousContents = `code {
padding: 0.25em;
line-height: 0;
}`;
let newContents = `/* New content */`;
await fsp.writeFile(includeFilePath, previousContents, { encoding: "utf8" });
let sizes = [TemplateContent._inputCache.size, TemplateContent._compileCache.size];
let results = await elev.toJSON();
t.is(results.length, 1);
t.is(
normalizeNewLines(results[0].content),
`/* Banner */
${previousContents}
/* Comment */`
);
// Cache sizes are now one bigger
t.is(sizes[0] + 1, 1);
t.is(sizes[1] + 1, 1);
let results2 = await elev.toJSON();
t.is(
normalizeNewLines(results2[0].content),
`/* Banner */
${previousContents}
/* Comment */`
);
// Cache sizes are unchanged from last build
t.is(sizes[0] + 1, 1);
t.is(sizes[1] + 1, 1);
await fsp.writeFile(includeFilePath, newContents, { encoding: "utf8" });
// Trigger that the file has changed
eventBus.emit("eleventy.resourceModified", includeFilePath);
elev.setIncrementalFile(includeFilePath);
let results3 = await elev.toJSON();
t.is(
normalizeNewLines(results3[0].content),
`/* Banner */
${newContents}
/* Comment */`
);
await fsp.writeFile(includeFilePath, previousContents, { encoding: "utf8" });
});
test("Lodash get (for pagination data target) object key with spaces, issue #2851", (t) => {
let data = {
collections: {
"tag with spaces": 2,
},
};
t.is(2, lodashGet(data, "collections['tag with spaces']"));
// wow, this works huh?
t.is(2, lodashGet(data, "collections.tag with spaces"));
let tm = new TemplateMap(new TemplateConfig());
t.is(tm.getTagTarget("collections.tag with spaces"), "tag with spaces");
t.is(tm.getTagTarget("collections['tag with spaces']"), "tag with spaces");
t.is(tm.getTagTarget('collections["tag with spaces"]'), "tag with spaces");
});
test("Eleventy tag collection with spaces in the tag name, issue #2851", async (t) => {
let elev = new Eleventy("./test/stubs-2851", "./test/stubs-2851/_site", {
config: function (eleventyConfig) {
eleventyConfig.dataFilterSelectors.add("collections");
},
});
elev.setIsVerbose(false);
let result = await elev.toJSON();
t.deepEqual(result.length, 2);
t.deepEqual(result.length, result[0].data.collections.all.length);
t.deepEqual(result[0].data.collections["tag with spaces"].length, 1);
});
test("this.eleventy on JavaScript template functions, issue #2790", async (t) => {
t.plan(3);
let elev = new Eleventy("./test/stubs-2790", "./test/stubs-2790/_site", {
config: function (eleventyConfig) {
eleventyConfig.addJavaScriptFunction("jsfunction", function () {
t.truthy(this.eleventy);
return this.eleventy.generator.split(" ")[0];
});
},
});
let result = await elev.toJSON();
t.deepEqual(result.length, 1);
t.deepEqual(result[0].content, `<p>Eleventy</p>`);
});
test("Global data JS files should only execute once, issue #2753", async (t) => {
let elev = new Eleventy("./test/stubs-2753", "./test/stubs-2753/_site", {
config: function (eleventyConfig) {},
});
let result = await elev.toJSON();
t.deepEqual(result.length, 2);
t.deepEqual(result[0].content, `1`);
t.deepEqual(result[0].content, `1`);
});
function sortResultsBy(results, key = "content") {
results.sort((a, b) => {
if(a[key] < b[key]) {
return -1;
}
if(b[key] < a[key]) {
return 1;
}
return 0;
});
}
test("Access to raw input of file (toJSON), issue #1206", async (t) => {
let elev = new Eleventy("./test/stubs-1206", "./test/stubs-1206/_site", {
config: function (eleventyConfig) {},
});
let results = await elev.toJSON();
sortResultsBy(results, "content");
t.deepEqual(results.length, 2);
t.deepEqual(results[0].content, `This is the first template.This is the first template.{{ page.rawInput }}`);
t.deepEqual(results[0].rawInput, `This is the first template.{{ page.rawInput }}`);
t.deepEqual(results[1].content, `This is the second template.This is the first template.{{ page.rawInput }}`);
t.deepEqual(results[1].rawInput, `This is the second template.{{ collections.tag1[0].rawInput }}`);
});
// Warning: this test writes to the file system
test("Access to raw input of file (dryRun), issue #1206", async (t) => {
let elev = new Eleventy("./test/stubs-1206", "./test/stubs-1206/_site", {
config: function (eleventyConfig) {},
});
elev.disableLogger();
let [,results] = await elev.write();
sortResultsBy(results, "content");
t.deepEqual(results.length, 2);
t.deepEqual(results[0].content, `This is the first template.This is the first template.{{ page.rawInput }}`);
t.deepEqual(results[0].rawInput, `This is the first template.{{ page.rawInput }}`);
t.deepEqual(results[1].content, `This is the second template.This is the first template.{{ page.rawInput }}`);
t.deepEqual(results[1].rawInput, `This is the second template.{{ collections.tag1[0].rawInput }}`);
rimrafSync("./test/stubs-1206/_site/");
});
test("eleventy.before and eleventy.after Event Arguments, directories", async (t) => {
t.plan(6);
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
config: function (eleventyConfig) {
eleventyConfig.on("eleventy.before", arg => {
t.is(arg.inputDir, "./test/noop/");
t.is(arg.directories.input, "./test/noop/");
t.is(arg.directories.includes, "./test/noop/_includes/");
})
eleventyConfig.on("eleventy.after", arg => {
t.is(arg.inputDir, "./test/noop/");
t.is(arg.directories.input, "./test/noop/");
t.is(arg.directories.includes, "./test/noop/_includes/");
})
},
});
let results = await elev.toJSON();
});
test("setInputDirectory config method #1503", async (t) => {
t.plan(5);
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
config: function (eleventyConfig) {
eleventyConfig.setInputDirectory("./test/noop2/");
eleventyConfig.on("eleventy.before", arg => {
t.is(arg.directories.input, "./test/noop2/");
t.is(arg.directories.includes, "./test/noop2/_includes/");
t.is(arg.directories.data, "./test/noop2/_data/");
t.is(arg.directories.layouts, undefined);
t.is(arg.directories.output, "./test/noop/_site/");
})
},
});
let results = await elev.toJSON();
});
test("setIncludesDirectory config method #1503", async (t) => {
t.plan(5);
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
config: function (eleventyConfig) {
eleventyConfig.setIncludesDirectory("myincludes");
eleventyConfig.on("eleventy.before", arg => {
t.is(arg.directories.input, "./test/noop/");
t.is(arg.directories.includes, "./test/noop/myincludes/");
t.is(arg.directories.data, "./test/noop/_data/");
t.is(arg.directories.layouts, undefined);
t.is(arg.directories.output, "./test/noop/_site/");
})
},
});
let results = await elev.toJSON();
});
test("setDataDirectory config method #1503", async (t) => {
t.plan(5);
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
config: function (eleventyConfig) {
eleventyConfig.setDataDirectory("data");
eleventyConfig.on("eleventy.before", arg => {
t.is(arg.directories.input, "./test/noop/");
t.is(arg.directories.includes, "./test/noop/_includes/");
t.is(arg.directories.data, "./test/noop/data/");
t.is(arg.directories.layouts, undefined);
t.is(arg.directories.output, "./test/noop/_site/");
})
},
});
let results = await elev.toJSON();
});
test("setLayoutsDirectory config method #1503", async (t) => {
t.plan(5);
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
config: function (eleventyConfig) {
eleventyConfig.setLayoutsDirectory("layouts");
eleventyConfig.on("eleventy.before", arg => {
t.is(arg.directories.input, "./test/noop/");
t.is(arg.directories.includes, "./test/noop/_includes/");
t.is(arg.directories.data, "./test/noop/_data/");
t.is(arg.directories.layouts, "./test/noop/layouts/");
t.is(arg.directories.output, "./test/noop/_site/");
})
},
});
let results = await elev.toJSON();
});
test("setInputDirectory config method #1503 in a plugin throws error", async (t) => {
let elev = new Eleventy("./test/noop/", "./test/noop/_site", {
config: function (eleventyConfig) {
eleventyConfig.addPlugin(() => {
eleventyConfig.setInputDirectory("./test/noop2/");
});
},
});
await t.throwsAsync(() => elev.toJSON(), {
// The `set*Directory` configuration API methods are not yet allowed in plugins.
message: "Error processing a plugin",
});
});
test("Accepts absolute paths for input and output", async (t) => {
let input = path.resolve("./test/noop/");
let output = path.resolve("./test/noop/_site");
let elev = new Eleventy(input, output);
let results = await elev.toJSON();
// trailing slashes are expected
t.is(PathNormalizer.normalizeSeperator(elev.directories.input), PathNormalizer.normalizeSeperator(path.resolve("./test/noop/") + path.sep));
t.is(PathNormalizer.normalizeSeperator(elev.directories.includes), PathNormalizer.normalizeSeperator(path.resolve("./test/noop/_includes/") + path.sep));
t.is(PathNormalizer.normalizeSeperator(elev.directories.data), PathNormalizer.normalizeSeperator(path.resolve("./test/noop/_data/") + path.sep));
t.is(elev.directories.layouts, undefined);
t.is(PathNormalizer.normalizeSeperator(elev.directories.output), PathNormalizer.normalizeSeperator(path.resolve("./test/noop/_site/") + path.sep));
});
test("Eleventy config export (ESM)", async (t) => {
t.plan(5);
let elev = new Eleventy("test/stubs/cfg-directories-export", null, {
configPath: "./test/stubs/cfg-directories-export/eleventy.config.js",
config: function (eleventyConfig) {
eleventyConfig.on("eleventy.after", arg => {
t.is(arg.directories.input, "./src/");
t.is(arg.directories.includes, "./src/myincludes/");
t.is(arg.directories.data, "./src/mydata/");
t.is(arg.directories.layouts, undefined);
t.is(arg.directories.output, "./dist/");
})
},
});
let result = await elev.toJSON();
});
test("Eleventy config export (CommonJS)", async (t) => {
t.plan(5);
let elev = new Eleventy("test/stubs/cfg-directories-export-cjs", null, {
configPath: "./test/stubs/cfg-directories-export-cjs/eleventy.config.cjs",
config: function (eleventyConfig) {
eleventyConfig.on("eleventy.after", arg => {
t.is(arg.directories.input, "./src/");
t.is(arg.directories.includes, "./src/myincludes2/");
t.is(arg.directories.data, "./src/mydata2/");
t.is(arg.directories.layouts, undefined);
t.is(arg.directories.output, "./dist2/");
})
},
});
let result = await elev.toJSON();
});
test("Eleventy setting reserved data throws error (eleventy)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addTemplate("index.html", `---
eleventy:
key1: NOOOOO
---`);
}
});
elev.disableLogger();
let e = await t.throwsAsync(() => elev.toJSON(), {
message: 'You attempted to set one of Eleventy’s reserved data property names. You can opt-out of this behavior with `eleventyConfig.setFreezeReservedData(false)` or rename/remove the property in your data cascade that conflicts with Eleventy’s reserved property names (e.g. `eleventy`, `pkg`, and others). Learn more: https://www.11ty.dev/docs/data-eleventy-supplied/'
});