-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(it): support for Node.js familiar API usage (#354)
* feat(it): support for Node.js familiar API usage * ci: add `beforeEach` and `afterEach` tests to `it` method
- Loading branch information
1 parent
b423a7e
commit c7b2b48
Showing
11 changed files
with
345 additions
and
16 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
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,45 @@ | ||
/* c8 ignore next */ | ||
import { each } from '../configs/each.js'; | ||
import { describe } from './describe.js'; | ||
|
||
export async function it( | ||
message: string, | ||
cb: () => Promise<unknown> | ||
): Promise<void>; | ||
export async function it(message: string, cb: () => unknown): Promise<void>; | ||
export async function it(cb: () => Promise<unknown>): Promise<void>; | ||
export function it(cb: () => unknown): void; | ||
export async function it( | ||
...args: [ | ||
string | (() => unknown | Promise<unknown>), | ||
(() => unknown | Promise<unknown>)?, | ||
] | ||
): Promise<void> { | ||
let message: string | undefined; | ||
let cb: () => unknown | Promise<unknown>; | ||
|
||
if (typeof each.before.cb === 'function' && each.before.test) { | ||
const beforeResult = each.before.cb(); | ||
|
||
/* c8 ignore next */ | ||
if (beforeResult instanceof Promise) await beforeResult; | ||
} | ||
|
||
if (typeof args[0] === 'string') { | ||
message = args[0]; | ||
cb = args[1] as () => unknown | Promise<unknown>; | ||
} else cb = args[0] as () => unknown | Promise<unknown>; | ||
|
||
if (message) describe(message, { icon: '›' }); | ||
|
||
const resultCb = cb(); | ||
|
||
/* c8 ignore next */ | ||
if (resultCb instanceof Promise) await resultCb; | ||
|
||
if (typeof each.after.cb === 'function' && each.after.test) { | ||
const afterResult = each.after.cb(); | ||
/* c8 ignore next */ | ||
if (afterResult instanceof Promise) await afterResult; | ||
} | ||
} |
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,80 @@ | ||
import { describe } from '../../../../src/modules/describe.js'; | ||
import { it } from '../../../../src/modules/it.js'; | ||
import { assert } from '../../../../src/modules/assert.js'; | ||
import { beforeEach, afterEach } from '../../../../src/modules/each.js'; | ||
|
||
let counter = 0; | ||
|
||
const asyncPreIncrement = async () => | ||
await new Promise((resolve) => resolve(++counter)); | ||
|
||
const beforeEachHelper = beforeEach(asyncPreIncrement, { immediate: true }); | ||
|
||
const afterEachHelper = afterEach(asyncPreIncrement, {}); | ||
|
||
describe('Asynchronous Before and After Each Suite (it)', () => { | ||
it(() => { | ||
assert.equal( | ||
counter, | ||
2, | ||
'value incremented by 1 from beforeEach with immediate option should be 2' | ||
); | ||
}); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
4, | ||
'value incremented by 1 from beforeEach with immediate option and by 1 from previous test with afterEach should be 4' | ||
); | ||
}); | ||
|
||
afterEachHelper.pause(); | ||
beforeEachHelper.pause(); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
5, | ||
'value should still 5 by pausing both beforeEach and afterEach, considering the abscence of beforeEach effect' | ||
); | ||
}); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
5, | ||
'value should still 5 by pausing both beforeEach and afterEach, considering the the abscence of beforeEach and afterEach (from previous test) effects' | ||
); | ||
}); | ||
|
||
afterEachHelper.continue(); | ||
beforeEachHelper.continue(); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
6, | ||
'value incremented by 1 from unpausing beforeEach should be 6' | ||
); | ||
}); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
8, | ||
'value incremented by 1 from beforeEach and 1 by previous test with afterEach should be 8' | ||
); | ||
}); | ||
|
||
beforeEachHelper.reset(); | ||
afterEachHelper.reset(); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
9, | ||
'value should still 9 by reseting both beforeEach and afterEach' | ||
); | ||
}); | ||
}); |
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,84 @@ | ||
import { describe } from '../../../../src/modules/describe.js'; | ||
import { it } from '../../../../src/modules/it.js'; | ||
import { assert } from '../../../../src/modules/assert.js'; | ||
import { beforeEach, afterEach } from '../../../../src/modules/each.js'; | ||
|
||
let counter = 0; | ||
|
||
const beforeEachHelper = beforeEach( | ||
() => { | ||
++counter; | ||
}, | ||
{ immediate: true } | ||
); | ||
|
||
const afterEachHelper = afterEach(() => { | ||
++counter; | ||
}, {}); | ||
|
||
describe('Before and After Each Suite (it)', () => { | ||
it(() => { | ||
assert.equal( | ||
counter, | ||
2, | ||
'value incremented by 1 from beforeEach with immediate option should be 2' | ||
); | ||
}); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
4, | ||
'value incremented by 1 from beforeEach with immediate option and by 1 from previous test with afterEach should be 4' | ||
); | ||
}); | ||
|
||
afterEachHelper.pause(); | ||
beforeEachHelper.pause(); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
5, | ||
'value should still 5 by pausing both beforeEach and afterEach, considering the abscence of beforeEach effect' | ||
); | ||
}); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
5, | ||
'value should still 5 by pausing both beforeEach and afterEach, considering the the abscence of beforeEach and afterEach (from previous test) effects' | ||
); | ||
}); | ||
|
||
afterEachHelper.continue(); | ||
beforeEachHelper.continue(); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
6, | ||
'value incremented by 1 from unpausing beforeEach should be 6' | ||
); | ||
}); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
8, | ||
'value incremented by 1 from beforeEach and 1 by previous test with afterEach should be 8' | ||
); | ||
}); | ||
|
||
beforeEachHelper.reset(); | ||
afterEachHelper.reset(); | ||
|
||
it(() => { | ||
assert.equal( | ||
counter, | ||
9, | ||
'value should still 9 by reseting both beforeEach and afterEach' | ||
); | ||
}); | ||
}); |
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,30 @@ | ||
import { describe } from '../../../src/modules/describe.js'; | ||
import { it } from '../../../src/modules/it.js'; | ||
|
||
describe('Testing "it" method', { | ||
icon: '🔬', | ||
}); | ||
|
||
describe(async () => { | ||
it(() => {}); | ||
it(() => true); | ||
it(() => false); | ||
it(() => undefined); | ||
it(() => new Promise((resolve) => resolve(undefined))); | ||
it(async () => await new Promise((resolve) => resolve(undefined))); | ||
|
||
await it(() => new Promise((resolve) => resolve(undefined))); | ||
await it(async () => await new Promise((resolve) => resolve(undefined))); | ||
}); | ||
|
||
describe(async () => { | ||
it('', () => {}); | ||
it('', () => true); | ||
it('', () => false); | ||
it('', () => undefined); | ||
it('', () => new Promise((resolve) => resolve(undefined))); | ||
it('', async () => await new Promise((resolve) => resolve(undefined))); | ||
|
||
await it('', () => new Promise((resolve) => resolve(undefined))); | ||
await it('', async () => await new Promise((resolve) => resolve(undefined))); | ||
}); |
10 changes: 5 additions & 5 deletions
10
test/integration/each/each-promise.test.ts → ...ntegration/test/each/each-promise.test.ts
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
10 changes: 5 additions & 5 deletions
10
test/integration/each/each.test.ts → test/integration/test/each/each.test.ts
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 |
---|---|---|
|
@@ -4,5 +4,5 @@ | |
"link": { | ||
"type": "generated-index" | ||
}, | ||
"position": 3 | ||
"position": 4 | ||
} |
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
Oops, something went wrong.