UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

52 lines (51 loc) 1.86 kB
import { io } from "../../io/module.js"; import { loadModuleMap } from "../module.f.js"; import { isTest, parseTestSet } from "./module.f.js"; import * as nodeTest from 'node:test'; const isBun = typeof Bun !== 'undefined'; const isPlaywright = typeof process !== 'undefined' && process?.env?.PLAYWRIGHT_TEST !== undefined; const createFramework = (fw) => (prefix, f) => fw.test(prefix, t => f((name, v) => t.test(name, v))); // Bun doesn't support nested tests yet. const createBunFramework = (fw) => (prefix, f) => f((name, v) => fw.test(`${prefix}: ${name}`, v)); const createPlaywrihtFramework = async () => { const pwTest = (await import('@playwright/test')).test; return (prefix, f) => f((name, v) => pwTest(`${prefix}: ${name}`, v)); }; const framework = isPlaywright ? await createPlaywrihtFramework() : isBun ? createBunFramework(nodeTest) : createFramework(nodeTest); const parse = parseTestSet(io.tryCatch); const scanModule = (x) => async (subTestRunner) => { let subTests = [x]; while (true) { const [first, ...rest] = subTests; if (first === undefined) { break; } subTests = rest; // const [name, value] = first; const set = parse(value); if (typeof set === 'function') { await subTestRunner(name, () => { const r = set(); subTests = [...subTests, [`${name}()`, r]]; }); } else { for (const [j, y] of set) { const pr = `${name}/${j}`; subTests = [...subTests, [pr, y]]; } } } }; const run = async () => { const x = await loadModuleMap(io); for (const [i, v] of Object.entries(x)) { if (isTest(i)) { framework(i, scanModule(['', v.default])); } } }; await run();