@augment-vir/test
Version:
A universal testing suite that works with Mocha style test runners _and_ Node.js's built-in test runner.
155 lines (154 loc) • 5.74 kB
JavaScript
import { randomString } from '@augment-vir/common';
import { isInsidePlaywrightTest, 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;
}
async function createPlaywrightIt() {
const { test: originalPlaywrightIt } = await import('@playwright/test');
/**
* Right now this wrapper nukes Playwright's file detection. See
* https://github.com/microsoft/playwright/issues/23157#issuecomment-1574955057 for possible
* help.
*/
const playwrightIt = Object.assign((doesThis, callback) => {
return originalPlaywrightIt(doesThis, async ({ page, baseURL, browser, context, extraHTTPHeaders, viewport, video, userAgent, timezoneId, serviceWorkers, screenshot, isMobile, headless, hasTouch, }, testInfo) => {
const playwrightTestContext = {
page,
baseURL,
browser,
context,
extraHTTPHeaders,
viewport,
video,
userAgent,
timezoneId,
serviceWorkers,
screenshot,
isMobile,
headless,
hasTouch,
testInfo,
testName: {
clean: testInfo.titlePath.join(' > '),
unique: [
...testInfo.titlePath,
randomString(),
].join(' > '),
},
};
await callback(playwrightTestContext);
});
}, {
skip: (doesThis, callback) => {
return originalPlaywrightIt.skip(doesThis, async ({ page, baseURL, browser, context, extraHTTPHeaders, viewport, video, userAgent, timezoneId, serviceWorkers, screenshot, isMobile, headless, hasTouch, }, testInfo) => {
const playwrightTestContext = {
page,
baseURL,
browser,
context,
extraHTTPHeaders,
viewport,
video,
userAgent,
timezoneId,
serviceWorkers,
screenshot,
isMobile,
headless,
hasTouch,
testInfo,
testName: {
clean: testInfo.titlePath.join(' > '),
unique: [
...testInfo.titlePath,
randomString(),
].join(' > '),
},
};
await callback(playwrightTestContext);
});
},
only: (doesThis, callback) => {
return originalPlaywrightIt.only(doesThis, async ({ page, baseURL, browser, context, extraHTTPHeaders, viewport, video, userAgent, timezoneId, serviceWorkers, screenshot, isMobile, headless, hasTouch, }, testInfo) => {
const playwrightTestContext = {
page,
baseURL,
browser,
context,
extraHTTPHeaders,
viewport,
video,
userAgent,
timezoneId,
serviceWorkers,
screenshot,
isMobile,
headless,
hasTouch,
testInfo,
testName: {
clean: testInfo.titlePath.join(' > '),
unique: [
...testInfo.titlePath,
randomString(),
].join(' > '),
},
};
await callback(playwrightTestContext);
});
},
});
return playwrightIt;
}
/**
* 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)
? isInsidePlaywrightTest()
? await createPlaywrightIt()
: (await import('node:test')).it
: createWebIt();