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 5 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/[email protected]", "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/[email protected]", "https://npm.jsr.io/~/11/@jsr/std__async/1.0.10.tgz", {}, "sha512-/6RY097xGI2xBi8c83i0C/DSJnR7uymrzuuJu1RT4qr6DzNruV+dV+kLoTt+MQfu3TCAWraRbnpR5RN0LlSZQA=="],

"@jsr/std__data-structures": ["@jsr/[email protected]", "https://npm.jsr.io/~/11/@jsr/std__data-structures/1.0.6.tgz", {}, "sha512-Ejc8mHLuoYxXLu2zPquvqijdgQ19OV+1DdVDrLc/Cg+tiuGh4Dq2FSnLiPINh4lO1AJ3XcZcYPx38RxdsZcCOg=="],

"@jsr/std__fs": ["@jsr/[email protected]", "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/[email protected]", "https://npm.jsr.io/~/11/@jsr/std__internal/1.0.5.tgz", {}, "sha512-W2aN7UypJJ6EkfyXt5hBeIijBb8CmZk0jdjCzhOROr0SrXsXZ/V/9Qu1N0NxumFauPDAJz9NcP1mmZ9ZVSXwpQ=="],

"@jsr/std__path": ["@jsr/[email protected]", "https://npm.jsr.io/~/11/@jsr/std__path/1.0.8.tgz", {}, "sha512-eNBGlh/8ZVkMxtFH4bwIzlAeKoHYk5in4wrBZhi20zMdOiuX4QozP4+19mIXBT2lzHDjhuVLyECbhFeR304iDg=="],

"@jsr/std__testing": ["@jsr/[email protected]", "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/[email protected]", "", { "dependencies": { "bun-types": "1.1.9" } }, "sha512-i+mVz8C/lx+RprDR6Mr402iE1kmajgJPnmSfJ/NvU85sGGXSylYZ/6yc+XhVLr2E/t8o6HmjwV0evtnUOR0CFA=="],

"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~5.26.4" } }, "sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg=="],

"@types/ws": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A=="],

"bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "~20.12.8", "@types/ws": "~8.5.10" } }, "sha512-3YuLiH4Ne/ghk7K6mHiaqCqKOMrtB0Z5p1WAskHSVgi0iMZgsARV4yGkbfi565YsStvUq6GXTWB3ga7M8cznkA=="],

"typescript": ["[email protected]", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw=="],

"undici-types": ["[email protected]", "", {}, "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
18 changes: 18 additions & 0 deletions src/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function createSuite(setup) {
return (meta, define) => {
const test = setup(meta)
if (typeof define === 'function') return define(test)
const ctx = define
if (typeof ctx === 'object') {
if ('beforeAll' in ctx) test.beforeAll(ctx.beforeAll.bind(ctx))
if ('beforeEach' in ctx) test.beforeEach(ctx.beforeEach.bind(ctx))
if ('afterEach' in ctx) test.afterEach(ctx.afterEach.bind(ctx))
if ('afterAll' in ctx) test.afterAll(ctx.afterAll.bind(ctx))
}
return Object.assign((name, run) => {
return test(name, () => {
return run(ctx)
})
}, test)
}
}
9 changes: 7 additions & 2 deletions src/suite.bun.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {createSuite} from './shared.js'

export function setup(meta) {
const native = Bun.jest(meta.path)
const test = native.test.bind()
Expand All @@ -15,8 +17,11 @@ 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
}

export const suite = (meta, define) =>
define ? define(setup(meta)) : setup(meta)
export const suite = createSuite(setup)
35 changes: 25 additions & 10 deletions src/suite.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/** Describe a test */
export type Describe = (name: string, run: () => void | Promise<void>) => void
export interface Describe<Context>{
(name: string, run: (context: Context) => void | Promise<void>): void
}

/** Define a test suite */
export interface DefineTest extends Describe {
/** Skip the test */
skip: Describe
/** Only run this test */
only: Describe
interface Assert {
/** Assert that actual is a truthy value */
ok(actual: any): void
/** Assert that actual strictly equals (===) the expects value */
Expand All @@ -26,11 +23,29 @@ export interface DefineTest extends Describe {
}
}

interface Hooks {
/** 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
}

/** Define a test suite */
export interface DefineTest<Context> extends Describe<Context>, Assert {
/** Skip the test */
skip: Describe<Context>
/** Only run this test */
only: Describe<Context>
}

/** Define a test suite */
export interface Suite {
(meta: ImportMeta): ((test: DefineTest) => void)
(meta: ImportMeta, define: (test: DefineTest) => void): void
// runtime: 'node' | 'deno' | 'bun'
<Context extends Hooks>(meta: ImportMeta, context?: Context): DefineTest<Context>
(meta: ImportMeta, define: (test: DefineTest<undefined>) => void): void
}

export const suite: Suite
15 changes: 13 additions & 2 deletions src/suite.deno.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ 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'
import {createSuite} from './shared.js'

const native = Deno.test
const test = native.bind()
test.skip = native.ignore
test.only = native.only
Expand All @@ -21,5 +28,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)
export const suite = createSuite(() => test)
7 changes: 6 additions & 1 deletion src/suite.node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as asserts from 'node:assert'
import * as native from 'node:test'
import {createSuite} from './shared.js'

const test = native.test.bind()
test.skip = native.skip
Expand All @@ -13,5 +14,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)
export const suite = createSuite(() => test)
28 changes: 25 additions & 3 deletions suite.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import {suite} from '#suite'
const test = suite(import.meta)

const test = suite(import.meta, {
status: '',
beforeAll() {
this.status = 'beforeAll'
},
beforeEach() {
if (this.status !== 'beforeAll' && this.status !== 'afterEach')
throw new Error(`hooks did not run, ${this.status}`)
this.status = 'beforeEach'
},
afterEach() {
this.status = 'afterEach'
},
afterAll() {
if (this.status !== 'afterEach')
throw new Error(`hooks did not run, ${this.status}`)
}
})

test('before each', ctx => {
test.equal(ctx.status, 'beforeEach')
})

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

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