UNPKG

@arizeai/phoenix-client

Version:
81 lines 3.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createTestApi = createTestApi; const runner_1 = require("./runner"); const types_1 = require("./types"); /** * Build the public `describe`/`test`/`it` API for a runner adapter. * * Both the jest and vitest entrypoints expose the identical surface; the only * thing that differs between them is how the {@link RunnerHooks} are obtained * (vitest imports them statically, jest resolves them lazily from globals). * That difference is captured by `getHooks`, which is invoked once per * declaration so adapters are free to resolve hooks lazily. * * The JSDoc that surfaces in editors lives on the {@link PhoenixDescribe} and * {@link PhoenixTest} interfaces rather than the implementations below, so the * docs survive the `export const { describe, test, it } = createTestApi(...)` * destructuring in each adapter. */ function createTestApi(getHooks) { const describe = ((name, fn, config) => { (0, runner_1.declareDescribe)(getHooks(), name, fn, config !== null && config !== void 0 ? config : {}); }); describe.only = (name, fn, config) => { (0, runner_1.declareDescribe)(getHooks(), name, fn, config !== null && config !== void 0 ? config : {}, "only"); }; describe.skip = (name, fn, config) => { (0, runner_1.declareDescribe)(getHooks(), name, fn, config !== null && config !== void 0 ? config : {}, "skip"); }; const test = ((name, params, fn, timeout) => { (0, runner_1.declareTest)(getHooks(), name, params, fn, "default", timeout); }); test.only = (name, params, fn, timeout) => { (0, runner_1.declareTest)(getHooks(), name, params, fn, "only", timeout); }; test.skip = (name, params, fn, timeout) => { (0, runner_1.declareTest)(getHooks(), name, params, fn, "skip", timeout); }; test.each = (table) => { return (name, fn, timeout) => { table.forEach((row, i) => { const testName = typeof name === "function" ? name(row, i) : interpolateName(name, row, i); (0, runner_1.declareTest)(getHooks(), testName, { id: row.id, input: row.input, expected: (0, types_1.resolveReference)(row), metadata: row.metadata, splits: row.splits, repetitions: row.repetitions, dryRun: row.dryRun, }, fn, "default", timeout); }); }; }; // `it` is the canonical alias for `test`. const it = test; return { describe, test, it }; } /** * Interpolate a `test.each` name template for a single row. Supports the * common `%i`/`%s`/`%j` placeholders for surface parity with the underlying * runners; when no placeholder is present the 1-based row index is appended. */ function interpolateName(name, row, index) { if (!name.includes("%")) { return `${name} #${index + 1}`; } const replacements = [ [/%i/g, String(index)], [/%s/g, JSON.stringify(row.input)], [/%j/g, JSON.stringify(row)], ]; let out = name; for (const [pattern, value] of replacements) { out = out.replace(pattern, value); } return out; } //# sourceMappingURL=define-api.js.map