UNPKG

@augment-vir/test

Version:

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

82 lines (81 loc) 2.52 kB
import { RuntimeEnv } from '@augment-vir/core'; export { RuntimeEnv } from '@augment-vir/core'; /** * 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, RuntimeEnv.Node)) { return testContext.fullName; } else { return flattenMochaParentTitles(testContext.test).join(' > '); } } 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'; /** * 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) { return nodeOnlyCheckKey in context ? RuntimeEnv.Node : RuntimeEnv.Web; }