From 4dad077925004a2559574662c7ebd530315fd5e5 Mon Sep 17 00:00:00 2001 From: Ben Merckx Date: Wed, 22 Jan 2025 11:39:31 +0100 Subject: [PATCH 1/3] Setup hooks --- bun.lockb | Bin 3837 -> 5691 bytes package.json | 3 ++- src/suite.bun.js | 19 ++++++++++++++++++- src/suite.d.ts | 9 ++++++++- src/suite.deno.js | 10 ++++++++++ src/suite.node.js | 11 ++++++++++- suite.test.js | 5 +++++ 7 files changed, 53 insertions(+), 4 deletions(-) diff --git a/bun.lockb b/bun.lockb index 5c532e11e56c5f0e12ecc8ce26ef86070a5589f8..f483e1011f7c8cdfe0637ff061b99bc6b58c9590 100644 GIT binary patch delta 2141 zcmb7FX;4#F6n-xoTM`U~5m2O{AP{&EGyzlwTtQI;sZvy+Aq0fl0Lf#DS`2lmI4&#~ zEof0JBSWPfwJf5jL#IkXaW9Aq6=&*H#ab6A(n9FDAumZC`=e*(zH{&S&N+8^_k4HM zrNy_H@*Y+#pxBEC7L8@qiXLzMu|dqWZ!eqd=RP#O;m?fIwGMR-gkFRt@vhoc!tnt? zXmT@XB@D#StO=sP&7i*rZ3SZ;XdBS;NrK>m9yHh|fEK|xltmDbtUwIf4zw$1?8gJ` z1bPVKY(axN0Wk&H)U1mcHn*!LANDBl6>nFjl=OK8S9<+=_Lo~_`7xi~o!XWlnxk7; zoD);mwWVubi0oiZ`Kr=~yin=!hgFe@KAE)_Ubn>hUfrepJ?R*lKt{6W63Ce?LouW! znUC&}VPp$(Vug`bC;`69Q3ZU9(H&Np=mIQd1}p)TURX#(PRDVgC|GQO8NxFnn*f<< zhOmvuaX_edBE5jjHbXeZ7)LfiNX!tf5m^jK;9H287IH}?3%Ts=(+92Age8nxL+(l5@E^m2Nbl+lX{8;BVr!?CW`Hvs1 z8;s=;c_T6Q{UNuPqe`BmmVL2iUVFsBeW!Q#M$cCHxGs!HSo7EU;j+&1z5KdA+!i+O zzOhdmJ?VALXrjU^b;KiJ?$d60)kQ@<>ru~jPuV5dJxSek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s#K%>)IR>Q`tDFwm@Wg+{#G8LWBJz37ZJO%{#RuY>C1hl;A_7wC@jPZrR24Z_a`L+ch6Sbe!YF?mIQl}OIVtOg2<&hCwgIRAqT4ykG>8`D+ zvfh+#wAQJtTvb_Fh1IIB native.expect(a).not.toBe(b), equal: (a, b) => native.expect(a).not.toEqual(b) } + test.beforeAll = native.beforeAll.bind(native) + test.beforeEach = native.beforeEach.bind(native) + test.afterEach = native.afterEach.bind(native) + test.afterAll = native.afterAll.bind(native) return test } diff --git a/src/suite.d.ts b/src/suite.d.ts index d8e75ad..6348476 100644 --- a/src/suite.d.ts +++ b/src/suite.d.ts @@ -3,6 +3,14 @@ export type Describe = (name: string, run: () => void | Promise) => void /** Define a test suite */ export interface DefineTest extends Describe { + /** Run this before all tests */ + beforeAll: (fn: () => void | Promise) => void + /** Run this before each test */ + beforeEach: (fn: () => void | Promise) => void + /** Run this after all tests */ + afterAll: (fn: () => void | Promise) => void + /** Run this after each test */ + afterEach: (fn: () => void | Promise) => void /** Skip the test */ skip: Describe /** Only run this test */ @@ -29,7 +37,6 @@ export interface DefineTest extends Describe { /** Define a test suite */ export interface Suite { (meta: ImportMeta, define: (test: DefineTest) => void): void - // runtime: 'node' | 'deno' | 'bun' } export const suite: Suite \ No newline at end of file diff --git a/src/suite.deno.js b/src/suite.deno.js index 7a333b2..b0f1e4f 100644 --- a/src/suite.deno.js +++ b/src/suite.deno.js @@ -7,6 +7,12 @@ import { assertStrictEquals, assertThrows } from '../node_modules/@jsr/std__assert/mod.js' +import { + afterAll, + afterEach, + beforeAll, + beforeEach +} from '../node_modules/@jsr/std__testing/bdd.js' const native = Deno.test const test = native.bind() @@ -21,5 +27,9 @@ test.not = { is: assertNotStrictEquals, equal: assertNotEquals } +test.beforeAll = beforeAll +test.beforeEach = beforeEach +test.afterEach = afterEach +test.afterAll = afterAll export const suite = (meta, define) => define(test) diff --git a/src/suite.node.js b/src/suite.node.js index 23c2347..ce976ac 100644 --- a/src/suite.node.js +++ b/src/suite.node.js @@ -1,4 +1,5 @@ import * as asserts from 'node:assert' +import * as path from 'node:path' import * as native from 'node:test' const test = native.test.bind() @@ -13,5 +14,13 @@ test.not = { is: asserts.notStrictEqual, equal: asserts.notDeepStrictEqual } +test.beforeAll = native.before.bind(native) +test.beforeEach = native.beforeEach.bind(native) +test.afterEach = native.afterEach.bind(native) +test.afterAll = native.after.bind(native) -export const suite = (meta, define) => define(test) +export const suite = (meta, define) => { + native.suite(path.relative(process.cwd(), meta.filename), () => { + define(test) + }) +} diff --git a/suite.test.js b/suite.test.js index 7c5e7b4..2671740 100644 --- a/suite.test.js +++ b/suite.test.js @@ -1,6 +1,11 @@ import {suite} from '#suite' suite(import.meta, test => { + test.beforeEach(() => console.log('before')) + test.afterEach(() => console.log('after')) + test.afterAll(() => console.log('after all')) + test.beforeAll(() => console.log('before all')) + test('truthy', () => { test.ok(true) }) From c37f59371a70681f49998a9b17bdf4f86ffddf1b Mon Sep 17 00:00:00 2001 From: Ben Merckx Date: Wed, 22 Jan 2025 12:08:33 +0100 Subject: [PATCH 2/3] Finish hooks --- bun.lock | 41 +++++++++++++++++++++ bun.lockb | Bin 5691 -> 0 bytes package.json | 17 +++++---- src/suite.bun.js | 15 +------- src/suite.deno.js | 4 +- suite.test.js | 91 +++++++++++++++++++++++++--------------------- 6 files changed, 103 insertions(+), 65 deletions(-) create mode 100644 bun.lock delete mode 100644 bun.lockb diff --git a/bun.lock b/bun.lock new file mode 100644 index 0000000..9a016a9 --- /dev/null +++ b/bun.lock @@ -0,0 +1,41 @@ +{ + "lockfileVersion": 0, + "workspaces": { + "": { + "name": "suite", + "devDependencies": { + "@jsr/std__assert": "^1.0.9", + "@jsr/std__testing": "^1.0.9", + "@types/bun": "latest", + "typescript": "^5.7.3", + }, + }, + }, + "packages": { + "@jsr/std__assert": ["@jsr/std__assert@1.0.11", "https://npm.jsr.io/~/11/@jsr/std__assert/1.0.11.tgz", { "dependencies": { "@jsr/std__internal": "^1.0.5" } }, "sha512-sNXyi9EQA4f/9F3gI1t+Dz+LnvPwCC3JDUqn9YfWl7IK7RRipiHOkDpV8Vq3LL/ZDZF8Tcr36zVhuEOrRs8i+g=="], + + "@jsr/std__async": ["@jsr/std__async@1.0.10", "https://npm.jsr.io/~/11/@jsr/std__async/1.0.10.tgz", {}, "sha512-/6RY097xGI2xBi8c83i0C/DSJnR7uymrzuuJu1RT4qr6DzNruV+dV+kLoTt+MQfu3TCAWraRbnpR5RN0LlSZQA=="], + + "@jsr/std__data-structures": ["@jsr/std__data-structures@1.0.6", "https://npm.jsr.io/~/11/@jsr/std__data-structures/1.0.6.tgz", {}, "sha512-Ejc8mHLuoYxXLu2zPquvqijdgQ19OV+1DdVDrLc/Cg+tiuGh4Dq2FSnLiPINh4lO1AJ3XcZcYPx38RxdsZcCOg=="], + + "@jsr/std__fs": ["@jsr/std__fs@1.0.10", "https://npm.jsr.io/~/11/@jsr/std__fs/1.0.10.tgz", { "dependencies": { "@jsr/std__path": "^1.0.8" } }, "sha512-rVH3Y73A2uJMn1XA6NLvJtVYTYNnpHlTFuqrvVVD3AZ8q/Q9h4WL14Us02ThzfZp2SqUhG9bD974vzXA3QIeZA=="], + + "@jsr/std__internal": ["@jsr/std__internal@1.0.5", "https://npm.jsr.io/~/11/@jsr/std__internal/1.0.5.tgz", {}, "sha512-W2aN7UypJJ6EkfyXt5hBeIijBb8CmZk0jdjCzhOROr0SrXsXZ/V/9Qu1N0NxumFauPDAJz9NcP1mmZ9ZVSXwpQ=="], + + "@jsr/std__path": ["@jsr/std__path@1.0.8", "https://npm.jsr.io/~/11/@jsr/std__path/1.0.8.tgz", {}, "sha512-eNBGlh/8ZVkMxtFH4bwIzlAeKoHYk5in4wrBZhi20zMdOiuX4QozP4+19mIXBT2lzHDjhuVLyECbhFeR304iDg=="], + + "@jsr/std__testing": ["@jsr/std__testing@1.0.9", "https://npm.jsr.io/~/11/@jsr/std__testing/1.0.9.tgz", { "dependencies": { "@jsr/std__assert": "^1.0.10", "@jsr/std__async": "^1.0.9", "@jsr/std__data-structures": "^1.0.6", "@jsr/std__fs": "^1.0.9", "@jsr/std__internal": "^1.0.5", "@jsr/std__path": "^1.0.8" } }, "sha512-aTVkTPvrBwbs/K9kF/nLrjYpsp/PrGwj3NwU+KU2sLuxmgmPyNW+JfHfZl7kL9jwf1Cfo1wGqleNQSjj94DtUg=="], + + "@types/bun": ["@types/bun@1.1.3", "", { "dependencies": { "bun-types": "1.1.9" } }, "sha512-i+mVz8C/lx+RprDR6Mr402iE1kmajgJPnmSfJ/NvU85sGGXSylYZ/6yc+XhVLr2E/t8o6HmjwV0evtnUOR0CFA=="], + + "@types/node": ["@types/node@20.12.14", "", { "dependencies": { "undici-types": "~5.26.4" } }, "sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg=="], + + "@types/ws": ["@types/ws@8.5.10", "", { "dependencies": { "@types/node": "*" } }, "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A=="], + + "bun-types": ["bun-types@1.1.9", "", { "dependencies": { "@types/node": "~20.12.8", "@types/ws": "~8.5.10" } }, "sha512-3YuLiH4Ne/ghk7K6mHiaqCqKOMrtB0Z5p1WAskHSVgi0iMZgsARV4yGkbfi565YsStvUq6GXTWB3ga7M8cznkA=="], + + "typescript": ["typescript@5.7.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw=="], + + "undici-types": ["undici-types@5.26.5", "", {}, "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA=="], + } +} diff --git a/bun.l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foreEach(() => console.log('before')) -test.afterEach(() => console.log('after')) -test.afterAll(() => console.log('after all')) -test.beforeAll(() => console.log('before all')) + test.beforeAll(() => (status = 'before')) + test.beforeEach(() => { + if (status !== 'before' && status !== 'afterEach') + throw new Error(`hooks did not run, ${status}`) + status = 'beforeEach' + }) + test.afterEach(() => (status = 'afterEach')) -test('truthy', () => { - test.ok(true) -}) + test('before each', () => { + test.equal(status, 'beforeEach') + }) -test('falsy', () => { - test.not.ok(false) -}) + test('truthy', () => { + test.ok(true) + }) -test('equal', () => { - test.equal({a: 1}, {a: 1}) -}) + test('falsy', () => { + test.not.ok(false) + }) -test('not equal', () => { - test.not.equal({a: 1}, {a: 2}) -}) + test('equal', () => { + test.equal({a: 1}, {a: 1}) + }) -test('strict equal', () => { - test.is(1, 1) -}) + test('not equal', () => { + test.not.equal({a: 1}, {a: 2}) + }) -test('not strict equal', () => { - test.not.is(1, '1') -}) + test('strict equal', () => { + test.is(1, 1) + }) -test('throws', () => { - test.throws(() => { - throw new Error('ok') + test('not strict equal', () => { + test.not.is(1, '1') }) -}) -test('throws message', () => { - test.throws(() => { - throw new Error('a message') - }, 'message') -}) + test('throws', () => { + test.throws(() => { + throw new Error('ok') + }) + }) -test.skip('skip', () => { - test.ok(false) -}) + test('throws message', () => { + test.throws(() => { + throw new Error('a message') + }, 'message') + }) -test('async', async () => { - test.ok(true) -}) + test.skip('skip', () => { + test.ok(false) + }) -/*test.only('only', () => { - test.ok(true) -})*/ + test('async', async () => { + test.ok(true) + }) + + /*test.only('only', () => { + test.ok(true) + })*/ +}) From 282944fec0d6c58311b1febf7e5d3d4b71bcf2e6 Mon Sep 17 00:00:00 2001 From: Ben Merckx Date: Wed, 22 Jan 2025 12:13:51 +0100 Subject: [PATCH 3/3] Cleanup --- suite.test.js | 97 ++++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/suite.test.js b/suite.test.js index 3c47b30..aa9feef 100644 --- a/suite.test.js +++ b/suite.test.js @@ -1,65 +1,68 @@ import {suite} from '#suite' -suite(import.meta, test => { - let status = '' +const test = suite(import.meta) - test.beforeAll(() => (status = 'before')) - test.beforeEach(() => { - if (status !== 'before' && status !== 'afterEach') - throw new Error(`hooks did not run, ${status}`) - status = 'beforeEach' - }) - test.afterEach(() => (status = 'afterEach')) +let status = '' - test('before each', () => { - test.equal(status, 'beforeEach') - }) +test.beforeAll(() => (status = 'before')) +test.beforeEach(() => { + if (status !== 'before' && status !== 'afterEach') + throw new Error(`hooks did not run, ${status}`) + status = 'beforeEach' +}) +test.afterEach(() => (status = 'afterEach')) +test.afterAll(() => { + if (status !== 'afterEach') throw new Error(`hooks did not run, ${status}`) +}) - test('truthy', () => { - test.ok(true) - }) +test('before each', () => { + test.equal(status, 'beforeEach') +}) - test('falsy', () => { - test.not.ok(false) - }) +test('truthy', () => { + test.ok(true) +}) - test('equal', () => { - test.equal({a: 1}, {a: 1}) - }) +test('falsy', () => { + test.not.ok(false) +}) - test('not equal', () => { - test.not.equal({a: 1}, {a: 2}) - }) +test('equal', () => { + test.equal({a: 1}, {a: 1}) +}) - test('strict equal', () => { - test.is(1, 1) - }) +test('not equal', () => { + test.not.equal({a: 1}, {a: 2}) +}) - test('not strict equal', () => { - test.not.is(1, '1') - }) +test('strict equal', () => { + test.is(1, 1) +}) - test('throws', () => { - test.throws(() => { - throw new Error('ok') - }) - }) +test('not strict equal', () => { + test.not.is(1, '1') +}) - test('throws message', () => { - test.throws(() => { - throw new Error('a message') - }, 'message') +test('throws', () => { + test.throws(() => { + throw new Error('ok') }) +}) - test.skip('skip', () => { - test.ok(false) - }) +test('throws message', () => { + test.throws(() => { + throw new Error('a message') + }, 'message') +}) - test('async', async () => { - test.ok(true) - }) +test.skip('skip', () => { + test.ok(false) +}) - /*test.only('only', () => { +test('async', async () => { + test.ok(true) +}) + +/*test.only('only', () => { test.ok(true) })*/ -})