Skip to content

Commit

Permalink
refactor: separate api methods
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 8, 2023
1 parent 62bbfd1 commit b52fcd3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
75 changes: 44 additions & 31 deletions src/create-context.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type ModuleDefaultExport <defaultExport> =
{ default: defaultExport }
| { default: { default: defaultExport } }; // ESM compiled to CJS

type RunTestSuite = <
export type RunTestSuite = <
Callback extends TestSuiteCallback
>(
testSuite: TestSuite<Callback> | Promise<
Expand All @@ -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;
Expand All @@ -58,7 +60,7 @@ export type DescribeApi = {
describe: Describe;
test: Test;
runTestSuite: RunTestSuite;
onFinish: (callback: Callback) => void;
onFinish: onFinish;
};

export type Describe = (
Expand Down

0 comments on commit b52fcd3

Please sign in to comment.