@augment-vir/test
Version:
A universal testing suite that works with Mocha style test runners _and_ Node.js's built-in test runner.
55 lines (54 loc) • 1.84 kB
JavaScript
import { isRuntimeEnv, RuntimeEnv } from '@augment-vir/core';
function createWebIt() {
const webIt = Object.assign((doesThis, callback) => {
return globalThis.it(doesThis, async function () {
const context = this;
await callback(context);
});
}, {
skip: (doesThis, callback) => {
return globalThis.it.skip(doesThis, async function () {
const context = this;
await callback(context);
});
},
only: (doesThis, callback) => {
return globalThis.it.only(doesThis, async function () {
const context = this;
await callback(context);
});
},
});
return webIt;
}
/**
* A single test declaration. This can be used in both web tests _and_ node tests, so you only have
* import from a single place and learn a single interface.
*
* This should be nested within a `describe` call. The `it` name should form a sentence fragment
* that is attached to the parent `describe`'s input. The sentence should ultimately read like this:
* "myFunction, it does a thing" (as shown in the example below).
*
* Compatible with both [Node.js's test runner](https://nodejs.org/api/test.html) and
* [web-test-runner](https://modern-web.dev/docs/test-runner/overview/) or other Mocha-style test
* runners.
*
* @category Test
* @category Package : @augment-vir/test
* @example
*
* ```ts
* import {describe, it} from '@augment-vir/test';
*
* describe(myFunction.name, () => {
* it('does a thing', () => {
* myFunction();
* });
* });
* ```
*
* @package [`@augment-vir/test`](https://www.npmjs.com/package/@augment-vir/test)
*/
export const it = isRuntimeEnv(RuntimeEnv.Node)
? (await import('node:test')).it
: createWebIt();