Skip to content

Commit

Permalink
Export test function if we don't define the suite
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Jan 21, 2025
1 parent 36040f1 commit 2f01af3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 60 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ Describe tests that run in the native test runners of Node.js, Deno and Bun.
// example.test.js
import {suite} from '@alinea/suite'

suite(import.meta, test => {
test('is', () => {
test.is(1, 1)
})
const test = suite(import.meta)

test('deep equal', () => {
test.equal({a: 1}, {a: 1})
})
test('is', () => {
test.is(1, 1)
})

test('async', async () => {
await new Promise(resolve => setTimeout(resolve, 100))
test.ok(true)
})
test('deep equal', () => {
test.equal({a: 1}, {a: 1})
})

test('throws', () => {
test.throws(() => {
throw new Error('test')
}, 'test')
})
test('async', async () => {
await new Promise(resolve => setTimeout(resolve, 100))
test.ok(true)
})

test('throws', () => {
test.throws(() => {
throw new Error('test')
}, 'test')
})

test.skip('skip', () => {
test.ok(false)
})
test.skip('skip', () => {
test.ok(false)
})
````

Expand Down
3 changes: 2 additions & 1 deletion src/suite.bun.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export function setup(meta) {
return test
}

export const suite = (meta, define) => define(setup(meta))
export const suite = (meta, define) =>
define ? define(setup(meta)) : setup(meta)
1 change: 1 addition & 0 deletions src/suite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface DefineTest extends Describe {

/** Define a test suite */
export interface Suite {
(meta: ImportMeta): ((test: DefineTest) => void)
(meta: ImportMeta, define: (test: DefineTest) => void): void
// runtime: 'node' | 'deno' | 'bun'
}
Expand Down
2 changes: 1 addition & 1 deletion src/suite.deno.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ test.not = {
equal: assertNotEquals
}

export const suite = (meta, define) => define(test)
export const suite = (meta, define) => (define ? define(test) : test)
2 changes: 1 addition & 1 deletion src/suite.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ test.not = {
equal: asserts.notDeepStrictEqual
}

export const suite = (meta, define) => define(test)
export const suite = (meta, define) => (define ? define(test) : test)
75 changes: 37 additions & 38 deletions suite.test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
import {suite} from '#suite'
const test = suite(import.meta)

suite(import.meta, test => {
test('truthy', () => {
test.ok(true)
})

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)
})*/

0 comments on commit 2f01af3

Please sign in to comment.