From d9b6330b1143c6fc23ce187b69b51825c92c942c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weslley=20Ara=C3=BAjo?= <46850407+wellwelwel@users.noreply.github.com> Date: Mon, 17 Jun 2024 18:15:07 -0300 Subject: [PATCH] ci: improve tests (#407) --- README.md | 3 +- test/e2e/background-process.test.ts | 184 +++--- test/e2e/cli.test.ts | 5 +- test/e2e/exit-code.test.ts | 156 ++--- .../assert/assert-no-message.test.ts | 360 +++++----- .../assert/assert-promise-no-message.test.ts | 362 ++++++----- .../integration/assert/assert-promise.test.ts | 614 +++++++++--------- test/integration/assert/assert.test.ts | 614 +++++++++--------- test/integration/import.test.ts | 32 +- test/integration/it/each/each-promise.test.ts | 63 +- test/integration/it/each/each.test.ts | 70 +- test/integration/it/it.test.ts | 46 +- .../test/each/each-promise.test.ts | 97 +-- test/integration/test/each/each.test.ts | 95 +-- test/integration/test/test.test.ts | 7 +- test/unit/assert-result-type.test.ts | 5 +- test/unit/deno/allow.test.ts | 5 +- test/unit/deno/cjs.test.ts | 54 +- test/unit/deno/deny.test.ts | 5 +- test/unit/map-tests.test.ts | 12 +- test/unit/run-test-file.test.ts | 30 +- test/unit/run-tests.test.ts | 34 +- website/docs/index.mdx | 20 +- 23 files changed, 1379 insertions(+), 1494 deletions(-) diff --git a/README.md b/README.md index aa4d8fd1..0ec953bd 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Enjoying **Poku**? Give him a star to show your support 🌟 check Easier and Less Verbose
     check [**Node.js**][node-version-url] familiar **API**
+     check Instantly re-run related tests in `watch` mode
     check Run **CJS** (**CommonJS**) files directly with [**Deno**][deno-version-url]
     check Easily handle, **servers**, **services**, **processes**, and **ports**
@@ -174,8 +175,8 @@ deno run npm:poku - [**test**](https://poku.io/docs/documentation/helpers/test) , [**describe**](https://poku.io/docs/documentation/helpers/describe) and [**it**](https://poku.io/docs/documentation/helpers/it) _(organize, group, and isolate tests)_ +- [**watch**](https://poku.io/docs/documentation/poku/options/watch) _(watch for changes and re-run related test files)_ - [**beforeEach**](https://poku.io/docs/category/beforeeach-and-aftereach) and [**afterEach**](https://poku.io/docs/category/beforeeach-and-aftereach) _(hooks for test setup and teardown)_ -- [**watch**](https://poku.io/docs/documentation/poku/options/watch) _(watch test files for changes)_ - [**startScript**](https://poku.io/docs/documentation/startScript) _(run **package.json** scripts in background)_ - [**startService**](https://poku.io/docs/documentation/startService) _(run files in background)_ - [**kill**](https://poku.io/docs/documentation/processes/kill) _(terminate ports, port ranges, and PIDs)_ diff --git a/test/e2e/background-process.test.ts b/test/e2e/background-process.test.ts index 8ee6048e..cffaa3ab 100644 --- a/test/e2e/background-process.test.ts +++ b/test/e2e/background-process.test.ts @@ -1,5 +1,5 @@ import { describe } from '../../src/modules/describe.js'; -import { test } from '../../src/modules/test.js'; +import { it } from '../../src/modules/it.js'; import { assert } from '../../src/modules/assert.js'; import { startScript, startService } from '../../src/modules/create-service.js'; import { legacyFetch } from '../helpers/legacy-fetch.test.js'; @@ -9,102 +9,55 @@ import { getRuntime } from '../../src/helpers/get-runtime.js'; (async () => { const runtime = getRuntime(); - await test(async () => { - describe('Start Service (Single Port)', { icon: '🔀' }); - - const server = await startService(`server-a.${ext}`, { - startAfter: 'ready', - cwd: - ext === 'ts' || isProduction ? 'fixtures/server' : 'ci/fixtures/server', - }); - - const res = await legacyFetch('localhost', 4000); - - assert.strictEqual(res?.statusCode, 200, 'Service is on'); - assert.deepStrictEqual( - JSON.parse(res?.body), - { name: 'Poku' }, - 'Poku service is online' - ); + await describe('Start Service (Single Port)', async () => { + await it(async () => { + const server = await startService(`server-a.${ext}`, { + startAfter: 'ready', + cwd: + ext === 'ts' || isProduction + ? 'fixtures/server' + : 'ci/fixtures/server', + }); - await server.end(4000); - }); + const res = await legacyFetch('localhost', 4000); - await test(async () => { - describe('Start Script (Single Port)', { icon: '🔀' }); + assert.strictEqual(res?.statusCode, 200, 'Service is on'); + assert.deepStrictEqual( + JSON.parse(res?.body), + { name: 'Poku' }, + 'Poku service is online' + ); - const server = await startScript(`start:${ext}`, { - startAfter: 'ready', - cwd: - ext === 'ts' || isProduction ? 'fixtures/server' : 'ci/fixtures/server', - runner: runtime === 'node' ? 'npm' : runtime, + await server.end(4000); }); - - const res = await legacyFetch('localhost', 4001); - - assert.strictEqual(res?.statusCode, 200, 'Script is on'); - assert.deepStrictEqual( - JSON.parse(res?.body), - { name: 'Poku' }, - 'Poku script is online' - ); - - await server.end(4001); }); - await test(async () => { - describe('Start Service (Multiple Ports)', { - icon: '🔀', - }); - - const server = await startService(`server-a.${ext}`, { - startAfter: 'ready', - cwd: - ext === 'ts' || isProduction ? 'fixtures/server' : 'ci/fixtures/server', - }); - - const res = await legacyFetch('localhost', 4000); - - assert.strictEqual(res?.statusCode, 200, 'Service is on'); - assert.deepStrictEqual( - JSON.parse(res?.body), - { name: 'Poku' }, - 'Poku service is online' - ); + await describe('Start Script (Single Port)', async () => { + await it(async () => { + const server = await startScript(`start:${ext}`, { + startAfter: 'ready', + cwd: + ext === 'ts' || isProduction + ? 'fixtures/server' + : 'ci/fixtures/server', + runner: runtime === 'node' ? 'npm' : runtime, + }); - await server.end([4000]); - }); + const res = await legacyFetch('localhost', 4001); - await test(async () => { - describe('Start Script (Multiple Ports)', { - icon: '🔀', - }); + assert.strictEqual(res?.statusCode, 200, 'Script is on'); + assert.deepStrictEqual( + JSON.parse(res?.body), + { name: 'Poku' }, + 'Poku script is online' + ); - const server = await startScript(`start:${ext}`, { - startAfter: 'ready', - cwd: - ext === 'ts' || isProduction ? 'fixtures/server' : 'ci/fixtures/server', - runner: runtime === 'node' ? 'npm' : runtime, + await server.end(4001); }); - - const res = await legacyFetch('localhost', 4001); - - assert.strictEqual(res?.statusCode, 200, 'Script is on'); - assert.deepStrictEqual( - JSON.parse(res?.body), - { name: 'Poku' }, - 'Poku script is online' - ); - - await server.end([4001]); }); - if (runtime === 'node') { - await test(async () => { - describe('Start Service (No Ports)', { - icon: '🔀', - }); - + await describe('Start Service (Multiple Ports)', async () => { + await it(async () => { const server = await startService(`server-a.${ext}`, { startAfter: 'ready', cwd: @@ -122,14 +75,12 @@ import { getRuntime } from '../../src/helpers/get-runtime.js'; 'Poku service is online' ); - await server.end(); + await server.end([4000]); }); + }); - await test(async () => { - describe('Start Script (No Ports)', { - icon: '🔀', - }); - + await describe('Start Script (Multiple Ports)', async () => { + await it(async () => { const server = await startScript(`start:${ext}`, { startAfter: 'ready', cwd: @@ -148,7 +99,56 @@ import { getRuntime } from '../../src/helpers/get-runtime.js'; 'Poku script is online' ); - await server.end(); + await server.end([4001]); + }); + }); + + if (runtime === 'node') { + await describe('Start Service (No Ports)', async () => { + await it(async () => { + const server = await startService(`server-a.${ext}`, { + startAfter: 'ready', + cwd: + ext === 'ts' || isProduction + ? 'fixtures/server' + : 'ci/fixtures/server', + }); + + const res = await legacyFetch('localhost', 4000); + + assert.strictEqual(res?.statusCode, 200, 'Service is on'); + assert.deepStrictEqual( + JSON.parse(res?.body), + { name: 'Poku' }, + 'Poku service is online' + ); + + await server.end(); + }); + }); + + await describe('Start Script (No Ports)', async () => { + await it(async () => { + const server = await startScript(`start:${ext}`, { + startAfter: 'ready', + cwd: + ext === 'ts' || isProduction + ? 'fixtures/server' + : 'ci/fixtures/server', + runner: runtime === 'node' ? 'npm' : runtime, + }); + + const res = await legacyFetch('localhost', 4001); + + assert.strictEqual(res?.statusCode, 200, 'Script is on'); + assert.deepStrictEqual( + JSON.parse(res?.body), + { name: 'Poku' }, + 'Poku script is online' + ); + + await server.end(); + }); }); } })(); diff --git a/test/e2e/cli.test.ts b/test/e2e/cli.test.ts index 8856d4ab..a46a4a6a 100644 --- a/test/e2e/cli.test.ts +++ b/test/e2e/cli.test.ts @@ -1,4 +1,3 @@ -import { describe } from '../../src/modules/describe.js'; import { test } from '../../src/modules/test.js'; import { assert } from '../../src/modules/assert.js'; import { executeCLI, ext, isProduction } from '../helpers/capture-cli.test.js'; @@ -8,9 +7,7 @@ const runtime = getRuntime(); if (runtime === 'deno' && !isProduction) process.exit(0); -test(async () => { - describe('Poku Test Runner: CLI', { icon: '🐷' }); - +test('Poku Test Runner: CLI', async () => { const output = await executeCLI([ ext === 'ts' || isProduction ? `src/bin/index.${ext}` diff --git a/test/e2e/exit-code.test.ts b/test/e2e/exit-code.test.ts index 6fd9eb51..ad873853 100644 --- a/test/e2e/exit-code.test.ts +++ b/test/e2e/exit-code.test.ts @@ -1,82 +1,84 @@ import { describe } from '../../src/modules/describe.js'; -import { test } from '../../src/modules/test.js'; +import { it } from '../../src/modules/it.js'; import { poku } from '../../src/modules/poku.js'; import { assert } from '../../src/modules/assert.js'; -describe('Poku Runner Suite', { icon: '🐷' }); - -test(async () => { - const code = await poku(['./fixtures/success', 'fixtures/fail'], { - noExit: true, - quiet: true, - }); - - assert.deepStrictEqual(code, 1, 'Testing all paths as a string array'); -}); - -test(async () => { - const code = await poku('./fixtures/fail', { - noExit: true, - quiet: true, - }); - - assert.deepStrictEqual(code, 1, 'Testing a fail path as string'); -}); - -test(async () => { - const code = await poku('./fixtures/success', { - noExit: true, - quiet: true, - }); - - assert.deepStrictEqual(code, 0, 'Testing a success path as string'); -}); - -test(async () => { - const code = await poku(['./fixtures/success'], { - noExit: true, - quiet: true, - }); - - assert.deepStrictEqual(code, 0); -}); - -test(async () => { - const code = await poku(['./fixtures/success', 'fixtures/fail'], { - noExit: true, - filter: /success/, - quiet: true, - }); - - assert.deepStrictEqual(code, 0, 'Filter paths that contains "success"'); -}); - -test(async () => { - const code = await poku(['./fixtures/success', 'fixtures/fail'], { - noExit: true, - filter: /fail/, - quiet: true, - }); - - assert.deepStrictEqual(code, 1, 'Filter paths that contains "fail"'); -}); - -test(async () => { - const code = await poku(['fixtures/fail'], { - noExit: true, - filter: /success/, - quiet: true, - }); - - assert.deepStrictEqual(code, 0, 'No files (success filter)'); -}); - -test(async () => { - const code = await poku(['./fixtures/success', 'fixtures/fail'], { - noExit: true, - filter: /\.(m)?(j|t)?s$/, - quiet: true, - }); - - assert.deepStrictEqual(code, 1, 'Filter by extension'); +describe('Poku Runner Suite', async () => { + await Promise.all([ + it(async () => { + const code = await poku(['./fixtures/success', 'fixtures/fail'], { + noExit: true, + quiet: true, + }); + + assert.deepStrictEqual(code, 1, 'Testing all paths as a string array'); + }), + + it(async () => { + const code = await poku('./fixtures/fail', { + noExit: true, + quiet: true, + }); + + assert.deepStrictEqual(code, 1, 'Testing a fail path as string'); + }), + + it(async () => { + const code = await poku('./fixtures/success', { + noExit: true, + quiet: true, + }); + + assert.deepStrictEqual(code, 0, 'Testing a success path as string'); + }), + + it(async () => { + const code = await poku(['./fixtures/success'], { + noExit: true, + quiet: true, + }); + + assert.deepStrictEqual(code, 0); + }), + + it(async () => { + const code = await poku(['./fixtures/success', 'fixtures/fail'], { + noExit: true, + filter: /success/, + quiet: true, + }); + + assert.deepStrictEqual(code, 0, 'Filter paths that contains "success"'); + }), + + it(async () => { + const code = await poku(['./fixtures/success', 'fixtures/fail'], { + noExit: true, + filter: /fail/, + quiet: true, + }); + + assert.deepStrictEqual(code, 1, 'Filter paths that contains "fail"'); + }), + + it(async () => { + const code = await poku(['fixtures/fail'], { + noExit: true, + filter: /success/, + quiet: true, + }); + + assert.deepStrictEqual(code, 0, 'No files (success filter)'); + }), + + it(async () => { + const code = await poku(['./fixtures/success', 'fixtures/fail'], { + noExit: true, + filter: /\.(m)?(j|t)?s$/, + quiet: true, + }); + + assert.deepStrictEqual(code, 1, 'Filter by extension'); + }), + ]); }); diff --git a/test/integration/assert/assert-no-message.test.ts b/test/integration/assert/assert-no-message.test.ts index 7c30cccc..95187837 100644 --- a/test/integration/assert/assert-no-message.test.ts +++ b/test/integration/assert/assert-no-message.test.ts @@ -1,188 +1,192 @@ +import { describe } from '../../../src/modules/describe.js'; +import { it } from '../../../src/modules/it.js'; +import { assert } from '../../../src/modules/assert.js'; import { nodeVersion } from '../../../src/helpers/get-runtime.js'; -import { assert, describe, test } from '../../../src/index.js'; - -describe('Assert Suite (No Message)', { icon: '🔬' }); - -test(() => { - assert(true); - assert(1); - assert('string'); - assert([]); - assert({}); - assert(() => {}); - assert(3 > 2); -}); - -test(() => { - assert.ok(true); - assert.ok(1); - assert.ok('string'); - assert.ok([]); - assert.ok({}); - assert.ok(() => {}); - assert.ok(3 > 2); -}); - -test(() => { - assert.equal(1, 1); - assert.equal('text', 'text'); - assert.equal(2 + 2, 4); - assert.equal('Hello'.toUpperCase(), 'HELLO'); -}); - -test(() => { - assert.deepEqual({ a: 1 }, { a: 1 }); - assert.deepEqual([1, 2], [1, 2]); - assert.deepEqual([2, 3, 4], [2, 3, 4]); - assert.deepEqual([1, 2, 3].reverse(), [3, 2, 1]); -}); - -test(() => { - assert.strictEqual(1, 1); - assert.strictEqual('text', 'text'); - assert.strictEqual(2 * 2, 4); -}); - -test(() => { - assert.deepStrictEqual({ a: 1 }, { a: 1 }); - assert.deepStrictEqual([1, 2], [1, 2]); - assert.deepStrictEqual({ a: 1, b: 2 }, { a: 1, b: 2 }); -}); - -test(() => { - assert.doesNotThrow(() => 1 + 1); -}); - -test(() => { - assert.notEqual(1, 2); - assert.notEqual(2 + 2, 5); - assert.notEqual('Hello'.toLowerCase(), 'HELLO'); -}); - -test(() => { - assert.notStrictEqual(1, true); - assert.notStrictEqual(1, '1'); - assert.notStrictEqual(2 * 2, '4'); - assert.notStrictEqual(2, '2'); -}); - -test(() => { - assert.notDeepEqual({ a: 1 }, { a: 2 }); - assert.notDeepEqual([2, 3, 4], [4, 5, 6]); -}); - -test(() => { - assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); - assert.notDeepStrictEqual([1, 2, 3], [1, 2, '3']); - assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); -}); - -const callbackFunction = (cb: (err: Error | null, result?: string) => void) => { - cb(null, 'no error'); -}; -test(() => { - assert.ifError(null); - assert.ifError(undefined); -}); +describe('Assert Suite (No Message)', () => { + it(() => { + assert(true); + assert(1); + assert('string'); + assert([]); + assert({}); + assert(() => {}); + assert(3 > 2); + }); + + it(() => { + assert.ok(true); + assert.ok(1); + assert.ok('string'); + assert.ok([]); + assert.ok({}); + assert.ok(() => {}); + assert.ok(3 > 2); + }); + + it(() => { + assert.equal(1, 1); + assert.equal('text', 'text'); + assert.equal(2 + 2, 4); + assert.equal('Hello'.toUpperCase(), 'HELLO'); + }); + + it(() => { + assert.deepEqual({ a: 1 }, { a: 1 }); + assert.deepEqual([1, 2], [1, 2]); + assert.deepEqual([2, 3, 4], [2, 3, 4]); + assert.deepEqual([1, 2, 3].reverse(), [3, 2, 1]); + }); + + it(() => { + assert.strictEqual(1, 1); + assert.strictEqual('text', 'text'); + assert.strictEqual(2 * 2, 4); + }); + + it(() => { + assert.deepStrictEqual({ a: 1 }, { a: 1 }); + assert.deepStrictEqual([1, 2], [1, 2]); + assert.deepStrictEqual({ a: 1, b: 2 }, { a: 1, b: 2 }); + }); + + it(() => { + assert.doesNotThrow(() => 1 + 1); + }); + + it(() => { + assert.notEqual(1, 2); + assert.notEqual(2 + 2, 5); + assert.notEqual('Hello'.toLowerCase(), 'HELLO'); + }); + + it(() => { + assert.notStrictEqual(1, true); + assert.notStrictEqual(1, '1'); + assert.notStrictEqual(2 * 2, '4'); + assert.notStrictEqual(2, '2'); + }); + + it(() => { + assert.notDeepEqual({ a: 1 }, { a: 2 }); + assert.notDeepEqual([2, 3, 4], [4, 5, 6]); + }); + + it(() => { + assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); + assert.notDeepStrictEqual([1, 2, 3], [1, 2, '3']); + assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); + }); + + const callbackFunction = ( + cb: (err: Error | null, result?: string) => void + ) => { + cb(null, 'no error'); + }; + + it(() => { + assert.ifError(null); + assert.ifError(undefined); + }); + + it(() => { + if (!nodeVersion || nodeVersion > 8) { + const obj = { a: 1 }; + + const functionThatThrows = () => { + throw new Error('Specific error'); + }; + + it(() => { + assert.throws(() => { + throw new Error('error'); + }); + assert.throws(() => { + throw new Error('Test error'); + }); + assert.throws(() => { + throw new Error('Test error'); + }); + assert.throws(functionThatThrows, new Error('Specific error')); + assert.throws(functionThatThrows, /Specific error/); + assert.throws( + functionThatThrows, + (err) => err instanceof Error && err.message === 'Specific error' + ); + }); -test(() => { - if (!nodeVersion || nodeVersion > 8) { - const obj = { a: 1 }; + it(() => { + assert.doesNotThrow(() => { + obj.a = 2; + }); + assert.strictEqual(obj.a, 2); + assert.doesNotThrow(() => { + return 42; + }); + assert.doesNotThrow(() => + callbackFunction((err) => { + assert.ifError(err); + }) + ); + assert.doesNotThrow(() => 42); + assert.doesNotThrow(() => 'no error'); + }); + } + }); - const functionThatThrows = () => { - throw new Error('Specific error'); - }; + it(() => { + if (!nodeVersion || nodeVersion > 12) { + const text = 'sample text'; - test(() => { - assert.throws(() => { - throw new Error('error'); - }); - assert.throws(() => { - throw new Error('Test error'); + it(() => { + assert.match(text, /sample/); }); - assert.throws(() => { - throw new Error('Test error'); - }); - assert.throws(functionThatThrows, new Error('Specific error')); - assert.throws(functionThatThrows, /Specific error/); - assert.throws( - functionThatThrows, - (err) => err instanceof Error && err.message === 'Specific error' - ); - }); - - test(() => { - assert.doesNotThrow(() => { - obj.a = 2; + + it(() => { + assert.doesNotMatch(text, /notpresent/); + assert.doesNotMatch('abc', /123/); + assert.doesNotMatch('', /\d/); + assert.doesNotMatch('abc', /\d+/); }); - assert.strictEqual(obj.a, 2); - assert.doesNotThrow(() => { - return 42; + } + }); + + it(() => { + if (!nodeVersion || nodeVersion > 10) { + const asyncFunctionThatRejects = async () => + await Promise.reject(new Error('Async error')); + + const asyncFunctionThatResolves = () => + Promise.resolve('Resolved successfully'); + + const asyncFunctionThatFails = () => + new Promise((_, reject) => reject(new Error('Failed'))); + + const asyncFunctionThatCouldReject = () => + new Promise((resolve) => resolve(undefined)); + + it(() => { + assert.rejects( + async () => await asyncFunctionThatFails(), + new Error('Failed') + ); + assert.rejects(asyncFunctionThatRejects, new Error('Async error')); + assert.rejects( + () => Promise.reject('Simple rejection'), + (err) => err === 'Simple rejection' + ); + assert.rejects(asyncFunctionThatRejects, new Error('Async error')); }); - assert.doesNotThrow(() => - callbackFunction((err) => { - assert.ifError(err); - }) - ); - assert.doesNotThrow(() => 42); - assert.doesNotThrow(() => 'no error'); - }); - } -}); - -test(() => { - if (!nodeVersion || nodeVersion > 12) { - const text = 'sample text'; - - test(() => { - assert.match(text, /sample/); - }); - - test(() => { - assert.doesNotMatch(text, /notpresent/); - assert.doesNotMatch('abc', /123/); - assert.doesNotMatch('', /\d/); - assert.doesNotMatch('abc', /\d+/); - }); - } -}); -test(() => { - if (!nodeVersion || nodeVersion > 10) { - const asyncFunctionThatRejects = async () => - await Promise.reject(new Error('Async error')); - - const asyncFunctionThatResolves = () => - Promise.resolve('Resolved successfully'); - - const asyncFunctionThatFails = () => - new Promise((_, reject) => reject(new Error('Failed'))); - - const asyncFunctionThatCouldReject = () => - new Promise((resolve) => resolve(undefined)); - - test(() => { - assert.rejects( - async () => await asyncFunctionThatFails(), - new Error('Failed') - ); - assert.rejects(asyncFunctionThatRejects, new Error('Async error')); - assert.rejects( - () => Promise.reject('Simple rejection'), - (err) => err === 'Simple rejection' - ); - assert.rejects(asyncFunctionThatRejects, new Error('Async error')); - }); - - test(() => { - assert.doesNotReject(asyncFunctionThatResolves); - assert.doesNotReject(Promise.resolve('Immediate resolve')); - assert.doesNotReject(asyncFunctionThatCouldReject); - assert.doesNotReject(() => - Promise.resolve('Async function with no rejection') - ); - assert.doesNotReject(asyncFunctionThatResolves); - }); - } + it(() => { + assert.doesNotReject(asyncFunctionThatResolves); + assert.doesNotReject(Promise.resolve('Immediate resolve')); + assert.doesNotReject(asyncFunctionThatCouldReject); + assert.doesNotReject(() => + Promise.resolve('Async function with no rejection') + ); + assert.doesNotReject(asyncFunctionThatResolves); + }); + } + }); }); diff --git a/test/integration/assert/assert-promise-no-message.test.ts b/test/integration/assert/assert-promise-no-message.test.ts index 86821a53..d2bf26ed 100644 --- a/test/integration/assert/assert-promise-no-message.test.ts +++ b/test/integration/assert/assert-promise-no-message.test.ts @@ -1,190 +1,192 @@ +import { describe } from '../../../src/modules/describe.js'; +import { it } from '../../../src/modules/it.js'; +import { assertPromise as assert } from '../../../src/modules/assert-promise.js'; import { nodeVersion } from '../../../src/helpers/get-runtime.js'; -import { assertPromise as assert, describe, test } from '../../../src/index.js'; -describe('Assert Promise Suite (No Message)', { - icon: '🔬', -}); - -test(() => { - assert(true); - assert(1); - assert('string'); - assert([]); - assert({}); - assert(() => {}); - assert(3 > 2); -}); - -test(() => { - assert.ok(true); - assert.ok(1); - assert.ok('string'); - assert.ok([]); - assert.ok({}); - assert.ok(() => {}); - assert.ok(3 > 2); -}); - -test(() => { - assert.equal(1, 1); - assert.equal('text', 'text'); - assert.equal(2 + 2, 4); - assert.equal('Hello'.toUpperCase(), 'HELLO'); -}); - -test(() => { - assert.deepEqual({ a: 1 }, { a: 1 }); - assert.deepEqual([1, 2], [1, 2]); - assert.deepEqual([2, 3, 4], [2, 3, 4]); - assert.deepEqual([1, 2, 3].reverse(), [3, 2, 1]); -}); - -test(() => { - assert.strictEqual(1, 1); - assert.strictEqual('text', 'text'); - assert.strictEqual(2 * 2, 4); -}); - -test(() => { - assert.deepStrictEqual({ a: 1 }, { a: 1 }); - assert.deepStrictEqual([1, 2], [1, 2]); - assert.deepStrictEqual({ a: 1, b: 2 }, { a: 1, b: 2 }); -}); - -test(() => { - assert.doesNotThrow(() => 1 + 1); -}); - -test(() => { - assert.notEqual(1, 2); - assert.notEqual(2 + 2, 5); - assert.notEqual('Hello'.toLowerCase(), 'HELLO'); -}); - -test(() => { - assert.notStrictEqual(1, true); - assert.notStrictEqual(1, '1'); - assert.notStrictEqual(2 * 2, '4'); - assert.notStrictEqual(2, '2'); -}); - -test(() => { - assert.notDeepEqual({ a: 1 }, { a: 2 }); - assert.notDeepEqual([2, 3, 4], [4, 5, 6]); -}); - -test(() => { - assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); - assert.notDeepStrictEqual([1, 2, 3], [1, 2, '3']); - assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); -}); - -const callbackFunction = (cb: (err: Error | null, result?: string) => void) => { - cb(null, 'no error'); -}; - -test(() => { - assert.ifError(null); - assert.ifError(undefined); -}); +describe('Assert Promise Suite (No Message)', () => { + it(() => { + assert(true); + assert(1); + assert('string'); + assert([]); + assert({}); + assert(() => {}); + assert(3 > 2); + }); + + it(() => { + assert.ok(true); + assert.ok(1); + assert.ok('string'); + assert.ok([]); + assert.ok({}); + assert.ok(() => {}); + assert.ok(3 > 2); + }); + + it(() => { + assert.equal(1, 1); + assert.equal('text', 'text'); + assert.equal(2 + 2, 4); + assert.equal('Hello'.toUpperCase(), 'HELLO'); + }); + + it(() => { + assert.deepEqual({ a: 1 }, { a: 1 }); + assert.deepEqual([1, 2], [1, 2]); + assert.deepEqual([2, 3, 4], [2, 3, 4]); + assert.deepEqual([1, 2, 3].reverse(), [3, 2, 1]); + }); + + it(() => { + assert.strictEqual(1, 1); + assert.strictEqual('text', 'text'); + assert.strictEqual(2 * 2, 4); + }); + + it(() => { + assert.deepStrictEqual({ a: 1 }, { a: 1 }); + assert.deepStrictEqual([1, 2], [1, 2]); + assert.deepStrictEqual({ a: 1, b: 2 }, { a: 1, b: 2 }); + }); + + it(() => { + assert.doesNotThrow(() => 1 + 1); + }); + + it(() => { + assert.notEqual(1, 2); + assert.notEqual(2 + 2, 5); + assert.notEqual('Hello'.toLowerCase(), 'HELLO'); + }); + + it(() => { + assert.notStrictEqual(1, true); + assert.notStrictEqual(1, '1'); + assert.notStrictEqual(2 * 2, '4'); + assert.notStrictEqual(2, '2'); + }); + + it(() => { + assert.notDeepEqual({ a: 1 }, { a: 2 }); + assert.notDeepEqual([2, 3, 4], [4, 5, 6]); + }); + + it(() => { + assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); + assert.notDeepStrictEqual([1, 2, 3], [1, 2, '3']); + assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); + }); + + const callbackFunction = ( + cb: (err: Error | null, result?: string) => void + ) => { + cb(null, 'no error'); + }; + + it(() => { + assert.ifError(null); + assert.ifError(undefined); + }); + + it(() => { + if (!nodeVersion || nodeVersion > 8) { + const obj = { a: 1 }; + + const functionThatThrows = () => { + throw new Error('Specific error'); + }; + + it(() => { + assert.throws(() => { + throw new Error('error'); + }); + assert.throws(() => { + throw new Error('Test error'); + }); + assert.throws(() => { + throw new Error('Test error'); + }); + assert.throws(functionThatThrows, new Error('Specific error')); + assert.throws(functionThatThrows, /Specific error/); + assert.throws( + functionThatThrows, + (err) => err instanceof Error && err.message === 'Specific error' + ); + }); -test(() => { - if (!nodeVersion || nodeVersion > 8) { - const obj = { a: 1 }; + it(() => { + assert.doesNotThrow(() => { + obj.a = 2; + }); + assert.strictEqual(obj.a, 2); + assert.doesNotThrow(() => { + return 42; + }); + assert.doesNotThrow(() => + callbackFunction((err) => { + assert.ifError(err); + }) + ); + assert.doesNotThrow(() => 42); + assert.doesNotThrow(() => 'no error'); + }); + } + }); - const functionThatThrows = () => { - throw new Error('Specific error'); - }; + it(() => { + if (!nodeVersion || nodeVersion > 12) { + const text = 'sample text'; - test(() => { - assert.throws(() => { - throw new Error('error'); - }); - assert.throws(() => { - throw new Error('Test error'); + it(() => { + assert.match(text, /sample/); }); - assert.throws(() => { - throw new Error('Test error'); - }); - assert.throws(functionThatThrows, new Error('Specific error')); - assert.throws(functionThatThrows, /Specific error/); - assert.throws( - functionThatThrows, - (err) => err instanceof Error && err.message === 'Specific error' - ); - }); - - test(() => { - assert.doesNotThrow(() => { - obj.a = 2; + + it(() => { + assert.doesNotMatch(text, /notpresent/); + assert.doesNotMatch('abc', /123/); + assert.doesNotMatch('', /\d/); + assert.doesNotMatch('abc', /\d+/); }); - assert.strictEqual(obj.a, 2); - assert.doesNotThrow(() => { - return 42; + } + }); + + it(() => { + if (!nodeVersion || nodeVersion > 10) { + const asyncFunctionThatRejects = async () => + await Promise.reject(new Error('Async error')); + + const asyncFunctionThatResolves = () => + Promise.resolve('Resolved successfully'); + + const asyncFunctionThatFails = () => + new Promise((_, reject) => reject(new Error('Failed'))); + + const asyncFunctionThatCouldReject = () => + new Promise((resolve) => resolve(undefined)); + + it(() => { + assert.rejects( + async () => await asyncFunctionThatFails(), + new Error('Failed') + ); + assert.rejects(asyncFunctionThatRejects, new Error('Async error')); + assert.rejects( + () => Promise.reject('Simple rejection'), + (err) => err === 'Simple rejection' + ); + assert.rejects(asyncFunctionThatRejects, new Error('Async error')); }); - assert.doesNotThrow(() => - callbackFunction((err) => { - assert.ifError(err); - }) - ); - assert.doesNotThrow(() => 42); - assert.doesNotThrow(() => 'no error'); - }); - } -}); - -test(() => { - if (!nodeVersion || nodeVersion > 12) { - const text = 'sample text'; - - test(() => { - assert.match(text, /sample/); - }); - - test(() => { - assert.doesNotMatch(text, /notpresent/); - assert.doesNotMatch('abc', /123/); - assert.doesNotMatch('', /\d/); - assert.doesNotMatch('abc', /\d+/); - }); - } -}); -test(() => { - if (!nodeVersion || nodeVersion > 10) { - const asyncFunctionThatRejects = async () => - await Promise.reject(new Error('Async error')); - - const asyncFunctionThatResolves = () => - Promise.resolve('Resolved successfully'); - - const asyncFunctionThatFails = () => - new Promise((_, reject) => reject(new Error('Failed'))); - - const asyncFunctionThatCouldReject = () => - new Promise((resolve) => resolve(undefined)); - - test(() => { - assert.rejects( - async () => await asyncFunctionThatFails(), - new Error('Failed') - ); - assert.rejects(asyncFunctionThatRejects, new Error('Async error')); - assert.rejects( - () => Promise.reject('Simple rejection'), - (err) => err === 'Simple rejection' - ); - assert.rejects(asyncFunctionThatRejects, new Error('Async error')); - }); - - test(() => { - assert.doesNotReject(asyncFunctionThatResolves); - assert.doesNotReject(Promise.resolve('Immediate resolve')); - assert.doesNotReject(asyncFunctionThatCouldReject); - assert.doesNotReject(() => - Promise.resolve('Async function with no rejection') - ); - assert.doesNotReject(asyncFunctionThatResolves); - }); - } + it(() => { + assert.doesNotReject(asyncFunctionThatResolves); + assert.doesNotReject(Promise.resolve('Immediate resolve')); + assert.doesNotReject(asyncFunctionThatCouldReject); + assert.doesNotReject(() => + Promise.resolve('Async function with no rejection') + ); + assert.doesNotReject(asyncFunctionThatResolves); + }); + } + }); }); diff --git a/test/integration/assert/assert-promise.test.ts b/test/integration/assert/assert-promise.test.ts index eb7bcc59..79ac7479 100644 --- a/test/integration/assert/assert-promise.test.ts +++ b/test/integration/assert/assert-promise.test.ts @@ -1,308 +1,312 @@ +import { describe } from '../../../src/modules/describe.js'; +import { it } from '../../../src/modules/it.js'; +import { assertPromise as assert } from '../../../src/modules/assert-promise.js'; import { nodeVersion } from '../../../src/helpers/get-runtime.js'; -import { assertPromise as assert, describe, test } from '../../../src/index.js'; - -describe('Assert (Promise) Suite', { icon: '🔬' }); - -test(() => { - assert(true, 'ok (default) with true'); - assert(1, 'ok (default) with 1'); - assert('string', 'ok (default) with string'); - assert([], 'ok (default) with empty array'); - assert({}, 'ok (default) with empty object'); - assert(() => {}, 'ok (default) with empty function'); - assert(3 > 2, 'ok (default) 3 should be greater than 2'); -}); - -test(() => { - assert.ok(true, 'ok with true'); - assert.ok(1, 'ok with 1'); - assert.ok('string', 'ok with string'); - assert.ok([], 'ok with empty array'); - assert.ok({}, 'ok with empty object'); - assert.ok(() => {}, 'ok with empty function'); - assert.ok(3 > 2, '3 should be greater than 2'); -}); - -test(() => { - assert.equal(1, 1, 'equal with same numbers'); - assert.equal('text', 'text', 'equal with same strings'); - assert.equal(2 + 2, 4, '2 + 2 should equal 4'); - assert.equal( - 'Hello'.toUpperCase(), - 'HELLO', - 'toUpperCase should convert to all upper case' - ); -}); - -test(() => { - assert.deepEqual({ a: 1 }, { a: 1 }, 'deepEqual with same objects'); - assert.deepEqual([1, 2], [1, 2], 'deepEqual with same arrays'); - assert.deepEqual( - [2, 3, 4], - [2, 3, 4], - 'Arrays [2, 3, 4] should be deeply equal' - ); - assert.deepEqual( - [1, 2, 3].reverse(), - [3, 2, 1], - 'Reversing [1, 2, 3] should give [3, 2, 1]' - ); -}); - -test(() => { - assert.strictEqual(1, 1, 'strictEqual with same numbers'); - assert.strictEqual('text', 'text', 'strictEqual with same strings'); - assert.strictEqual(2 * 2, 4, '2 * 2 should be strictly equal to 4'); -}); - -test(() => { - assert.deepStrictEqual( - { a: 1 }, - { a: 1 }, - 'deepStrictEqual with same objects' - ); - assert.deepStrictEqual([1, 2], [1, 2], 'deepStrictEqual with same arrays'); - assert.deepStrictEqual( - { a: 1, b: 2 }, - { a: 1, b: 2 }, - 'Objects { a: 1, b: 2 } should be deeply and strictly equal' - ); -}); - -test(() => { - assert.doesNotThrow(() => 1 + 1, 'doesNotThrow with non-throwing function'); -}); - -test(() => { - assert.notEqual(1, 2, 'notEqual with different numbers'); - assert.notEqual(2 + 2, 5, '2 + 2 should not equal 5'); - assert.notEqual( - 'Hello'.toLowerCase(), - 'HELLO', - 'toLowerCase should not match the upper case version' - ); -}); - -test(() => { - assert.notStrictEqual(1, true, 'notStrictEqual with different types'); - assert.notStrictEqual( - 1, - '1', - 'notStrictEqual with loosely equal but not strictly equal values' - ); - assert.notStrictEqual( - 2 * 2, - '4', - '2 * 2 should not be strictly equal to "4"' - ); - assert.notStrictEqual(2, '2', '2 should not be strictly equal to "2"'); -}); - -test(() => { - assert.notDeepEqual( - { a: 1 }, - { a: 2 }, - 'notDeepEqual with different objects' - ); - assert.notDeepEqual( - [2, 3, 4], - [4, 5, 6], - 'Arrays [2, 3, 4] and [4, 5, 6] should not be deeply equal' - ); -}); - -test(() => { - assert.notDeepStrictEqual( - { a: 1 }, - { a: '1' }, - 'notDeepStrictEqual with loosely equal but not strictly deep equal objects' - ); - assert.notDeepStrictEqual( - [1, 2, 3], - [1, 2, '3'], - '[1, 2, 3] should not be deeply strictly equal to [1, 2, "3"]' - ); - assert.notDeepStrictEqual( - { a: 1 }, - { a: '1' }, - 'Objects { a: 1 } and { a: "1" } should not be deeply and strictly equal' - ); -}); - -const callbackFunction = (cb: (err: Error | null, result?: string) => void) => { - cb(null, 'no error'); -}; - -test(() => { - assert.ifError(null, 'ifError did not throw an error for null'); - assert.ifError(undefined, 'ifError did not throw an error for undefined'); -}); - -test(() => { - if (!nodeVersion || nodeVersion > 8) { - const obj = { a: 1 }; - - const functionThatThrows = () => { - throw new Error('Specific error'); - }; - - test(() => { - assert.throws(() => { - throw new Error('error'); - }, 'throws with throwing function'); - assert.throws(() => { - throw new Error('Test error'); - }, 'Should throw an exception for a function that generates an error'); - assert.throws(() => { - throw new Error('Test error'); - }, 'Should throw an error for a function that actually throws'); - assert.throws( - functionThatThrows, - new Error('Specific error'), - 'Should throw the specific error' - ); - - assert.throws( - functionThatThrows, - /Specific error/, - 'Should throw an error matching the regex' - ); - - assert.throws( - functionThatThrows, - (err) => err instanceof Error && err.message === 'Specific error', - 'Should throw an error where the message equals the specific string' - ); - }); - - test(() => { - assert.doesNotThrow(() => { - obj.a = 2; - }, 'Changing property should not throw'); - assert.strictEqual(obj.a, 2, 'Property a should be 2 after mutation'); - - // Test to check functions that do or do not throw errors - assert.doesNotThrow(() => { - return 42; - }, 'Should not throw an exception for a function returning 42'); - - assert.doesNotThrow( - () => - callbackFunction((err) => { - assert.ifError(err); - }), - 'Should not throw an error for a callback function that does not error' - ); - - assert.doesNotThrow( - () => 42, - 'Should not throw an error for a function returning a number' - ); - - assert.doesNotThrow( - () => 'no error', - 'Should not throw an error for a function returning a string' - ); - - assert.doesNotThrow( - () => 'no error', - 'Should not throw an error for an async function that resolves' - ); - }); - } -}); - -test(() => { - if (!nodeVersion || nodeVersion > 12) { - const text = 'sample text'; - - test(() => { - assert.match(text, /sample/, 'Text should match the regex'); - }); - - test(() => { - assert.doesNotMatch( - text, - /notpresent/, - 'Text should not match the regex' - ); - assert.doesNotMatch( - 'abc', - /123/, - 'String "abc" should not match the pattern /123/' - ); - assert.doesNotMatch( - '', - /\d/, - 'Empty string should not match the pattern /d/' - ); - assert.doesNotMatch( - 'abc', - /\d+/, - 'String "abc" should not match the pattern /d+/' - ); - }); - } -}); -test(() => { - if (!nodeVersion || nodeVersion > 10) { - const asyncFunctionThatRejects = async () => - await Promise.reject(new Error('Async error')); - - const asyncFunctionThatResolves = () => - Promise.resolve('Resolved successfully'); - - const asyncFunctionThatFails = () => - new Promise((_, reject) => reject(new Error('Failed'))); - - const asyncFunctionThatCouldReject = () => - new Promise((resolve) => resolve(undefined)); - - test(() => { - assert.rejects( - async () => await asyncFunctionThatFails(), - new Error('Failed'), - 'Async function should reject with an error' - ); - assert.rejects( - asyncFunctionThatRejects, - new Error('Async error'), - 'Should reject with an Error object with "Async error" message' - ); - assert.rejects( - () => Promise.reject('Simple rejection'), - (err) => err === 'Simple rejection', - 'Should handle rejection with a simple string message' - ); - assert.rejects( - asyncFunctionThatRejects, - new Error('Async error'), - 'Should reject with the specified error message' - ); - }); - - test(() => { - assert.doesNotReject( - asyncFunctionThatResolves, - 'Should not reject for a function that resolves' - ); - assert.doesNotReject( - Promise.resolve('Immediate resolve'), - 'Should not reject for an immediately resolving promise' - ); - assert.doesNotReject( - asyncFunctionThatCouldReject, - 'Should not reject for a function that could reject but resolves instead' - ); - assert.doesNotReject( - () => Promise.resolve('Async function with no rejection'), - 'Should handle async functions that do not reject' - ); - assert.doesNotReject( - asyncFunctionThatResolves, - 'Should handle cases with no specific error argument in doesNotReject' - ); - }); - } +describe('Assert (Promise) Suite', async () => { + it(() => { + assert(true, 'ok (default) with true'); + assert(1, 'ok (default) with 1'); + assert('string', 'ok (default) with string'); + assert([], 'ok (default) with empty array'); + assert({}, 'ok (default) with empty object'); + assert(() => {}, 'ok (default) with empty function'); + assert(3 > 2, 'ok (default) 3 should be greater than 2'); + }); + + it(() => { + assert.ok(true, 'ok with true'); + assert.ok(1, 'ok with 1'); + assert.ok('string', 'ok with string'); + assert.ok([], 'ok with empty array'); + assert.ok({}, 'ok with empty object'); + assert.ok(() => {}, 'ok with empty function'); + assert.ok(3 > 2, '3 should be greater than 2'); + }); + + it(() => { + assert.equal(1, 1, 'equal with same numbers'); + assert.equal('text', 'text', 'equal with same strings'); + assert.equal(2 + 2, 4, '2 + 2 should equal 4'); + assert.equal( + 'Hello'.toUpperCase(), + 'HELLO', + 'toUpperCase should convert to all upper case' + ); + }); + + it(() => { + assert.deepEqual({ a: 1 }, { a: 1 }, 'deepEqual with same objects'); + assert.deepEqual([1, 2], [1, 2], 'deepEqual with same arrays'); + assert.deepEqual( + [2, 3, 4], + [2, 3, 4], + 'Arrays [2, 3, 4] should be deeply equal' + ); + assert.deepEqual( + [1, 2, 3].reverse(), + [3, 2, 1], + 'Reversing [1, 2, 3] should give [3, 2, 1]' + ); + }); + + it(() => { + assert.strictEqual(1, 1, 'strictEqual with same numbers'); + assert.strictEqual('text', 'text', 'strictEqual with same strings'); + assert.strictEqual(2 * 2, 4, '2 * 2 should be strictly equal to 4'); + }); + + it(() => { + assert.deepStrictEqual( + { a: 1 }, + { a: 1 }, + 'deepStrictEqual with same objects' + ); + assert.deepStrictEqual([1, 2], [1, 2], 'deepStrictEqual with same arrays'); + assert.deepStrictEqual( + { a: 1, b: 2 }, + { a: 1, b: 2 }, + 'Objects { a: 1, b: 2 } should be deeply and strictly equal' + ); + }); + + it(() => { + assert.doesNotThrow(() => 1 + 1, 'doesNotThrow with non-throwing function'); + }); + + it(() => { + assert.notEqual(1, 2, 'notEqual with different numbers'); + assert.notEqual(2 + 2, 5, '2 + 2 should not equal 5'); + assert.notEqual( + 'Hello'.toLowerCase(), + 'HELLO', + 'toLowerCase should not match the upper case version' + ); + }); + + it(() => { + assert.notStrictEqual(1, true, 'notStrictEqual with different types'); + assert.notStrictEqual( + 1, + '1', + 'notStrictEqual with loosely equal but not strictly equal values' + ); + assert.notStrictEqual( + 2 * 2, + '4', + '2 * 2 should not be strictly equal to "4"' + ); + assert.notStrictEqual(2, '2', '2 should not be strictly equal to "2"'); + }); + + it(() => { + assert.notDeepEqual( + { a: 1 }, + { a: 2 }, + 'notDeepEqual with different objects' + ); + assert.notDeepEqual( + [2, 3, 4], + [4, 5, 6], + 'Arrays [2, 3, 4] and [4, 5, 6] should not be deeply equal' + ); + }); + + it(() => { + assert.notDeepStrictEqual( + { a: 1 }, + { a: '1' }, + 'notDeepStrictEqual with loosely equal but not strictly deep equal objects' + ); + assert.notDeepStrictEqual( + [1, 2, 3], + [1, 2, '3'], + '[1, 2, 3] should not be deeply strictly equal to [1, 2, "3"]' + ); + assert.notDeepStrictEqual( + { a: 1 }, + { a: '1' }, + 'Objects { a: 1 } and { a: "1" } should not be deeply and strictly equal' + ); + }); + + const callbackFunction = ( + cb: (err: Error | null, result?: string) => void + ) => { + cb(null, 'no error'); + }; + + it(() => { + assert.ifError(null, 'ifError did not throw an error for null'); + assert.ifError(undefined, 'ifError did not throw an error for undefined'); + }); + + it(() => { + if (!nodeVersion || nodeVersion > 8) { + const obj = { a: 1 }; + + const functionThatThrows = () => { + throw new Error('Specific error'); + }; + + it(() => { + assert.throws(() => { + throw new Error('error'); + }, 'throws with throwing function'); + assert.throws(() => { + throw new Error('Test error'); + }, 'Should throw an exception for a function that generates an error'); + assert.throws(() => { + throw new Error('Test error'); + }, 'Should throw an error for a function that actually throws'); + assert.throws( + functionThatThrows, + new Error('Specific error'), + 'Should throw the specific error' + ); + + assert.throws( + functionThatThrows, + /Specific error/, + 'Should throw an error matching the regex' + ); + + assert.throws( + functionThatThrows, + (err) => err instanceof Error && err.message === 'Specific error', + 'Should throw an error where the message equals the specific string' + ); + }); + + it(() => { + assert.doesNotThrow(() => { + obj.a = 2; + }, 'Changing property should not throw'); + assert.strictEqual(obj.a, 2, 'Property a should be 2 after mutation'); + + // Test to check functions that do or do not throw errors + assert.doesNotThrow(() => { + return 42; + }, 'Should not throw an exception for a function returning 42'); + + assert.doesNotThrow( + () => + callbackFunction((err) => { + assert.ifError(err); + }), + 'Should not throw an error for a callback function that does not error' + ); + + assert.doesNotThrow( + () => 42, + 'Should not throw an error for a function returning a number' + ); + + assert.doesNotThrow( + () => 'no error', + 'Should not throw an error for a function returning a string' + ); + + assert.doesNotThrow( + () => 'no error', + 'Should not throw an error for an async function that resolves' + ); + }); + } + }); + + it(() => { + if (!nodeVersion || nodeVersion > 12) { + const text = 'sample text'; + + it(() => { + assert.match(text, /sample/, 'Text should match the regex'); + }); + + it(() => { + assert.doesNotMatch( + text, + /notpresent/, + 'Text should not match the regex' + ); + assert.doesNotMatch( + 'abc', + /123/, + 'String "abc" should not match the pattern /123/' + ); + assert.doesNotMatch( + '', + /\d/, + 'Empty string should not match the pattern /d/' + ); + assert.doesNotMatch( + 'abc', + /\d+/, + 'String "abc" should not match the pattern /d+/' + ); + }); + } + }); + + await it(async () => { + if (!nodeVersion || nodeVersion > 10) { + const asyncFunctionThatRejects = async () => + await Promise.reject(new Error('Async error')); + + const asyncFunctionThatResolves = () => + Promise.resolve('Resolved successfully'); + + const asyncFunctionThatFails = () => + new Promise((_, reject) => reject(new Error('Failed'))); + + const asyncFunctionThatCouldReject = () => + new Promise((resolve) => resolve(undefined)); + + await it(async () => { + await assert.rejects( + async () => await asyncFunctionThatFails(), + new Error('Failed'), + 'Async function should reject with an error' + ); + await assert.rejects( + asyncFunctionThatRejects, + new Error('Async error'), + 'Should reject with an Error object with "Async error" message' + ); + await assert.rejects( + () => Promise.reject('Simple rejection'), + (err) => err === 'Simple rejection', + 'Should handle rejection with a simple string message' + ); + await assert.rejects( + asyncFunctionThatRejects, + new Error('Async error'), + 'Should reject with the specified error message' + ); + }); + + await it(async () => { + await assert.doesNotReject( + asyncFunctionThatResolves, + 'Should not reject for a function that resolves' + ); + await assert.doesNotReject( + Promise.resolve('Immediate resolve'), + 'Should not reject for an immediately resolving promise' + ); + await assert.doesNotReject( + asyncFunctionThatCouldReject, + 'Should not reject for a function that could reject but resolves instead' + ); + await assert.doesNotReject( + () => Promise.resolve('Async function with no rejection'), + 'Should handle async functions that do not reject' + ); + await assert.doesNotReject( + asyncFunctionThatResolves, + 'Should handle cases with no specific error argument in doesNotReject' + ); + }); + } + }); }); diff --git a/test/integration/assert/assert.test.ts b/test/integration/assert/assert.test.ts index 5d2c2e68..066266d8 100644 --- a/test/integration/assert/assert.test.ts +++ b/test/integration/assert/assert.test.ts @@ -1,308 +1,312 @@ +import { describe } from '../../../src/modules/describe.js'; +import { it } from '../../../src/modules/it.js'; +import { assert } from '../../../src/modules/assert.js'; import { nodeVersion } from '../../../src/helpers/get-runtime.js'; -import { assert, describe, test } from '../../../src/index.js'; - -describe('Assert Suite', { icon: '🔬' }); - -test(() => { - assert(true, 'ok (default) with true'); - assert(1, 'ok (default) with 1'); - assert('string', 'ok (default) with string'); - assert([], 'ok (default) with empty array'); - assert({}, 'ok (default) with empty object'); - assert(() => {}, 'ok (default) with empty function'); - assert(3 > 2, 'ok (default) 3 should be greater than 2'); -}); - -test(() => { - assert.ok(true, 'ok with true'); - assert.ok(1, 'ok with 1'); - assert.ok('string', 'ok with string'); - assert.ok([], 'ok with empty array'); - assert.ok({}, 'ok with empty object'); - assert.ok(() => {}, 'ok with empty function'); - assert.ok(3 > 2, '3 should be greater than 2'); -}); - -test(() => { - assert.equal(1, 1, 'equal with same numbers'); - assert.equal('text', 'text', 'equal with same strings'); - assert.equal(2 + 2, 4, '2 + 2 should equal 4'); - assert.equal( - 'Hello'.toUpperCase(), - 'HELLO', - 'toUpperCase should convert to all upper case' - ); -}); - -test(() => { - assert.deepEqual({ a: 1 }, { a: 1 }, 'deepEqual with same objects'); - assert.deepEqual([1, 2], [1, 2], 'deepEqual with same arrays'); - assert.deepEqual( - [2, 3, 4], - [2, 3, 4], - 'Arrays [2, 3, 4] should be deeply equal' - ); - assert.deepEqual( - [1, 2, 3].reverse(), - [3, 2, 1], - 'Reversing [1, 2, 3] should give [3, 2, 1]' - ); -}); - -test(() => { - assert.strictEqual(1, 1, 'strictEqual with same numbers'); - assert.strictEqual('text', 'text', 'strictEqual with same strings'); - assert.strictEqual(2 * 2, 4, '2 * 2 should be strictly equal to 4'); -}); - -test(() => { - assert.deepStrictEqual( - { a: 1 }, - { a: 1 }, - 'deepStrictEqual with same objects' - ); - assert.deepStrictEqual([1, 2], [1, 2], 'deepStrictEqual with same arrays'); - assert.deepStrictEqual( - { a: 1, b: 2 }, - { a: 1, b: 2 }, - 'Objects { a: 1, b: 2 } should be deeply and strictly equal' - ); -}); - -test(() => { - assert.doesNotThrow(() => 1 + 1, 'doesNotThrow with non-throwing function'); -}); - -test(() => { - assert.notEqual(1, 2, 'notEqual with different numbers'); - assert.notEqual(2 + 2, 5, '2 + 2 should not equal 5'); - assert.notEqual( - 'Hello'.toLowerCase(), - 'HELLO', - 'toLowerCase should not match the upper case version' - ); -}); - -test(() => { - assert.notStrictEqual(1, true, 'notStrictEqual with different types'); - assert.notStrictEqual( - 1, - '1', - 'notStrictEqual with loosely equal but not strictly equal values' - ); - assert.notStrictEqual( - 2 * 2, - '4', - '2 * 2 should not be strictly equal to "4"' - ); - assert.notStrictEqual(2, '2', '2 should not be strictly equal to "2"'); -}); - -test(() => { - assert.notDeepEqual( - { a: 1 }, - { a: 2 }, - 'notDeepEqual with different objects' - ); - assert.notDeepEqual( - [2, 3, 4], - [4, 5, 6], - 'Arrays [2, 3, 4] and [4, 5, 6] should not be deeply equal' - ); -}); - -test(() => { - assert.notDeepStrictEqual( - { a: 1 }, - { a: '1' }, - 'notDeepStrictEqual with loosely equal but not strictly deep equal objects' - ); - assert.notDeepStrictEqual( - [1, 2, 3], - [1, 2, '3'], - '[1, 2, 3] should not be deeply strictly equal to [1, 2, "3"]' - ); - assert.notDeepStrictEqual( - { a: 1 }, - { a: '1' }, - 'Objects { a: 1 } and { a: "1" } should not be deeply and strictly equal' - ); -}); - -const callbackFunction = (cb: (err: Error | null, result?: string) => void) => { - cb(null, 'no error'); -}; - -test(() => { - assert.ifError(null, 'ifError did not throw an error for null'); - assert.ifError(undefined, 'ifError did not throw an error for undefined'); -}); - -test(() => { - if (!nodeVersion || nodeVersion > 8) { - const obj = { a: 1 }; - - const functionThatThrows = () => { - throw new Error('Specific error'); - }; - - test(() => { - assert.throws(() => { - throw new Error('error'); - }, 'throws with throwing function'); - assert.throws(() => { - throw new Error('Test error'); - }, 'Should throw an exception for a function that generates an error'); - assert.throws(() => { - throw new Error('Test error'); - }, 'Should throw an error for a function that actually throws'); - assert.throws( - functionThatThrows, - new Error('Specific error'), - 'Should throw the specific error' - ); - - assert.throws( - functionThatThrows, - /Specific error/, - 'Should throw an error matching the regex' - ); - - assert.throws( - functionThatThrows, - (err) => err instanceof Error && err.message === 'Specific error', - 'Should throw an error where the message equals the specific string' - ); - }); - - test(() => { - assert.doesNotThrow(() => { - obj.a = 2; - }, 'Changing property should not throw'); - assert.strictEqual(obj.a, 2, 'Property a should be 2 after mutation'); - - // Test to check functions that do or do not throw errors - assert.doesNotThrow(() => { - return 42; - }, 'Should not throw an exception for a function returning 42'); - - assert.doesNotThrow( - () => - callbackFunction((err) => { - assert.ifError(err); - }), - 'Should not throw an error for a callback function that does not error' - ); - - assert.doesNotThrow( - () => 42, - 'Should not throw an error for a function returning a number' - ); - - assert.doesNotThrow( - () => 'no error', - 'Should not throw an error for a function returning a string' - ); - - assert.doesNotThrow( - () => 'no error', - 'Should not throw an error for an async function that resolves' - ); - }); - } -}); - -test(() => { - if (!nodeVersion || nodeVersion > 12) { - const text = 'sample text'; - - test(() => { - assert.match(text, /sample/, 'Text should match the regex'); - }); - - test(() => { - assert.doesNotMatch( - text, - /notpresent/, - 'Text should not match the regex' - ); - assert.doesNotMatch( - 'abc', - /123/, - 'String "abc" should not match the pattern /123/' - ); - assert.doesNotMatch( - '', - /\d/, - 'Empty string should not match the pattern /d/' - ); - assert.doesNotMatch( - 'abc', - /\d+/, - 'String "abc" should not match the pattern /d+/' - ); - }); - } -}); -test(() => { - if (!nodeVersion || nodeVersion > 10) { - const asyncFunctionThatRejects = async () => - await Promise.reject(new Error('Async error')); - - const asyncFunctionThatResolves = () => - Promise.resolve('Resolved successfully'); - - const asyncFunctionThatFails = () => - new Promise((_, reject) => reject(new Error('Failed'))); - - const asyncFunctionThatCouldReject = () => - new Promise((resolve) => resolve(undefined)); - - test(() => { - assert.rejects( - async () => await asyncFunctionThatFails(), - new Error('Failed'), - 'Async function should reject with an error' - ); - assert.rejects( - asyncFunctionThatRejects, - new Error('Async error'), - 'Should reject with an Error object with "Async error" message' - ); - assert.rejects( - () => Promise.reject('Simple rejection'), - (err) => err === 'Simple rejection', - 'Should handle rejection with a simple string message' - ); - assert.rejects( - asyncFunctionThatRejects, - new Error('Async error'), - 'Should reject with the specified error message' - ); - }); - - test(() => { - assert.doesNotReject( - asyncFunctionThatResolves, - 'Should not reject for a function that resolves' - ); - assert.doesNotReject( - Promise.resolve('Immediate resolve'), - 'Should not reject for an immediately resolving promise' - ); - assert.doesNotReject( - asyncFunctionThatCouldReject, - 'Should not reject for a function that could reject but resolves instead' - ); - assert.doesNotReject( - () => Promise.resolve('Async function with no rejection'), - 'Should handle async functions that do not reject' - ); - assert.doesNotReject( - asyncFunctionThatResolves, - 'Should handle cases with no specific error argument in doesNotReject' - ); - }); - } +describe('Assert Suite', async () => { + it(() => { + assert(true, 'ok (default) with true'); + assert(1, 'ok (default) with 1'); + assert('string', 'ok (default) with string'); + assert([], 'ok (default) with empty array'); + assert({}, 'ok (default) with empty object'); + assert(() => {}, 'ok (default) with empty function'); + assert(3 > 2, 'ok (default) 3 should be greater than 2'); + }); + + it(() => { + assert.ok(true, 'ok with true'); + assert.ok(1, 'ok with 1'); + assert.ok('string', 'ok with string'); + assert.ok([], 'ok with empty array'); + assert.ok({}, 'ok with empty object'); + assert.ok(() => {}, 'ok with empty function'); + assert.ok(3 > 2, '3 should be greater than 2'); + }); + + it(() => { + assert.equal(1, 1, 'equal with same numbers'); + assert.equal('text', 'text', 'equal with same strings'); + assert.equal(2 + 2, 4, '2 + 2 should equal 4'); + assert.equal( + 'Hello'.toUpperCase(), + 'HELLO', + 'toUpperCase should convert to all upper case' + ); + }); + + it(() => { + assert.deepEqual({ a: 1 }, { a: 1 }, 'deepEqual with same objects'); + assert.deepEqual([1, 2], [1, 2], 'deepEqual with same arrays'); + assert.deepEqual( + [2, 3, 4], + [2, 3, 4], + 'Arrays [2, 3, 4] should be deeply equal' + ); + assert.deepEqual( + [1, 2, 3].reverse(), + [3, 2, 1], + 'Reversing [1, 2, 3] should give [3, 2, 1]' + ); + }); + + it(() => { + assert.strictEqual(1, 1, 'strictEqual with same numbers'); + assert.strictEqual('text', 'text', 'strictEqual with same strings'); + assert.strictEqual(2 * 2, 4, '2 * 2 should be strictly equal to 4'); + }); + + it(() => { + assert.deepStrictEqual( + { a: 1 }, + { a: 1 }, + 'deepStrictEqual with same objects' + ); + assert.deepStrictEqual([1, 2], [1, 2], 'deepStrictEqual with same arrays'); + assert.deepStrictEqual( + { a: 1, b: 2 }, + { a: 1, b: 2 }, + 'Objects { a: 1, b: 2 } should be deeply and strictly equal' + ); + }); + + it(() => { + assert.doesNotThrow(() => 1 + 1, 'doesNotThrow with non-throwing function'); + }); + + it(() => { + assert.notEqual(1, 2, 'notEqual with different numbers'); + assert.notEqual(2 + 2, 5, '2 + 2 should not equal 5'); + assert.notEqual( + 'Hello'.toLowerCase(), + 'HELLO', + 'toLowerCase should not match the upper case version' + ); + }); + + it(() => { + assert.notStrictEqual(1, true, 'notStrictEqual with different types'); + assert.notStrictEqual( + 1, + '1', + 'notStrictEqual with loosely equal but not strictly equal values' + ); + assert.notStrictEqual( + 2 * 2, + '4', + '2 * 2 should not be strictly equal to "4"' + ); + assert.notStrictEqual(2, '2', '2 should not be strictly equal to "2"'); + }); + + it(() => { + assert.notDeepEqual( + { a: 1 }, + { a: 2 }, + 'notDeepEqual with different objects' + ); + assert.notDeepEqual( + [2, 3, 4], + [4, 5, 6], + 'Arrays [2, 3, 4] and [4, 5, 6] should not be deeply equal' + ); + }); + + it(() => { + assert.notDeepStrictEqual( + { a: 1 }, + { a: '1' }, + 'notDeepStrictEqual with loosely equal but not strictly deep equal objects' + ); + assert.notDeepStrictEqual( + [1, 2, 3], + [1, 2, '3'], + '[1, 2, 3] should not be deeply strictly equal to [1, 2, "3"]' + ); + assert.notDeepStrictEqual( + { a: 1 }, + { a: '1' }, + 'Objects { a: 1 } and { a: "1" } should not be deeply and strictly equal' + ); + }); + + const callbackFunction = ( + cb: (err: Error | null, result?: string) => void + ) => { + cb(null, 'no error'); + }; + + it(() => { + assert.ifError(null, 'ifError did not throw an error for null'); + assert.ifError(undefined, 'ifError did not throw an error for undefined'); + }); + + it(() => { + if (!nodeVersion || nodeVersion > 8) { + const obj = { a: 1 }; + + const functionThatThrows = () => { + throw new Error('Specific error'); + }; + + it(() => { + assert.throws(() => { + throw new Error('error'); + }, 'throws with throwing function'); + assert.throws(() => { + throw new Error('Test error'); + }, 'Should throw an exception for a function that generates an error'); + assert.throws(() => { + throw new Error('Test error'); + }, 'Should throw an error for a function that actually throws'); + assert.throws( + functionThatThrows, + new Error('Specific error'), + 'Should throw the specific error' + ); + + assert.throws( + functionThatThrows, + /Specific error/, + 'Should throw an error matching the regex' + ); + + assert.throws( + functionThatThrows, + (err) => err instanceof Error && err.message === 'Specific error', + 'Should throw an error where the message equals the specific string' + ); + }); + + it(() => { + assert.doesNotThrow(() => { + obj.a = 2; + }, 'Changing property should not throw'); + assert.strictEqual(obj.a, 2, 'Property a should be 2 after mutation'); + + // Test to check functions that do or do not throw errors + assert.doesNotThrow(() => { + return 42; + }, 'Should not throw an exception for a function returning 42'); + + assert.doesNotThrow( + () => + callbackFunction((err) => { + assert.ifError(err); + }), + 'Should not throw an error for a callback function that does not error' + ); + + assert.doesNotThrow( + () => 42, + 'Should not throw an error for a function returning a number' + ); + + assert.doesNotThrow( + () => 'no error', + 'Should not throw an error for a function returning a string' + ); + + assert.doesNotThrow( + () => 'no error', + 'Should not throw an error for an async function that resolves' + ); + }); + } + }); + + it(() => { + if (!nodeVersion || nodeVersion > 12) { + const text = 'sample text'; + + it(() => { + assert.match(text, /sample/, 'Text should match the regex'); + }); + + it(() => { + assert.doesNotMatch( + text, + /notpresent/, + 'Text should not match the regex' + ); + assert.doesNotMatch( + 'abc', + /123/, + 'String "abc" should not match the pattern /123/' + ); + assert.doesNotMatch( + '', + /\d/, + 'Empty string should not match the pattern /d/' + ); + assert.doesNotMatch( + 'abc', + /\d+/, + 'String "abc" should not match the pattern /d+/' + ); + }); + } + }); + + await it(async () => { + if (!nodeVersion || nodeVersion > 10) { + const asyncFunctionThatRejects = async () => + await Promise.reject(new Error('Async error')); + + const asyncFunctionThatResolves = () => + Promise.resolve('Resolved successfully'); + + const asyncFunctionThatFails = () => + new Promise((_, reject) => reject(new Error('Failed'))); + + const asyncFunctionThatCouldReject = () => + new Promise((resolve) => resolve(undefined)); + + await it(async () => { + await assert.rejects( + async () => await asyncFunctionThatFails(), + new Error('Failed'), + 'Async function should reject with an error' + ); + await assert.rejects( + asyncFunctionThatRejects, + new Error('Async error'), + 'Should reject with an Error object with "Async error" message' + ); + await assert.rejects( + () => Promise.reject('Simple rejection'), + (err) => err === 'Simple rejection', + 'Should handle rejection with a simple string message' + ); + await assert.rejects( + asyncFunctionThatRejects, + new Error('Async error'), + 'Should reject with the specified error message' + ); + }); + + await it(async () => { + await assert.doesNotReject( + asyncFunctionThatResolves, + 'Should not reject for a function that resolves' + ); + await assert.doesNotReject( + Promise.resolve('Immediate resolve'), + 'Should not reject for an immediately resolving promise' + ); + await assert.doesNotReject( + asyncFunctionThatCouldReject, + 'Should not reject for a function that could reject but resolves instead' + ); + await assert.doesNotReject( + () => Promise.resolve('Async function with no rejection'), + 'Should handle async functions that do not reject' + ); + await assert.doesNotReject( + asyncFunctionThatResolves, + 'Should handle cases with no specific error argument in doesNotReject' + ); + }); + } + }); }); diff --git a/test/integration/import.test.ts b/test/integration/import.test.ts index 38f252cd..32e3da6d 100644 --- a/test/integration/import.test.ts +++ b/test/integration/import.test.ts @@ -1,19 +1,19 @@ import * as index from '../../src/index.js'; -index.describe('Import Suite', { icon: '🔬' }); +index.test('Import Suite', () => { + index.assert.ok(index.poku, 'Importing poku method'); + index.assert.ok(index.assert, 'Importing assert method'); + index.assert.ok(index.assertPromise, 'Importing assertPromise method'); + index.assert.ok(index.startService, 'Importing startService method'); + index.assert.ok(index.startScript, 'Importing startScript method'); -index.assert.ok(index.poku, 'Importing poku method'); -index.assert.ok(index.assert, 'Importing assert method'); -index.assert.ok(index.assertPromise, 'Importing assertPromise method'); -index.assert.ok(index.startService, 'Importing startService method'); -index.assert.ok(index.startScript, 'Importing startScript method'); - -index.assert.ok(index.getPIDs, 'Importing getPIDs helper'); -index.assert.ok(index.kill, 'Importing kill helper'); -index.assert.ok(index.describe, 'Importing describe helper'); -index.assert.ok(index.beforeEach, 'Importing beforeEach helper'); -index.assert.ok(index.afterEach, 'Importing afterEach helper'); -index.assert.ok(index.log, 'Importing log helper'); -index.assert.ok(index.test, 'Importing test helper'); -index.assert.ok(index.exit, 'Importing exit helper'); -index.assert.ok(index.listFiles, 'Importing listFiles helper'); + index.assert.ok(index.getPIDs, 'Importing getPIDs helper'); + index.assert.ok(index.kill, 'Importing kill helper'); + index.assert.ok(index.describe, 'Importing describe helper'); + index.assert.ok(index.beforeEach, 'Importing beforeEach helper'); + index.assert.ok(index.afterEach, 'Importing afterEach helper'); + index.assert.ok(index.log, 'Importing log helper'); + index.assert.ok(index.test, 'Importing test helper'); + index.assert.ok(index.exit, 'Importing exit helper'); + index.assert.ok(index.listFiles, 'Importing listFiles helper'); +}); diff --git a/test/integration/it/each/each-promise.test.ts b/test/integration/it/each/each-promise.test.ts index 20acf569..fa517353 100644 --- a/test/integration/it/each/each-promise.test.ts +++ b/test/integration/it/each/each-promise.test.ts @@ -13,68 +13,27 @@ const beforeEachHelper = beforeEach(asyncPreIncrement, { immediate: true }); const afterEachHelper = afterEach(asyncPreIncrement, {}); describe('Asynchronous Before and After Each Suite (it)', () => { - it(() => { - assert.equal( - counter, - 2, - 'value incremented by 1 from beforeEach with immediate option should be 2' - ); - }); - - it(() => { - assert.equal( - counter, - 4, - 'value incremented by 1 from beforeEach with immediate option and by 1 from previous test with afterEach should be 4' - ); - }); + it(() => assert.equal(counter, 2, 'Counter should be 2 after beforeEach')); + it(() => + assert.equal(counter, 4, 'Counter should be 4 after before/afterEach') + ); afterEachHelper.pause(); beforeEachHelper.pause(); - it(() => { - assert.equal( - counter, - 5, - 'value should still 5 by pausing both beforeEach and afterEach, considering the abscence of beforeEach effect' - ); - }); - - it(() => { - assert.equal( - counter, - 5, - 'value should still 5 by pausing both beforeEach and afterEach, considering the the abscence of beforeEach and afterEach (from previous test) effects' - ); - }); + it(() => assert.equal(counter, 5, 'Counter should remain 5 after pausing')); + it(() => assert.equal(counter, 5, 'Counter should still be 5')); afterEachHelper.continue(); beforeEachHelper.continue(); - it(() => { - assert.equal( - counter, - 6, - 'value incremented by 1 from unpausing beforeEach should be 6' - ); - }); - - it(() => { - assert.equal( - counter, - 8, - 'value incremented by 1 from beforeEach and 1 by previous test with afterEach should be 8' - ); - }); + it(() => assert.equal(counter, 6, 'Counter should be 6 after resuming')); + it(() => + assert.equal(counter, 8, 'Counter should be 8 after before/afterEach') + ); beforeEachHelper.reset(); afterEachHelper.reset(); - it(() => { - assert.equal( - counter, - 9, - 'value should still 9 by reseting both beforeEach and afterEach' - ); - }); + it(() => assert.equal(counter, 9, 'Counter should be 9 after reset')); }); diff --git a/test/integration/it/each/each.test.ts b/test/integration/it/each/each.test.ts index 2e529c57..0a36ca11 100644 --- a/test/integration/it/each/each.test.ts +++ b/test/integration/it/each/each.test.ts @@ -17,68 +17,40 @@ const afterEachHelper = afterEach(() => { }, {}); describe('Before and After Each Suite (it)', () => { - it(() => { - assert.equal( - counter, - 2, - 'value incremented by 1 from beforeEach with immediate option should be 2' - ); - }); + it(() => + assert.equal(counter, 2, 'Counter should be 2 after immediate beforeEach') + ); - it(() => { - assert.equal( - counter, - 4, - 'value incremented by 1 from beforeEach with immediate option and by 1 from previous test with afterEach should be 4' - ); - }); + it(() => + assert.equal(counter, 4, 'Counter should be 4 after before/afterEach') + ); afterEachHelper.pause(); beforeEachHelper.pause(); - it(() => { - assert.equal( - counter, - 5, - 'value should still 5 by pausing both beforeEach and afterEach, considering the abscence of beforeEach effect' - ); - }); + it(() => + assert.equal(counter, 5, 'Counter should remain 5 after pausing hooks') + ); - it(() => { - assert.equal( - counter, - 5, - 'value should still 5 by pausing both beforeEach and afterEach, considering the the abscence of beforeEach and afterEach (from previous test) effects' - ); - }); + it(() => + assert.equal(counter, 5, 'Counter should still be 5 with hooks paused') + ); afterEachHelper.continue(); beforeEachHelper.continue(); - it(() => { - assert.equal( - counter, - 6, - 'value incremented by 1 from unpausing beforeEach should be 6' - ); - }); + it(() => + assert.equal(counter, 6, 'Counter should be 6 after resuming beforeEach') + ); - it(() => { - assert.equal( - counter, - 8, - 'value incremented by 1 from beforeEach and 1 by previous test with afterEach should be 8' - ); - }); + it(() => + assert.equal(counter, 8, 'Counter should be 8 after before/afterEach') + ); beforeEachHelper.reset(); afterEachHelper.reset(); - it(() => { - assert.equal( - counter, - 9, - 'value should still 9 by reseting both beforeEach and afterEach' - ); - }); + it(() => + assert.equal(counter, 9, 'Counter should be 9 after resetting hooks') + ); }); diff --git a/test/integration/it/it.test.ts b/test/integration/it/it.test.ts index 881e2638..90d22bc9 100644 --- a/test/integration/it/it.test.ts +++ b/test/integration/it/it.test.ts @@ -1,30 +1,30 @@ +import { test } from '../../../src/modules/test.js'; import { describe } from '../../../src/modules/describe.js'; import { it } from '../../../src/modules/it.js'; -describe('Testing "it" method', { - icon: '🔬', -}); - -describe(async () => { - it(() => {}); - it(() => true); - it(() => false); - it(() => undefined); - it(() => new Promise((resolve) => resolve(undefined))); - it(async () => await new Promise((resolve) => resolve(undefined))); +test('Testing "it" method', () => { + describe(async () => { + it(() => {}); + it(() => true); + it(() => false); + it(() => undefined); + it(() => new Promise((resolve) => resolve(undefined))); + it(async () => await new Promise((resolve) => resolve(undefined))); - await it(() => new Promise((resolve) => resolve(undefined))); - await it(async () => await new Promise((resolve) => resolve(undefined))); -}); + await it(() => new Promise((resolve) => resolve(undefined))); + await it(async () => await new Promise((resolve) => resolve(undefined))); + }); -describe(async () => { - it('', () => {}); - it('', () => true); - it('', () => false); - it('', () => undefined); - it('', () => new Promise((resolve) => resolve(undefined))); - it('', async () => await new Promise((resolve) => resolve(undefined))); + describe(async () => { + it('', () => {}); + it('', () => true); + it('', () => false); + it('', () => undefined); + it('', () => new Promise((resolve) => resolve(undefined))); + it('', async () => await new Promise((resolve) => resolve(undefined))); - await it('', () => new Promise((resolve) => resolve(undefined))); - await it('', async () => await new Promise((resolve) => resolve(undefined))); + await it('', () => new Promise((resolve) => resolve(undefined))); + await it('', async () => + await new Promise((resolve) => resolve(undefined))); + }); }); diff --git a/test/integration/test/each/each-promise.test.ts b/test/integration/test/each/each-promise.test.ts index cd4ddd3a..9ece4868 100644 --- a/test/integration/test/each/each-promise.test.ts +++ b/test/integration/test/each/each-promise.test.ts @@ -1,82 +1,51 @@ -import { describe } from '../../../../src/modules/describe.js'; import { test } from '../../../../src/modules/test.js'; import { assert } from '../../../../src/modules/assert.js'; import { beforeEach, afterEach } from '../../../../src/modules/each.js'; -describe('Asynchronous Before and After Each Suite (test)', { - icon: '🔬', -}); - -let counter = 0; +test('Asynchronous Before and After Each Suite (test)', () => { + let counter = 0; -const asyncPreIncrement = async () => - await new Promise((resolve) => resolve(++counter)); + const asyncPreIncrement = async () => + await new Promise((resolve) => resolve(++counter)); -const beforeEachHelper = beforeEach(asyncPreIncrement, { immediate: true }); + const beforeEachHelper = beforeEach(asyncPreIncrement, { immediate: true }); -const afterEachHelper = afterEach(asyncPreIncrement, {}); + const afterEachHelper = afterEach(asyncPreIncrement, {}); -test(() => { - assert.equal( - counter, - 2, - 'value incremented by 1 from beforeEach with immediate option should be 2' - ); -}); + test(() => { + assert.equal(counter, 2, 'Counter should be 2 after immediate beforeEach'); + }); -test(() => { - assert.equal( - counter, - 4, - 'value incremented by 1 from beforeEach with immediate option and by 1 from previous test with afterEach should be 4' - ); -}); + test(() => { + assert.equal(counter, 4, 'Counter should be 4 after before/afterEach'); + }); -afterEachHelper.pause(); -beforeEachHelper.pause(); + afterEachHelper.pause(); + beforeEachHelper.pause(); -test(() => { - assert.equal( - counter, - 5, - 'value should still 5 by pausing both beforeEach and afterEach, considering the abscence of beforeEach effect' - ); -}); + test(() => { + assert.equal(counter, 5, 'Counter should remain 5 after pausing hooks'); + }); -test(() => { - assert.equal( - counter, - 5, - 'value should still 5 by pausing both beforeEach and afterEach, considering the the abscence of beforeEach and afterEach (from previous test) effects' - ); -}); + test(() => { + assert.equal(counter, 5, 'Counter should still be 5 with hooks paused'); + }); -afterEachHelper.continue(); -beforeEachHelper.continue(); + afterEachHelper.continue(); + beforeEachHelper.continue(); -test(() => { - assert.equal( - counter, - 6, - 'value incremented by 1 from unpausing beforeEach should be 6' - ); -}); + test(() => { + assert.equal(counter, 6, 'Counter should be 6 after resuming beforeEach'); + }); -test(() => { - assert.equal( - counter, - 8, - 'value incremented by 1 from beforeEach and 1 by previous test with afterEach should be 8' - ); -}); + test(() => { + assert.equal(counter, 8, 'Counter should be 8 after before/afterEach'); + }); -beforeEachHelper.reset(); -afterEachHelper.reset(); + beforeEachHelper.reset(); + afterEachHelper.reset(); -test(() => { - assert.equal( - counter, - 9, - 'value should still 9 by reseting both beforeEach and afterEach' - ); + test(() => { + assert.equal(counter, 9, 'Counter should be 9 after resetting hooks'); + }); }); diff --git a/test/integration/test/each/each.test.ts b/test/integration/test/each/each.test.ts index b96b3766..53c68210 100644 --- a/test/integration/test/each/each.test.ts +++ b/test/integration/test/each/each.test.ts @@ -1,86 +1,55 @@ -import { describe } from '../../../../src/modules/describe.js'; import { test } from '../../../../src/modules/test.js'; import { assert } from '../../../../src/modules/assert.js'; import { beforeEach, afterEach } from '../../../../src/modules/each.js'; -describe('Before and After Each Suite (test)', { - icon: '🔬', -}); - -let counter = 0; - -const beforeEachHelper = beforeEach( - () => { - ++counter; - }, - { immediate: true } -); +test('Before and After Each Suite (test)', () => { + let counter = 0; -const afterEachHelper = afterEach(() => { - ++counter; -}, {}); + const beforeEachHelper = beforeEach(() => ++counter, { immediate: true }); + const afterEachHelper = afterEach(() => ++counter, {}); -test(() => { - assert.equal( - counter, - 2, - 'value incremented by 1 from beforeEach with immediate option should be 2' + test(() => + assert.equal(counter, 2, 'Counter should be 2 after immediate beforeEach') ); -}); -test(() => { - assert.equal( - counter, - 4, - 'value incremented by 1 from beforeEach with immediate option and by 1 from previous test with afterEach should be 4' + test(() => + assert.equal( + counter, + 4, + 'Counter should be 4 after beforeEach and afterEach' + ) ); -}); -afterEachHelper.pause(); -beforeEachHelper.pause(); + afterEachHelper.pause(); + beforeEachHelper.pause(); -test(() => { - assert.equal( - counter, - 5, - 'value should still 5 by pausing both beforeEach and afterEach, considering the abscence of beforeEach effect' + test(() => + assert.equal(counter, 5, 'Counter should remain 5 after pausing hooks') ); -}); -test(() => { - assert.equal( - counter, - 5, - 'value should still 5 by pausing both beforeEach and afterEach, considering the the abscence of beforeEach and afterEach (from previous test) effects' + test(() => + assert.equal(counter, 5, 'Counter should still be 5 with hooks paused') ); -}); -afterEachHelper.continue(); -beforeEachHelper.continue(); + afterEachHelper.continue(); + beforeEachHelper.continue(); -test(() => { - assert.equal( - counter, - 6, - 'value incremented by 1 from unpausing beforeEach should be 6' + test(() => + assert.equal(counter, 6, 'Counter should be 6 after resuming beforeEach') ); -}); -test(() => { - assert.equal( - counter, - 8, - 'value incremented by 1 from beforeEach and 1 by previous test with afterEach should be 8' + test(() => + assert.equal( + counter, + 8, + 'Counter should be 8 after beforeEach and afterEach' + ) ); -}); -beforeEachHelper.reset(); -afterEachHelper.reset(); + beforeEachHelper.reset(); + afterEachHelper.reset(); -test(() => { - assert.equal( - counter, - 9, - 'value should still 9 by reseting both beforeEach and afterEach' + test(() => + assert.equal(counter, 9, 'Counter should be 9 after resetting hooks') ); }); diff --git a/test/integration/test/test.test.ts b/test/integration/test/test.test.ts index 3b2955ad..4a01136c 100644 --- a/test/integration/test/test.test.ts +++ b/test/integration/test/test.test.ts @@ -1,11 +1,6 @@ -import { describe } from '../../../src/modules/describe.js'; import { test } from '../../../src/modules/test.js'; -describe('Testing "test" method', { - icon: '🔬', -}); - -test(async () => { +test('Testing "test" method', async () => { test(() => {}); test(() => true); test(() => false); diff --git a/test/unit/assert-result-type.test.ts b/test/unit/assert-result-type.test.ts index eb64f199..949abe54 100644 --- a/test/unit/assert-result-type.test.ts +++ b/test/unit/assert-result-type.test.ts @@ -1,12 +1,9 @@ -import { describe } from '../../src/modules/describe.js'; import { test } from '../../src/modules/test.js'; import { assert } from '../../src/modules/assert.js'; import { parseResultType } from '../../src/helpers/parse-assertion.js'; import { nodeVersion } from '../../src/helpers/get-runtime.js'; -describe('Assert: Parse Result Type', { icon: '🔬' }); - -test(async () => { +test('Assert: Parse Result Type', async () => { assert.deepStrictEqual( parseResultType(), 'undefined', diff --git a/test/unit/deno/allow.test.ts b/test/unit/deno/allow.test.ts index e6f421ed..f4a855d0 100644 --- a/test/unit/deno/allow.test.ts +++ b/test/unit/deno/allow.test.ts @@ -1,11 +1,8 @@ -import { describe } from '../../../src/modules/describe.js'; import { test } from '../../../src/modules/test.js'; import { assert } from '../../../src/modules/assert.js'; import { runner } from '../../../src/helpers/runner.js'; -describe('Deno Permissions (Allow)', { icon: '🔬' }); - -test(() => { +test('Deno Permissions (Allow)', () => { assert.deepStrictEqual( runner('', { platform: 'deno', diff --git a/test/unit/deno/cjs.test.ts b/test/unit/deno/cjs.test.ts index f2a08717..5d2efc83 100644 --- a/test/unit/deno/cjs.test.ts +++ b/test/unit/deno/cjs.test.ts @@ -1,44 +1,48 @@ import process from 'node:process'; import { spawn } from 'node:child_process'; -import { getRuntime } from '../../../src/helpers/get-runtime.js'; -import { describe } from '../../../src/modules/describe.js'; +import { test } from '../../../src/modules/test.js'; import { assert } from '../../../src/modules/assert.js'; +import { getRuntime } from '../../../src/helpers/get-runtime.js'; const runtime = getRuntime(); if (runtime !== 'deno') process.exit(0); -describe('Deno Compatibility', { icon: '🦕' }); +test('Deno Compatibility', async () => { + const FILE = './fixtures/deno/require.cjs'; + const polyfillPath = './lib/polyfills/deno.mjs'; -const FILE = './fixtures/deno/require.cjs'; -const polyfillPath = './lib/polyfills/deno.mjs'; + const command = 'deno'; + const args = ['run', '--allow-env', '--allow-read', polyfillPath]; + const env = { ...process.env, FILE }; -const command = 'deno'; -const args = ['run', '--allow-env', '--allow-read', polyfillPath]; -const env = { ...process.env, FILE }; + const denoProcess = spawn(command, args, { env }); -const denoProcess = spawn(command, args, { env }); + let output: string = ''; -let output: string = ''; + denoProcess.stdout.on('data', (data) => { + output += String(data); + }); -denoProcess.stdout.on('data', (data) => { - output += String(data); -}); + denoProcess.stderr.on('data', (data) => { + output += String(data); + }); -denoProcess.stderr.on('data', (data) => { - output += String(data); -}); + denoProcess.on('error', (err) => { + assert.ifError(err); + }); -denoProcess.on('error', (err) => { - console.log(err); -}); + const code = await new Promise((resolve) => + denoProcess.on('close', (code) => { + assert.strictEqual( + output.trim(), + 'Hello from module.exports\nHello from exports'.trim(), + 'Testing CJS polyfill' + ); -denoProcess.on('close', (code) => { - assert.strictEqual( - output.trim(), - 'Hello from module.exports\nHello from exports'.trim(), - 'Testing CJS polyfill' + resolve(code); + }) ); - if (code !== 0) process.exit(code); + assert.strictEqual(code, 0, 'Expect for success'); }); diff --git a/test/unit/deno/deny.test.ts b/test/unit/deno/deny.test.ts index b9da0bdf..32954558 100644 --- a/test/unit/deno/deny.test.ts +++ b/test/unit/deno/deny.test.ts @@ -1,11 +1,8 @@ -import { describe } from '../../../src/modules/describe.js'; import { test } from '../../../src/modules/test.js'; import { assert } from '../../../src/modules/assert.js'; import { runner } from '../../../src/helpers/runner.js'; -describe('Deno Permissions (Deny)', { icon: '🔬' }); - -test(() => { +test('Deno Permissions (Deny)', () => { assert.deepStrictEqual( runner('', { platform: 'deno', diff --git a/test/unit/map-tests.test.ts b/test/unit/map-tests.test.ts index 240e9496..a8b114f9 100644 --- a/test/unit/map-tests.test.ts +++ b/test/unit/map-tests.test.ts @@ -52,7 +52,11 @@ describe('mapTests', async () => { ], ]); - assert.deepStrictEqual(importMap, expected); + assert.deepStrictEqual( + importMap, + expected, + 'Check if tes are correctly mapped to their corresponding source files.' + ); }); await it('should map single test file correctly', async () => { @@ -66,6 +70,10 @@ describe('mapTests', async () => { ], ]); - assert.deepStrictEqual(importMap, expected); + assert.deepStrictEqual( + importMap, + expected, + 'Check if a test file is correctly mapped to its corresponding source file.' + ); }); }); diff --git a/test/unit/run-test-file.test.ts b/test/unit/run-test-file.test.ts index b57e13b1..2f3184ee 100644 --- a/test/unit/run-test-file.test.ts +++ b/test/unit/run-test-file.test.ts @@ -1,6 +1,6 @@ import process from 'node:process'; import { describe } from '../../src/modules/describe.js'; -import { test } from '../../src/modules/test.js'; +import { it } from '../../src/modules/it.js'; import { assert } from '../../src/modules/assert.js'; import { runTestFile } from '../../src/services/run-test-file.js'; import { getRuntime } from '../../src/helpers/get-runtime.js'; @@ -8,20 +8,22 @@ import { getRuntime } from '../../src/helpers/get-runtime.js'; const isProduction = process.env.NODE_ENV === 'production'; const ext = getRuntime() === 'deno' ? 'ts' : isProduction ? 'js' : 'ts'; -describe('Service: runTestFile', { icon: '🔬' }); +describe('Service: runTestFile', async () => { + await Promise.all([ + it(async () => { + const code = await runTestFile(`./fixtures/fail/exit.test.${ext}`, { + quiet: true, + }); -test(async () => { - const code = await runTestFile(`./fixtures/fail/exit.test.${ext}`, { - quiet: true, - }); + assert.deepStrictEqual(code, false, 'Failure test file case'); + }), - assert.deepStrictEqual(code, false, 'Failure test file case'); -}); - -test(async () => { - const code = await runTestFile(`./fixtures/success/exit.test.${ext}`, { - quiet: true, - }); + it(async () => { + const code = await runTestFile(`./fixtures/success/exit.test.${ext}`, { + quiet: true, + }); - assert.deepStrictEqual(code, true, 'Success test file case'); + assert.deepStrictEqual(code, true, 'Success test file case'); + }), + ]); }); diff --git a/test/unit/run-tests.test.ts b/test/unit/run-tests.test.ts index 6e80741a..955dd737 100644 --- a/test/unit/run-tests.test.ts +++ b/test/unit/run-tests.test.ts @@ -1,24 +1,26 @@ import { describe } from '../../src/modules/describe.js'; -import { test } from '../../src/modules/test.js'; +import { it } from '../../src/modules/it.js'; import { assert } from '../../src/modules/assert.js'; import { runTests } from '../../src/services/run-tests.js'; -describe('Service: runTests', { icon: '🔬' }); +describe('Service: runTests', async () => { + await Promise.all([ + it(async () => { + const code = await runTests('./fixtures/fail', { + noExit: true, + quiet: true, + }); -test(async () => { - const code = await runTests('./fixtures/fail', { - noExit: true, - quiet: true, - }); + assert.deepStrictEqual(code, false, 'Failure test directory case'); + }), - assert.deepStrictEqual(code, false, 'Failure test directory case'); -}); - -test(async () => { - const code = await runTests('./fixtures/success', { - noExit: true, - quiet: true, - }); + it(async () => { + const code = await runTests('./fixtures/success', { + noExit: true, + quiet: true, + }); - assert.deepStrictEqual(code, true, 'Success test directory case'); + assert.deepStrictEqual(code, true, 'Success test directory case'); + }), + ]); }); diff --git a/website/docs/index.mdx b/website/docs/index.mdx index bbd4d76a..20d445d6 100644 --- a/website/docs/index.mdx +++ b/website/docs/index.mdx @@ -193,19 +193,19 @@ deno run npm:poku ### Essentials -- [**poku**](https://poku.io/docs/category/poku) _(test runner)_ -- [**assert**](https://poku.io/docs/documentation/assert) _(test assertion)_ +- [**poku**](/docs/category/poku) _(test runner)_ +- [**assert**](/docs/documentation/assert) _(test assertion)_ ### Helpers -- [**test**](https://poku.io/docs/documentation/helpers/test) - , [**describe**](https://poku.io/docs/documentation/helpers/describe) and [**it**](https://poku.io/docs/documentation/helpers/it) _(organize, group, and isolate tests)_ -- [**beforeEach**](https://poku.io/docs/category/beforeeach-and-aftereach) and [**afterEach**](https://poku.io/docs/category/beforeeach-and-aftereach) _(hooks for test setup and teardown)_ -- [**watch**](https://poku.io/docs/documentation/poku/options/watch) _(watch test files for changes)_ -- [**startScript**](https://poku.io/docs/documentation/startScript) _(run **package.json** scripts in background)_ -- [**startService**](https://poku.io/docs/documentation/startService) _(run files in background)_ -- [**kill**](https://poku.io/docs/documentation/processes/kill) _(terminate ports, port ranges, and PIDs)_ -- [**getPIDs**](https://poku.io/docs/documentation/processes/get-pids) _(debug processes IDs using ports and port ranges)_ +- [**test**](/docs/documentation/helpers/test) + , [**describe**](/docs/documentation/helpers/describe) and [**it**](/docs/documentation/helpers/it) _(organize, group, and isolate tests)_ +- [**watch**](/docs/documentation/poku/options/watch) _(watch for changes and re-run related test files)_ +- [**beforeEach**](/docs/category/beforeeach-and-aftereach) and [**afterEach**](/docs/category/beforeeach-and-aftereach) _(hooks for test setup and teardown)_ +- [**startScript**](/docs/documentation/startScript) _(run **package.json** scripts in background)_ +- [**startService**](/docs/documentation/startService) _(run files in background)_ +- [**kill**](/docs/documentation/processes/kill) _(terminate ports, port ranges, and PIDs)_ +- [**getPIDs**](/docs/documentation/processes/get-pids) _(debug processes IDs using ports and port ranges)_ - _and much more_ 👇🏻 [**See the complete documentation**](/docs/category/documentation).