"use strict"; import path = require("path"); import { main, __parseArgs as parseArgs, __NOW, __normalizeArgPaths as normalizeArgPaths, } from "../src/main"; const inFile = "./test/data/index-template.html"; const inFileHTML5 = "./test/data/index-html5-template.html"; const inFileSubdir = "./test/data/subdir/index-template.html"; const JS_ASSET_ALERT = "./test/data/assets/alert.js"; const JS_ASSET_ALERT_HASH = "6yOVKodXSr6FwCnpf6HWhXZl2w"; // original: "6yOVKod/XSr6FwCnpf6HWhXZl2w="; const MJS_ASSET_ANSWER = "./test/data/assets/answer.mjs"; const MJS_ASSET_ANSWER_HASH = "M2lyTaM7ZJXfwxvnwmsz2vvbk"; // original: "M2lyTaM7ZJXfwxvnwmsz2v+v+bk="; const ICO_ASSET_GITHUB = "./test/data/assets/github.ico"; const ICO_ASSET_GITHUB_HASH = "B1oibWKIpml3j2X37M3ZZ3CSU"; // original: "B+1oi+bWKIpml3j2X37M3ZZ3CSU="; const CSS_ASSET_RESET = "./test/data/assets/reset.css"; const CSS_ASSET_RESET_HASH = "D3C2hyvo84Oc7dvX3WUThJ6oUl4"; // original: "D3C2hyvo84Oc7dvX3WUThJ6oUl4="; let output = ""; function write(_: string, content: string) { output = content; } afterEach(() => (output = "")); function mainTest(args: string[]) { // Disable hashing for basic tests args.push("--stamp", "none"); return main(args, write); } function scriptHtml(script: string) { return `
`; } describe("base", () => { it("should noop when no assets", () => { expect(mainTest(["--out", "index.html", "--html", inFile])).toBe(0); expect(output).toBe(""); }); it("should noop when no assets with html5 template", () => { expect(mainTest(["--out", "index.html", "--html", inFileHTML5])).toBe(0); expect(output).toBe( "" ); }); it("should normalize asset paths", () => { mainTest([ "--out", "index.html", "--html", inFile, "--assets", "./b/../a.js", "./././b.js", ]); expect(output).toBe( '' ); }); it("should throw when --strict and unknown asset types", () => { expect(() => mainTest([ "--out", "index.html", "--html", inFile, "--strict", "--assets", "foo", ]) ).toThrow(); expect(() => mainTest([ "--out", "index.html", "--html", inFile, "--strict", "--assets", "foo.bar", ]) ).toThrow(); expect(() => mainTest([ "--out", "index.html", "--html", inFile, "--strict", "--assets", "foo.js.bar", ]) ).toThrow(); expect(() => mainTest([ "--out", "index.html", "--html", inFile, "--strict", "--assets", "foo.js", "foo.ts", ]) ).toThrow(); expect(() => mainTest([ "--out", "index.html", "--html", inFile, "--strict", "--assets", "foo.js", "foo.d.ts", ]) ).toThrow(); }); it("should not throw when non --strict and unknown asset types", () => { // Spy to avaoid output during tests spyOn(console, "warn"); expect(() => mainTest(["--out", "index.html", "--html", inFile, "--assets", "foo"]) ).not.toThrow(); expect(() => mainTest(["--out", "index.html", "--html", inFile, "--assets", "foo.bar"]) ).not.toThrow(); expect(() => mainTest([ "--out", "index.html", "--html", inFile, "--assets", "foo.js.bar", ]) ).not.toThrow(); expect(() => mainTest([ "--out", "index.html", "--html", inFile, "--assets", "foo.js", "foo.ts", ]) ).not.toThrow(); expect(() => mainTest([ "--out", "index.html", "--html", inFile, "--assets", "foo.js", "foo.d.ts", ]) ).not.toThrow(); }); it("should warn non --strict and unknown asset types", () => { const spy = spyOn(console, "warn"); mainTest([ "--out", "index.html", "--html", inFile, // "--quiet", "--assets", "foo.x", "foo.y", ]); expect(spy).toHaveBeenCalledTimes(2); expect(spy.calls.argsFor(0)[0]).toContain(`Unknown asset usage: foo.x`); expect(spy.calls.argsFor(1)[0]).toContain(`Unknown asset usage: foo.y`); }); it("should not warn when --quiet and sourcemap files for .js files found", () => { const spy = spyOn(console, "warn"); mainTest([ "--out", "index.html", "--html", inFile, "--quiet", "--assets", "foo.js", "foo.js.map", ]); expect(spy).not.toHaveBeenCalled(); }); }); describe("--assets", () => { describe("js assets", () => { it("should inject js assets as