-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9aa5a83
commit c6e03d4
Showing
11 changed files
with
184 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,8 @@ | |
{ | ||
"allow": [ | ||
"describe", | ||
"test" | ||
"test", | ||
"onFinish" | ||
] | ||
} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { | ||
Context, | ||
PendingTests, | ||
} from './types.js'; | ||
import { createTest } from './create-test.js'; | ||
import { createDescribe } from './create-describe.js'; // eslint-disable-line import/no-cycle | ||
import { | ||
describe as topLevelDescribe, | ||
test as topLevelTest, | ||
} from './top-level-context.js'; | ||
|
||
export const createContext = ( | ||
description?: string, | ||
): Context => { | ||
const callbacks: Context['callbacks'] = { | ||
onFinish: [], | ||
}; | ||
|
||
const pendingTests: PendingTests = []; | ||
|
||
const test = description ? createTest(`${description} ›`, pendingTests) : topLevelTest; | ||
const describe = description ? createDescribe(`${description} ›`, pendingTests) : topLevelDescribe; | ||
|
||
const context: Context = { | ||
test, | ||
describe, | ||
runTestSuite: ( | ||
testSuite, | ||
...args | ||
) => { | ||
const runningTestSuite = (async () => { | ||
let maybeTestSuiteModule = await testSuite; | ||
|
||
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); | ||
})(); | ||
|
||
pendingTests.push(runningTestSuite); | ||
|
||
return runningTestSuite; | ||
}, | ||
onFinish(callback) { | ||
callbacks.onFinish.push(callback); | ||
}, | ||
pendingTests, | ||
callbacks, | ||
}; | ||
|
||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { createTest } from './create-test.js'; | ||
import { createDescribe } from './create-describe.js'; | ||
import { createDescribe } from './create-describe.js'; // eslint-disable-line import/no-cycle | ||
|
||
export const test = createTest(); | ||
export const describe = createDescribe(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* This accepts a promises array that can have more promises | ||
* in it by the time every promise is resolved. | ||
* | ||
* This keeps waiting on the new it until the promises array | ||
* is empty. | ||
*/ | ||
export const waitAllPromises = async ( | ||
promises: Promise<unknown>[], | ||
) => { | ||
while (promises.length > 0) { | ||
const currentPromises = promises.splice(0); | ||
await Promise.all(currentPromises); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { describe, testSuite } from '#manten'; | ||
|
||
describe('describe', ({ test, onFinish, runTestSuite }) => { | ||
onFinish(() => { | ||
console.log('describe finish'); | ||
}); | ||
|
||
test('hooks', ({ onTestFail, onTestFinish }) => { | ||
console.log('test start'); | ||
onTestFail((error) => { | ||
console.log('test error', error.message); | ||
}); | ||
|
||
onTestFinish(() => { | ||
console.log('test finish'); | ||
}); | ||
|
||
throw new Error('hello'); | ||
}); | ||
|
||
runTestSuite(testSuite(({ describe, onFinish }) => { | ||
console.log('test suite start'); | ||
|
||
onFinish(() => { | ||
/** | ||
* This is triggered after "describe finish" because | ||
* it shares the same context as the first describe | ||
*/ | ||
console.log('test suite finish'); | ||
}); | ||
|
||
describe('test suite', ({ onFinish }) => { | ||
console.log('test suite describe start'); | ||
|
||
onFinish(() => { | ||
console.log('test suite describe finish'); | ||
}); | ||
}); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
// 'timers/promises' polyfill until Node 12 is deprecated | ||
export const setTimeout = (duration: number) => new Promise((resolve) => { | ||
export const setTimeout = ( | ||
duration: number, | ||
) => new Promise((resolve) => { | ||
global.setTimeout(resolve, duration); | ||
}); |