From 2f01af35c61ae604c3abf9b8ada0139886b16d21 Mon Sep 17 00:00:00 2001 From: Ben Merckx Date: Tue, 21 Jan 2025 10:48:23 +0100 Subject: [PATCH] Export test function if we don't define the suite --- README.md | 38 ++++++++++++------------ src/suite.bun.js | 3 +- src/suite.d.ts | 1 + src/suite.deno.js | 2 +- src/suite.node.js | 2 +- suite.test.js | 75 +++++++++++++++++++++++------------------------ 6 files changed, 61 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index bf11499..bdf9c1a 100644 --- a/README.md +++ b/README.md @@ -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) }) ```` diff --git a/src/suite.bun.js b/src/suite.bun.js index 65f2ba5..710f535 100644 --- a/src/suite.bun.js +++ b/src/suite.bun.js @@ -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) diff --git a/src/suite.d.ts b/src/suite.d.ts index d8e75ad..2a0c2bc 100644 --- a/src/suite.d.ts +++ b/src/suite.d.ts @@ -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' } diff --git a/src/suite.deno.js b/src/suite.deno.js index 7a333b2..c208780 100644 --- a/src/suite.deno.js +++ b/src/suite.deno.js @@ -22,4 +22,4 @@ test.not = { equal: assertNotEquals } -export const suite = (meta, define) => define(test) +export const suite = (meta, define) => (define ? define(test) : test) diff --git a/src/suite.node.js b/src/suite.node.js index 23c2347..04b0957 100644 --- a/src/suite.node.js +++ b/src/suite.node.js @@ -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) diff --git a/suite.test.js b/suite.test.js index 7c5e7b4..a3f7464 100644 --- a/suite.test.js +++ b/suite.test.js @@ -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) +})*/