-
-
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.
fix: ensure
afterEach
execution after a test failure (#598)
* ci(tdd): include a failure case * ci: fix test failure case * chore: unify `it` and `test` * ci: improve tests * ci: fix tests * chore: better name `hasIt` to `hasItOrTest`
- Loading branch information
1 parent
897577c
commit e6654c4
Showing
14 changed files
with
291 additions
and
128 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ node_modules | |
.env | ||
.nyc_output | ||
/.temp | ||
/.temp2 | ||
/test-src | ||
/test-tests | ||
/test-before-and-after-each.json |
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,68 @@ | ||
import { readFile, writeFile, rm, mkdir } from 'node:fs/promises'; | ||
import { | ||
beforeEach, | ||
afterEach, | ||
log, | ||
test, | ||
describe, | ||
assert, | ||
} from '../../src/modules/index.js'; | ||
|
||
const testDir = '../../.temp2'; | ||
const testFile = `${testDir}/each-hook`; | ||
|
||
const clearFixture = async () => { | ||
try { | ||
await rm(testFile); | ||
await rm(testDir, { recursive: true, force: true }); | ||
log(' - Cleaning'); | ||
} catch {} | ||
}; | ||
|
||
const writeFixture = async () => { | ||
await mkdir(testDir); | ||
await writeFile(testFile, 'test', 'utf-8'); | ||
log(' - Writing'); | ||
}; | ||
|
||
const toTest = async (message: string) => { | ||
await readFile(testFile, 'utf-8'); | ||
return message; | ||
}; | ||
|
||
beforeEach(async () => { | ||
log('- before beforeEach writeFixture'); | ||
|
||
await writeFixture(); | ||
|
||
log('- after beforeEach writeFixture'); | ||
}); | ||
|
||
afterEach(async () => { | ||
log('- before afterEach clearFixture'); | ||
|
||
await clearFixture(); | ||
|
||
log('- after afterEach clearFixture'); | ||
}); | ||
|
||
describe(async () => { | ||
await clearFixture(); | ||
|
||
await test('first test', async () => { | ||
log(' before first test'); | ||
|
||
assert(true, await toTest('first test')); | ||
|
||
log(' after first test'); | ||
}); | ||
|
||
await writeFixture(); | ||
await test('Force failure', async () => { | ||
log(' before first test'); | ||
|
||
assert(true, await toTest('first test')); | ||
|
||
log(' after first test'); | ||
}); | ||
}); |
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,54 @@ | ||
import { assert } from '../../src/modules/essentials/assert.js'; | ||
import { test } from '../../src/modules/helpers/test.js'; | ||
import { describe } from '../../src/modules/helpers/describe.js'; | ||
import { it } from '../../src/modules/helpers/it.js'; | ||
|
||
assert(true, 'Should emit a basic assetion log'); | ||
|
||
test('Should emit a basic test scope log', () => { | ||
// ... | ||
}); | ||
|
||
describe('Should emit a basic test scope log', () => { | ||
// ... | ||
}); | ||
|
||
it('Should emit a basic it scope log', () => { | ||
// ... | ||
}); | ||
|
||
test('Should emit a basic test scope log', () => { | ||
assert(true, 'Should emit a basic assetion log'); | ||
}); | ||
|
||
describe('Should emit a basic test scope log', () => { | ||
assert(true, 'Should emit a basic assetion log'); | ||
}); | ||
|
||
it('Should emit a basic test scope log', () => { | ||
assert(true, 'Should emit a basic assetion log'); | ||
}); | ||
|
||
describe('Should emit a basic test scope log', () => { | ||
it('Should emit a basic it scope log', () => { | ||
// ... | ||
}); | ||
}); | ||
|
||
describe('Should emit a basic test scope log', () => { | ||
test('Should emit a basic test scope log', () => { | ||
// ... | ||
}); | ||
}); | ||
|
||
describe('Should emit a basic test scope log', () => { | ||
it('Should emit a basic it scope log', () => { | ||
assert(true, 'Should emit a basic assetion log'); | ||
}); | ||
}); | ||
|
||
describe('Should emit a basic test scope log', () => { | ||
test('Should emit a basic test scope log', () => { | ||
assert(true, 'Should emit a basic assetion log'); | ||
}); | ||
}); |
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,79 +1,5 @@ | ||
/* c8 ignore next */ // ? | ||
import { hrtime, env } from 'node:process'; | ||
import { each } from '../../configs/each.js'; | ||
import { indentation } from '../../configs/indentation.js'; | ||
import { format } from '../../services/format.js'; | ||
import { Write } from '../../services/write.js'; | ||
import { it } from './it.js'; | ||
|
||
export async function test( | ||
message: string, | ||
cb: () => Promise<unknown> | ||
): Promise<void>; | ||
export function test(message: string, cb: () => unknown): void; | ||
export async function test(cb: () => Promise<unknown>): Promise<void>; | ||
export function test(cb: () => unknown): void; | ||
/* c8 ignore next */ // ? | ||
export async function test( | ||
...args: [ | ||
string | (() => unknown | Promise<unknown>), | ||
(() => unknown | Promise<unknown>)?, | ||
] | ||
): Promise<void> { | ||
let message: string | undefined; | ||
let cb: () => unknown | Promise<unknown>; | ||
|
||
const isPoku = typeof env?.FILE === 'string' && env?.FILE.length > 0; | ||
const FILE = env.FILE; | ||
|
||
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) { | ||
indentation.hasTest = true; | ||
|
||
Write.log( | ||
isPoku | ||
? /* c8 ignore next 2 */ | ||
format(`◌ ${message} › ${format(`${FILE}`).italic().gray()}`).dim() | ||
: format(`◌ ${message}`).dim() | ||
); | ||
} | ||
|
||
if (typeof each.before.cb === 'function') { | ||
const beforeResult = each.before.cb(); | ||
|
||
if (beforeResult instanceof Promise) { | ||
await beforeResult; | ||
} | ||
} | ||
|
||
const start = hrtime(); | ||
const resultCb = cb(); | ||
|
||
if (resultCb instanceof Promise) { | ||
await resultCb; | ||
} | ||
|
||
const end = hrtime(start); | ||
|
||
if (typeof each.after.cb === 'function') { | ||
const afterResult = each.after.cb(); | ||
|
||
if (afterResult instanceof Promise) { | ||
await afterResult; | ||
} | ||
} | ||
|
||
if (message) { | ||
const total = (end[0] * 1e3 + end[1] / 1e6).toFixed(6); | ||
|
||
indentation.hasTest = false; | ||
Write.log( | ||
`${format(`● ${message}`).success().bold()} ${format(`› ${total}ms`).success().dim()}` | ||
); | ||
} | ||
} | ||
export const test = it; |
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.