testUnreadComputedConstantsKeepResults

tcmts.cli.test.e2e.suites.testUnreadComputedConstantsKeepResults · #kqhl4fle · term

A constant whose value is computed doing nothing else counts for a suite only when code it ran refers to it: a change to one the command loads and doesn't read (its help) keeps the result, and a change to one it reads runs the suite again.

import { okIn, tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testUnreadComputedConstantsKeepResults = () => {
  const dir = tempDir("suites");
  const ok = (...args: string[]) => okIn(dir, ...args);
  const scratch = (ns: string, text: string) =>
    fs.writeFileSync(path.join(dir, "scratch.ts"), `// tcm: namespace ${ns}\n${text}`);
  ok("init");
  scratch(
    "greeter",
    `export const HELP = ["usage: greet [name]", "says hello"].join("\\n");
export const GREETING = ["hel", "lo"].join("");
export const help = () => console.log(HELP);
export const main = (...args: string[]) =>
  args[0] === "--help" ? help() : console.log(\`\${GREETING}, \${args[0] ?? "world"}\`);
export const manifest = { name: "greeter", bin: { greet: "main" }, suites: { greet: ["e2e"] } };
`,
  );
  ok("add");
  scratch(
    "greeter.e2e",
    `import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
export const testGreets = () => {
  const r = spawnSync(process.execPath, [process.env.GREET_BIN!, "tcm"], { encoding: "utf8" });
  assert.equal(r.stdout, "hello, tcm\\n");
};
`,
  );
  ok("add");
  assert.match(ok("test"), /suites: 1 passed, 0 failed\n/);

  // the help is loaded with the command, computed, and never read by the test
  scratch(
    "greeter",
    `export const HELP = ["usage: greet [name]", "says hello to someone"].join("\\n");\n`,
  );
  ok("update");
  assert.match(ok("test"), /suites: 1 passed, 0 failed \(1 cached\)/);

  // the greeting is read
  scratch("greeter", `export const GREETING = ["h", "ello"].join("");\n`);
  ok("update");
  const again = ok("test");
  assert.match(again, /suites: 1 passed, 0 failed\n/);
  assert.doesNotMatch(again, /cached/);
}

uses