cloud / internal-packages / installer / test / imports.mjsmain at aacbc74
// imports.mjs — the import check of the run (container/check-imports.mjs): which addresses it
// asks for an app with no build. Run: node test/imports.mjs
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { addressesOf, scriptsOf } from "../container/check-imports.mjs";
const PAGE = `<link rel="stylesheet" href="https://cdn.example/ui/style.css" />
<script type="importmap">{ "imports": { "react": "https://cdn.example/react.js", "ui/": "https://cdn.example/ui/" } }</script>`;
test("a module beside app.js and one in a folder below it are read", () => {
const folder = mkdtempSync(join(tmpdir(), "imports-"));
mkdirSync(join(folder, "parts"));
writeFileSync(join(folder, "app.js"), 'import React from "react";\nimport "./nav.js";\n');
writeFileSync(join(folder, "nav.js"), 'import { Button } from "ui/button.js";\n');
writeFileSync(join(folder, "parts/row.mjs"), 'const m = await import("ui/row.js");\n');
writeFileSync(join(folder, "notes.txt"), 'import "ui/never.js";\n');
assert.deepEqual(addressesOf(PAGE, scriptsOf(folder)).sort(), [
"https://cdn.example/react.js",
"https://cdn.example/ui/button.js",
"https://cdn.example/ui/row.js",
"https://cdn.example/ui/style.css",
]);
});
test("a name that the import map does not hold is said, not skipped", () => {
assert.ok(
addressesOf(PAGE, 'import x from "charts";').includes(
'no entry in the import map for "charts"',
),
);
});