vitestModule

tcmts.cli.vitest.vitestModule · #4hlgv7qw · term · runs on the server

uses node:path

One generated Vitest file: each test definition as a test(), named by its hash.

import { TEST_TIMEOUT_MS } from "#codebase/tcmts/cli/vitest";
import type { TestEntry } from "#codebase/tcmts/cli/vitest";
import path from "node:path";

export function vitestModule(
  dir: string,
  cacheDir: string,
  vitestImport: string,
  tests: TestEntry[],
): string {
  const rel = path.relative(dir, cacheDir).split(path.sep).join("/");
  const lines = [`import { test } from ${JSON.stringify(vitestImport)};`];
  tests.forEach((t, i) =>
    lines.push(`import { ${t.export} as t${i} } from ${JSON.stringify(`${rel}/${t.module}`)};`),
  );
  tests.forEach((t, i) =>
    lines.push(
      `test(${JSON.stringify(t.hash)}, async () => {`,
      `  if (typeof t${i} !== "function") throw new Error("a test must be a function");`,
      `  if ((await t${i}()) === false) throw new Error("returned false");`,
      `}, ${TEST_TIMEOUT_MS});`,
    ),
  );
  return lines.join("\n") + "\n";
}

uses

used by