diff --git a/src/modules/helpers/assert-promise.ts b/src/modules/helpers/assert-promise.ts deleted file mode 100644 index ecbe548c..00000000 --- a/src/modules/helpers/assert-promise.ts +++ /dev/null @@ -1,350 +0,0 @@ -/* c8 ignore next */ // Types -import type { ProcessAssertionOptions } from '../../@types/assert.js'; -import * as nodeAssert from 'node:assert'; -import { processAssert } from '../../services/assert.js'; -import { nodeVersion } from '../../parsers/get-runtime.js'; - -const ok = async ( - value: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert( - () => { - nodeAssert.ok(value); - }, - { message } - ); -}; - -const equal = async ( - actual: unknown, - expected: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert( - () => { - nodeAssert.equal(actual, expected); - }, - { message } - ); -}; - -const deepEqual = async ( - actual: unknown, - expected: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert(() => nodeAssert.deepEqual(actual, expected), { - message, - }); -}; - -const strictEqual = async ( - actual: unknown, - expected: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert(() => nodeAssert.strictEqual(actual, expected), { - message, - }); -}; - -const deepStrictEqual = async ( - actual: unknown, - expected: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert(() => nodeAssert.deepStrictEqual(actual, expected), { - message, - }); -}; - -const notEqual = async ( - actual: unknown, - expected: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert(() => nodeAssert.notEqual(actual, expected), { - message, - }); -}; - -const notDeepEqual = async ( - actual: unknown, - expected: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert(() => nodeAssert.notDeepEqual(actual, expected), { - message, - }); -}; - -const notStrictEqual = async ( - actual: unknown, - expected: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert(() => nodeAssert.notStrictEqual(actual, expected), { - message, - }); -}; - -const notDeepStrictEqual = async ( - actual: unknown, - expected: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert(() => nodeAssert.notDeepStrictEqual(actual, expected), { - message, - }); -}; - -const ifError = async ( - value: unknown, - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert( - () => { - nodeAssert.ifError(value); - }, - { - message, - defaultMessage: 'Expected no error, but received an error', - hideDiff: true, - throw: true, - } - ); -}; - -/* c8 ignore start */ -const fail = async ( - message?: ProcessAssertionOptions['message'] -): Promise => { - await processAssert( - () => { - nodeAssert.fail(message); - }, - { - message, - defaultMessage: 'Test failed intentionally', - hideDiff: true, - } - ); - - process.exit(1); -}; -/* c8 ignore stop */ - -function doesNotThrow( - block: () => unknown, - message?: string | ProcessAssertionOptions['message'] -): Promise; -function doesNotThrow( - block: () => unknown, - error: nodeAssert.AssertPredicate, - message?: ProcessAssertionOptions['message'] -): Promise; -async function doesNotThrow( - block: () => unknown, - errorOrMessage?: - | nodeAssert.AssertPredicate - | ProcessAssertionOptions['message'], - message?: ProcessAssertionOptions['message'] -): Promise { - await processAssert( - () => { - if ( - typeof errorOrMessage === 'function' || - errorOrMessage instanceof RegExp || - typeof errorOrMessage === 'object' - ) { - nodeAssert.doesNotThrow(block, errorOrMessage, message); - } else { - const msg = - typeof errorOrMessage === 'string' ? errorOrMessage : message; - nodeAssert.doesNotThrow(block, msg); - } - }, - { - message: typeof errorOrMessage === 'string' ? errorOrMessage : message, - defaultMessage: 'Expected function not to throw', - hideDiff: true, - throw: true, - } - ); -} - -function throws( - block: () => unknown, - message?: ProcessAssertionOptions['message'] -): Promise; -function throws( - block: () => unknown, - error: nodeAssert.AssertPredicate, - message?: ProcessAssertionOptions['message'] -): Promise; -async function throws( - block: () => unknown, - errorOrMessage?: - | nodeAssert.AssertPredicate - | ProcessAssertionOptions['message'], - message?: ProcessAssertionOptions['message'] -): Promise { - if ( - typeof errorOrMessage === 'function' || - errorOrMessage instanceof RegExp || - typeof errorOrMessage === 'object' - ) { - await processAssert(() => nodeAssert.throws(block, errorOrMessage), { - message, - defaultMessage: 'Expected function to throw', - hideDiff: true, - }); - } else { - const msg = - typeof errorOrMessage !== 'undefined' ? errorOrMessage : message; - - await processAssert(() => nodeAssert.throws(block, message), { - message: msg, - defaultMessage: 'Expected function to throw', - hideDiff: true, - }); - } -} - -function rejects( - block: (() => Promise) | Promise, - message?: ProcessAssertionOptions['message'] -): Promise; -function rejects( - block: (() => Promise) | Promise, - error: nodeAssert.AssertPredicate, - message?: ProcessAssertionOptions['message'] -): Promise; -async function rejects( - block: (() => Promise) | Promise, - errorOrMessage?: - | nodeAssert.AssertPredicate - | ProcessAssertionOptions['message'], - message?: ProcessAssertionOptions['message'] -): Promise { - await processAssert( - async () => { - if ( - typeof errorOrMessage === 'function' || - errorOrMessage instanceof RegExp || - typeof errorOrMessage === 'object' - ) { - await nodeAssert.rejects(block, errorOrMessage, message); - } else { - const msg = - typeof errorOrMessage === 'string' ? errorOrMessage : message; - await nodeAssert.rejects(block, msg); - } - }, - { - message: typeof errorOrMessage === 'string' ? errorOrMessage : message, - defaultMessage: 'Expected promise to be rejected with specified error', - hideDiff: true, - throw: true, - } - ); -} - -function doesNotReject( - block: (() => Promise) | Promise, - message?: ProcessAssertionOptions['message'] -): Promise; -function doesNotReject( - block: (() => Promise) | Promise, - error: nodeAssert.AssertPredicate, - message?: ProcessAssertionOptions['message'] -): Promise; -async function doesNotReject( - block: (() => Promise) | Promise, - errorOrMessage?: - | nodeAssert.AssertPredicate - | ProcessAssertionOptions['message'], - message?: ProcessAssertionOptions['message'] -): Promise { - await processAssert( - async () => { - if ( - typeof errorOrMessage === 'function' || - errorOrMessage instanceof RegExp || - typeof errorOrMessage === 'object' - ) { - await nodeAssert.doesNotReject(block, errorOrMessage, message); - } else { - await nodeAssert.doesNotReject(block, message); - } - }, - { - message: typeof errorOrMessage === 'string' ? errorOrMessage : message, - defaultMessage: 'Got unwanted rejection', - hideDiff: true, - throw: true, - } - ); -} - -const match = async ( - value: string, - regExp: RegExp, - message?: ProcessAssertionOptions['message'] -): Promise => { - /* c8 ignore next 3 */ - if (typeof nodeVersion === 'number' && nodeVersion < 12) { - throw new Error('match is available from Node.js 12 or higher'); - } - - await processAssert(() => nodeAssert?.match(value, regExp), { - message, - actual: 'Value', - expected: 'RegExp', - defaultMessage: 'Value should match regExp', - }); -}; - -const doesNotMatch = async ( - value: string, - regExp: RegExp, - message?: ProcessAssertionOptions['message'] -): Promise => { - /* c8 ignore next 3 */ - if (typeof nodeVersion === 'number' && nodeVersion < 12) { - throw new Error('doesNotMatch is available from Node.js 12 or higher'); - } - - await processAssert(() => nodeAssert.doesNotMatch(value, regExp), { - message, - actual: 'Value', - expected: 'RegExp', - defaultMessage: 'Value should not match regExp', - }); -}; - -export const assertPromise = Object.assign( - (value: unknown, message?: ProcessAssertionOptions['message']) => - ok(value, message), - { - ok, - equal, - deepEqual, - strictEqual, - deepStrictEqual, - doesNotMatch, - doesNotReject, - throws, - doesNotThrow, - notEqual, - notDeepEqual, - notStrictEqual, - notDeepStrictEqual, - match, - ifError, - fail, - rejects, - } - /* c8 ignore next */ // ? -); diff --git a/src/modules/index.ts b/src/modules/index.ts index daccd749..4b145af7 100755 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -18,7 +18,6 @@ export { getPIDs } from './helpers/get-pids.js'; export { exit } from './helpers/exit.js'; export { log } from './helpers/log.js'; export { listFiles } from './helpers/list-files.js'; -export { assertPromise } from './helpers/assert-promise.js'; export type { Code } from '../@types/code.js'; export type { Configs } from '../@types/poku.js'; export type { diff --git a/test/integration/assert/assert-promise-no-message.test.ts b/test/integration/assert/assert-promise-no-message.test.ts deleted file mode 100644 index 6b659468..00000000 --- a/test/integration/assert/assert-promise-no-message.test.ts +++ /dev/null @@ -1,192 +0,0 @@ -import { describe } from '../../../src/modules/helpers/describe.js'; -import { it } from '../../../src/modules/helpers/it.js'; -import { assertPromise as assert } from '../../../src/modules/helpers/assert-promise.js'; -import { nodeVersion } from '../../../src/parsers/get-runtime.js'; - -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' - ); - }); - - 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'); - }); - } - }); - - it(() => { - if (!nodeVersion || nodeVersion > 12) { - const text = 'sample text'; - - it(() => { - assert.match(text, /sample/); - }); - - it(() => { - assert.doesNotMatch(text, /notpresent/); - assert.doesNotMatch('abc', /123/); - assert.doesNotMatch('', /\d/); - assert.doesNotMatch('abc', /\d+/); - }); - } - }); - - 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')); - }); - - 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 deleted file mode 100644 index 1a3b6fc0..00000000 --- a/test/integration/assert/assert-promise.test.ts +++ /dev/null @@ -1,345 +0,0 @@ -import { describe } from '../../../src/modules/helpers/describe.js'; -import { it } from '../../../src/modules/helpers/it.js'; -import { assertPromise as assert } from '../../../src/modules/helpers/assert-promise.js'; -import { nodeVersion } from '../../../src/parsers/get-runtime.js'; - -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.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( - () => { - return 'test'; - }, - /test/, - 'RegExp predicate should not match' - ); - - assert.doesNotThrow( - () => 1 + 1, - 'doesNotThrow with non-throwing function' - ); - - 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 assert.rejects( - asyncFunctionThatRejects, - /Async error/, - 'Should reject with the specified regex message' - ); - await assert.rejects( - asyncFunctionThatRejects, - { message: 'Async error' }, - 'Should reject with the specified predicate message' - ); - await assert.rejects( - asyncFunctionThatRejects, - 'Should reject with the specified string message' - ); - await assert.rejects( - asyncFunctionThatRejects, - // @ts-expect-error invalid third param - undefined, - 'Should reject with the specified string message (third arg)' - ); - }); - - 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' - ); - await assert.doesNotReject( - asyncFunctionThatResolves, - /Resolved successfully/ - ); - }); - } - }); -}); diff --git a/test/integration/import.test.ts b/test/integration/import.test.ts index 8fb3e2c4..64241722 100644 --- a/test/integration/import.test.ts +++ b/test/integration/import.test.ts @@ -5,7 +5,6 @@ index.test('Import Suite', () => { index.assert.ok(index.assert, 'Importing assert method'); index.assert.ok(index.envFile, 'Importing envFile 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.docker, 'Importing docker method'); diff --git a/website/docs/documentation/helpers/before-after-each/in-code.mdx b/website/docs/documentation/helpers/before-after-each/in-code.mdx index 1968e790..4abc7588 100644 --- a/website/docs/documentation/helpers/before-after-each/in-code.mdx +++ b/website/docs/documentation/helpers/before-after-each/in-code.mdx @@ -55,246 +55,14 @@ await test(async () => { }); ``` -## Combine `beforeEach`, `afterEach` and `assert` - -Since `test` is completely optional on **Poku**, you can use `beforeEach`, `afterEach` and `assert` together: - - - - - ```ts - import { assert, beforeEach, afterEach } from 'poku'; - - const prepareService = () => true; - const resetService = () => true; - - beforeEach(() => prepareService(), { assert: true }); - afterEach(() => resetService(), { assert: true }); - - assert(true, 'Test A'); - assert(true, 'Test B'); - assert(true, 'Test C'); - assert(true, 'Test D'); - ``` - :::tip -- ✅ Handling **global** and **external** services (_preparing a database, for example_) -- ✅ It's made for **exclusive use** in combination with **Poku**'s **`assert`** methods -- ✅ You can combine `beforeEach`, `afterEach` and all `assert` methods, except for `assert.fail(message?: string)`. - -::: - -:::info - -- Although `beforeEach` and `afterEach` accepts local variables changes, it's discouraged ([_you can use a mock instead_](/docs/category/mock)). - -See why (_note the `immediate` option_): - -```ts -import { assert, beforeEach, afterEach } from 'poku'; - -let value = 0; - -beforeEach(() => ++value, { immediate: true, assert: true }); -afterEach(() => ++value, { assert: true }); - -assert.equal(value, 1); // ✅ -assert.equal(value, 3); // ✅ - -// ✋ In the `eachBefore` context, `value` is now `4`, while locally it's `5` -console.log(value); -``` - -::: - - - - - ```ts - import { assert } from 'poku'; - - const prepareService = () => true; - const resetService = () => true; - - prepareService(); - assert(true, 'Test A'); - resetService(); - - prepareService(); - assert(true, 'Test B'); - resetService(); - - prepareService(); - assert(true, 'Test C'); - resetService(); - - prepareService(); - assert(true, 'Test D'); - resetService(); - ``` - - - - -**Poku** provides three optional methods from both `beforeEach` and `afterEach`: - - - `.pause()` - - `.continue()` - - `.reset()` - - ```ts - import { assert, beforeEach, afterEach } from 'poku'; - - const prepareService = () => true; - const resetService = () => true; - - const before = beforeEach(() => prepareService()); - const after = afterEach(() => resetService()); - - assert.ok(true, 'Test A'); - assert.ok(true, 'Test B'); - - before.pause(); - - assert.ok(true, 'Test C'); - assert.ok(true, 'Test D'); - - before.continue(); - - assert.ok(true, 'Test E'); - - // From now, it will not run beforeEach until you declare it again. - before.reset(); - - assert.ok(true, 'Test F'); - ``` - -:::tip You can overwriting both `beforeEach` and `afterEach` by declaring them again anytime. -::: - - - -### By using Promises - - - - - ```ts - import { assertPromise as assert, beforeEach, afterEach } from 'poku'; - - const prepareService = () => new Promise((resolve) => resolve(true)); - const resetService = () => new Promise((resolve) => resolve(true)); - - beforeEach(async () => await prepareService()); - afterEach(async () => await resetService()); - - await assert(true, 'Test A'); - await assert(true, 'Test B'); - await assert(true, 'Test C'); - await assert(true, 'Test D'); - ``` - -:::tip - -- ✅ Handling **global** and **external** services (_preparing a database, for example_) -- ✅ It's made for **exclusive use** in combination with **Poku**'s **`assert`** methods -- ✅ You can combine `beforeEach`, `afterEach` and all `assert` methods, except for `assert.fail(message?: string)`. - -::: - -:::info - -- Although `beforeEach` and `afterEach` accepts local variables changes, it's discouraged ([_you can use a mock instead_](/docs/category/mock)). - -See why (_note the `immediate` option_): - -```ts -import { assertPromise as assert, beforeEach, afterEach } from 'poku'; - -let value = 0; - -beforeEach(async () => new Promise((resolve) => resolve(++value)), { - immediate: true, -}); -afterEach(async () => new Promise((resolve) => resolve(++value))); - -await assert.equal(value, 1); // ✅ -await assert.equal(value, 3); // ✅ - -// ✋ In the `eachBefore` context, `value` is now `4`, while locally it's `5` -console.log(value); -``` - -::: - - - - - ```ts - import { assert } from 'poku'; - - const prepareService = () => new Promise((resolve) => resolve(true)); - const resetService = () => new Promise((resolve) => resolve(true)); - - await prepareService(); - assert(true, 'Test A'); - await resetService(); - - await prepareService(); - assert(true, 'Test B'); - await resetService(); - - await prepareService(); - assert(true, 'Test C'); - await resetService(); - - await prepareService(); - assert(true, 'Test D'); - await resetService(); - ``` - - - - - **Poku** provides three optional methods from both `beforeEach` and `afterEach`: - - - `.pause()` - - `.continue()` - - `.reset()` - - ```ts - import { assertPromise as assert, beforeEach, afterEach } from 'poku'; - - const prepareService = () => new Promise((resolve) => resolve(true)); - const resetService = () => new Promise((resolve) => resolve(true)); - - const before = await beforeEach(async () => await prepareService()); - const after = await afterEach(async () => await resetService()); - - await assert.ok(true, 'Test A'); - await assert.ok(true, 'Test B'); - - before.pause(); - - await assert.ok(true, 'Test C'); - await assert.ok(true, 'Test D'); - - before.continue(); - - await assert.ok(true, 'Test E'); - - // From now, it will not run beforeEach until you declare it again. - before.reset(); +**Poku** provides three optional methods from both `beforeEach` and `afterEach`: - await assert.ok(true, 'Test F'); - ``` +- `.pause()` +- `.continue()` +- `.reset()` -:::tip -You can overwriting both `beforeEach` and `afterEach` by declaring them again anytime. ::: - - -