Skip to content

Commit

Permalink
fix(assert): allow custom message for ifError (#147)
Browse files Browse the repository at this point in the history
Thanks to @kusmin for the idea 🤝
  • Loading branch information
wellwelwel authored Mar 24, 2024
1 parent 54d6a2e commit 7252fd4
Show file tree
Hide file tree
Showing 7 changed files with 956 additions and 399 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ Enjoying **Poku**? Consider giving him a star ⭐️
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Less verbose<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> High **isolation** level per file<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> No eval needed 🔐<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> No global state<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> **Parallel** and **Sequential** runs 🏃🏽🏃🏻<br />

<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> **Poku** is [**100%** documented](https://poku.io/docs)<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Designed to be human-friendly<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> **Poku** doesn't use a global state<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Compatible with **Coverage** tools<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> [**Node.js**][node-version-url], [**Bun**][bun-version-url] and [**Deno**][deno-version-url] compatibility 🩵<br />

Expand Down Expand Up @@ -190,11 +190,10 @@ deno run npm:poku

### Helpers

- `beforeEach` and `afterEach`
- `test`
- `describe` and `log`
- `listFiles`
- `exit`
- [**beforeEach**](https://poku.io/docs/category/beforeeach-and-aftereach) and [**afterEach**](https://poku.io/docs/category/beforeeach-and-aftereach)
- [**test**](https://poku.io/docs/documentation/helpers/test)
- [**describe**](https://poku.io/docs/documentation/helpers/describe)
- _and much more_

[**See the complete documentation**](https://poku.io/docs).

Expand Down
6 changes: 5 additions & 1 deletion src/modules/assert-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ const notDeepStrictEqual = async (
});
};

const ifError = async (value: unknown): Promise<void> => {
const ifError = async (
value: unknown,
message?: ParseAssertionOptions['message']
): Promise<void> => {
await parseAssertion(
() => {
nodeAssert.ifError(value);
},
{
message,
defaultMessage: 'Expected no error, but received an error',
hideDiff: true,
throw: true,
Expand Down
8 changes: 6 additions & 2 deletions src/modules/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ const notDeepStrictEqual = (
});
};

const ifError = (value: unknown, message?: string): void => {
const ifError = (
value: unknown,
message?: ParseAssertionOptions['message']
): void => {
parseAssertion(
() => {
nodeAssert.ifError(value);
},
{
defaultMessage: message || 'Expected no error, but received an error',
message,
defaultMessage: 'Expected no error, but received an error',
hideDiff: true,
throw: true,
}
Expand Down
191 changes: 191 additions & 0 deletions test/unit/asser-promise-no-message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import { nodeVersion } from '../../src/helpers/get-runtime.js';
import { assertPromise as assert, describe, test } from '../../src/index.js';

describe('Assert Promise Suite (No Message)', {
background: false,
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);
});

test(() => {
if (!nodeVersion || nodeVersion > 8) {
const obj = { a: 1 };

const functionThatThrows = () => {
throw new Error('Specific error');
};

test(() => {
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(() => {
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');
});
}
});

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);
});
}
});
Loading

0 comments on commit 7252fd4

Please sign in to comment.