testWatcherSaysImportsWithoutWritingThem

tcmts.cli.test.e2e.watch.testWatcherSaysImportsWithoutWritingThem · #r6pnuvtn · term

The watcher says what a file uses and doesn't import, and leaves the file as it is: it's open in an editor, being written (tcm check and saving write the imports).

import { plainEnv, strip, tcmBin, withChildren } from "#codebase/tcmts/cli/test/e2e/harness";
import { setUp, waitFor } from "#codebase/tcmts/cli/test/e2e/watch";
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

export const testWatcherSaysImportsWithoutWritingThem = () =>
  withChildren(async (keep) => {
    const { dir, tcm, scratch } = setUp();
    scratch(
      "scratch.ts",
      `// tcm: namespace lib\nexport const greet = (name: string): string => "hi " + name;\n`,
    );
    tcm("add");
    fs.rmSync(path.join(dir, "scratch.ts"));
    const text = `// tcm: namespace app\nexport const hello = (): string => greet("ada");\n`;
    scratch("app.ts", text);
    const child = keep(spawn(process.execPath, [tcmBin(), "watch"], { cwd: dir, env: plainEnv() }));
    let out = "";
    const plain = (d: string) => (out += strip(d));
    child.stdout.setEncoding("utf8").on("data", plain);
    child.stderr.setEncoding("utf8").on("data", plain);
    await waitFor(() => /not imported: import \{ greet \} from "#codebase\/lib";/.test(out));
    assert.equal(fs.readFileSync(path.join(dir, "app.ts"), "utf8"), text);
  })

uses