Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add beforeAll, beforeEach, afterEach and afterAll hooks #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions bun.lock
Original file line number Diff line number Diff line change
@@ -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=="],
}
}
Binary file removed bun.lockb
Binary file not shown.
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
"types": "./src/suite.d.ts",
"type": "module",
"files": ["./dist", "./src"],
"devDependencies": {
"@jsr/std__assert": "^0.225.2",
"@types/bun": "latest",
"typescript": "^5.0.0"
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "bun build src/suite.deno.js --outdir=dist --minify",
"test": "node --test && deno test && bun test",
"test:node": "node --test",
"test:deno": "bun run build && deno test",
"deploy": "npm publish && npx jsr publish",
"prepublishOnly": "bun run build"
},
"publishConfig": {
"access": "public"
"devDependencies": {
"@jsr/std__assert": "^1.0.9",
"@jsr/std__testing": "^1.0.9",
"@types/bun": "latest",
"typescript": "^5.7.3"
},
"exports": {
".": {
Expand Down
4 changes: 4 additions & 0 deletions src/suite.bun.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export function setup(meta) {
is: (a, b) => 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
}

Expand Down
9 changes: 8 additions & 1 deletion src/suite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ export type Describe = (name: string, run: () => void | Promise<void>) => void

/** Define a test suite */
export interface DefineTest extends Describe {
/** Run this before all tests */
beforeAll: (fn: () => void | Promise<void>) => void
/** Run this before each test */
beforeEach: (fn: () => void | Promise<void>) => void
/** Run this after all tests */
afterAll: (fn: () => void | Promise<void>) => void
/** Run this after each test */
afterEach: (fn: () => void | Promise<void>) => void
/** Skip the test */
skip: Describe
/** Only run this test */
Expand Down Expand Up @@ -30,7 +38,6 @@ export interface DefineTest extends Describe {
export interface Suite {
(meta: ImportMeta): ((test: DefineTest) => void)
(meta: ImportMeta, define: (test: DefineTest) => void): void
// runtime: 'node' | 'deno' | 'bun'
}

export const suite: Suite
12 changes: 11 additions & 1 deletion src/suite.deno.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import {
assertStrictEquals,
assertThrows
} from '../node_modules/@jsr/std__assert/mod.js'
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
it as native
} from '../node_modules/@jsr/std__testing/bdd.js'

const native = Deno.test
const test = native.bind()
test.skip = native.ignore
test.only = native.only
Expand All @@ -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 ? define(test) : test)
4 changes: 4 additions & 0 deletions src/suite.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ 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 ? define(test) : test)
22 changes: 20 additions & 2 deletions suite.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import {suite} from '#suite'

const test = suite(import.meta)

let status = ''

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('before each', () => {
test.equal(status, 'beforeEach')
})

test('truthy', () => {
test.ok(true)
})
Expand Down Expand Up @@ -46,5 +64,5 @@ test('async', async () => {
})

/*test.only('only', () => {
test.ok(true)
})*/
test.ok(true)
})*/