UNPKG

@augment-vir/test

Version:

A universal testing suite that works with Mocha style test runners _and_ Node.js's built-in test runner.

128 lines (127 loc) 4.05 kB
import { assertWrap } from '@augment-vir/assert'; import { camelCaseToKebabCase, sanitizeFileName } from '@augment-vir/common'; /** * Used to determine which test context is in use. * * @category Test : Util * @category Package : @augment-vir/test * @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test) */ export var TestEnv; (function (TestEnv) { TestEnv["Node"] = "node"; TestEnv["Web"] = "web"; TestEnv["Playwright"] = "playwright"; })(TestEnv || (TestEnv = {})); /** * Extracts the full test name (including parent describes) of a given test context. Whether the * test be run in web or node tests, the name will be the same. * * @category Test : Util * @category Package : @augment-vir/test * @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test) */ export function extractTestName(testContext) { if (isTestContext(testContext, TestEnv.Node)) { return testContext.fullName; } else if (isTestContext(testContext, TestEnv.Playwright)) { return testContext.testName.clean; } else { return flattenMochaParentTitles(testContext.test).join(' > '); } } /** * Same as {@link extractTestName} but sanitizes the output so that it's safe for directory names * (even on Windows). * * @category Test : Util * @category Package : @augment-vir/test * @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test) */ export function extractTestNameAsDir(testContext) { return assertWrap.isTruthy(cleanTestNameAsDir(extractTestName(testContext))); } /** * Same as {@link extractTestNameAsDir} but sanitizes any input in the same way. * * @category Test : Util * @category Package : @augment-vir/test * @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test) */ export function cleanTestNameAsDir(testName) { return assertWrap.isTruthy(sanitizeFileName(camelCaseToKebabCase(testName).replaceAll(/[<>:"/\-\\|?*_\s.]+/g, '_'))); } function flattenMochaParentTitles(node) { if (node.root) { return []; } else { return [ ...flattenMochaParentTitles(node.parent), node.title, ]; } } /** * Asserts that the given context is for the given env and returns that context. * * @category Test : Util * @category Package : @augment-vir/test * @throws `TypeError` if the context does not match the env. * @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test) */ export function assertWrapTestContext(context, env) { assertTestContext(context, env); return context; } /** * Asserts that the given context is for the given env, otherwise throws an Error. * * @category Test : Util * @category Package : @augment-vir/test * @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test) */ export function assertTestContext(context, env) { const actualEnv = determineTestContextEnv(context); if (actualEnv !== env) { throw new TypeError(`Provided test context is not for the expected env '${env}'.`); } } /** * Checks that the given context is for the given env. * * @category Test : Util * @category Package : @augment-vir/test * @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test) */ export function isTestContext(context, env) { try { assertTestContext(context, env); return true; } catch { return false; } } const nodeOnlyCheckKey = 'diagnostic'; const playwrightOnlyCheckKey = 'browser'; /** * Determine the env for the given test context. * * @category Test : Util * @category Package : @augment-vir/test * @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test) */ export function determineTestContextEnv(context) { if (playwrightOnlyCheckKey in context) { return TestEnv.Playwright; } else if (nodeOnlyCheckKey in context) { return TestEnv.Node; } else { return TestEnv.Web; } }