testModuleSideEffectsAreRefused

tcmts.core.test.golden.testModuleSideEffectsAreRefused · #zg7m4l6b · term

Importing a module with top-level side effects is refused, with a reason.

import { analyzeProject } from "#codebase/tcmts/core/analyze";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testModuleSideEffectsAreRefused = () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm-effects-"));
  fs.writeFileSync(path.join(dir, "package.json"), JSON.stringify({ type: "module" }));
  const file = path.join(dir, "kinds.ts");
  // skipping the loop would silently leave `names` empty, so it must be an error, not a warning
  fs.writeFileSync(file, `export const names: string[] = [];\nfor (const n of ["a", "b"]) names.push(n);\n`);
  const a = analyzeProject([file], { cacheDir: dir, resolveName: () => "" });
  assert.match(a.errors.join("\n"), /top-level ForOfStatement: tcm stores definitions, not module side effects/);
}

uses