diff --git a/src/create-context.ts b/src/create-context.ts index ef74379..e4b0bf6 100644 --- a/src/create-context.ts +++ b/src/create-context.ts @@ -1,6 +1,8 @@ import type { Context, PendingTests, + RunTestSuite, + onFinish, } from './types.js'; import { createTest } from './create-test.js'; import { createDescribe } from './create-describe.js'; // eslint-disable-line import/no-cycle @@ -20,49 +22,60 @@ export const createContext = ( const test = ( description - ? createTest(`${description} ›`, pendingTests) + ? createTest( + `${description} ›`, + pendingTests, + ) : topLevelTest ); + const describe = ( description - ? createDescribe(`${description} ›`, pendingTests) + ? createDescribe( + `${description} ›`, + pendingTests, + ) : topLevelDescribe ); - const context: Context = { - api: { - test, - describe, - runTestSuite: ( - testSuite, - ...args - ) => { - const runningTestSuite = (async () => { - let maybeTestSuiteModule = await testSuite; + const runTestSuite: RunTestSuite = ( + testSuite, + ...args + ) => { + const runningTestSuite = (async () => { + let maybeTestSuiteModule = await testSuite; + + if ('default' in maybeTestSuiteModule) { + maybeTestSuiteModule = maybeTestSuiteModule.default; + } - if ('default' in maybeTestSuiteModule) { - maybeTestSuiteModule = maybeTestSuiteModule.default; - } + /** + * When ESM is compiled to CJS, it's possible the entire module + * gets assigned as an object o default. In this case, + * it needs to be unwrapped again. + */ + if ('default' in maybeTestSuiteModule) { + maybeTestSuiteModule = maybeTestSuiteModule.default; + } - /** - * When ESM is compiled to CJS, it's possible the entire module - * gets assigned as an object o default. In this case, - * it needs to be unwrapped again. - */ - if ('default' in maybeTestSuiteModule) { - maybeTestSuiteModule = maybeTestSuiteModule.default; - } + return maybeTestSuiteModule.apply(context, args); + })(); - return maybeTestSuiteModule.apply(context, args); - })(); + pendingTests.push(runningTestSuite); - pendingTests.push(runningTestSuite); + return runningTestSuite; + }; - return runningTestSuite; - }, - onFinish(callback) { - callbacks.onFinish.push(callback); - }, + const onFinish: onFinish = (callback) => { + callbacks.onFinish.push(callback); + }; + + const context: Context = { + api: { + test, + describe, + runTestSuite, + onFinish, }, pendingTests, callbacks, diff --git a/src/types.ts b/src/types.ts index 280d7ec..7f2e67a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -29,7 +29,7 @@ type ModuleDefaultExport = { default: defaultExport } | { default: { default: defaultExport } }; // ESM compiled to CJS -type RunTestSuite = < +export type RunTestSuite = < Callback extends TestSuiteCallback >( testSuite: TestSuite | Promise< @@ -40,10 +40,12 @@ type RunTestSuite = < export type Callback = () => void; +export type onFinish = (callback: Callback) => void; + export type onTestFailCallback = (error: Error) => void; export type TestApi = { onTestFail: (callback: onTestFailCallback) => void; - onTestFinish: (callback: Callback) => void; + onTestFinish: onFinish; }; type TestFunction = (api: TestApi) => void; @@ -58,7 +60,7 @@ export type DescribeApi = { describe: Describe; test: Test; runTestSuite: RunTestSuite; - onFinish: (callback: Callback) => void; + onFinish: onFinish; }; export type Describe = (