testImportFollowsPackageNamespaceReexports

tcmts.cli.test.e2e.import.testImportFollowsPackageNamespaceReexports · #qoaf22s6 · term

a package's namespace re-exported by a project module (export * as v from "vitest") is imported as the package's

import { installedPackage, 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 testImportFollowsPackageNamespaceReexports = () => {
  const dir = tempDir("import");
  const lib = tempDir("import reexports");
  const tcm = (...args: string[]) => okIn(dir, ...args);
  const write = (file: string, text: string) => {
    fs.mkdirSync(path.dirname(path.join(lib, file)), { recursive: true });
    fs.writeFileSync(path.join(lib, file), text);
  };
  write(
    "tsconfig.json",
    JSON.stringify({
      compilerOptions: {
        strict: true,
        module: "ESNext",
        moduleResolution: "bundler",
        noEmit: true,
      },
    }),
  );
  // vitest installed in the library (here, linked to the one tcm's tests use)
  fs.mkdirSync(path.join(lib, "node_modules"), { recursive: true });
  fs.symlinkSync(
    fs.realpathSync(installedPackage("vitest")),
    path.join(lib, "node_modules", "vitest"),
    "dir",
  );
  write("src/ns.ts", `export * as v from "vitest";\n`);
  write(
    "src/use.ts",
    `import { v } from "./ns";
import * as m from "./ns";
export const check = (n: number): void => {
  v.expect(n).toBe(1);
  m.v.expect(n).toBe(1);
};
export type Fn = v.Mock | m.v.Mock;
`,
  );
  tcm("init");
  assert.match(tcm("import", path.join(lib, "src"), "--into", "lib"), /\+ new/);
  // both spellings are the package's `expect` (rendered with the local names they were written with)
  assert.match(tcm("deps", "lib.use.check").trim(), /^npm:vitest@\d+#expect$/);
  assert.match(tcm("deps", "lib.use.Fn").trim(), /^npm:vitest@\d+#Mock \(type\)$/);
  assert.match(tcm("view", "lib.use.check"), /import \{[^}]*\bexpect\b[^}]*\} from "vitest";/);
  assert.match(tcm("verify"), /verified \d+ definitions: rendering and re-hashing is the identity/);
}

uses