Skip to content

Commit

Permalink
chore: improve assert test coverage (#142)
Browse files Browse the repository at this point in the history
* feature: add testes in assert

* feature: add testes in assert promisse

* add tests

* feature: finalize tests in assert

* feature: format

* fix: fix lint

* fix: fix version helper

* fix: adjust test rejects

* fix: adjust test promisse

* fix: adjust rejects node 10 or higher

* fix: adjust rejects node 10 or higher

* fix: adjust doesNotReject node 10 or higher

* fix: organize assert.test.ts

* fix: node 8 assert throw

* fix: adjust function callback
  • Loading branch information
kusmin committed Mar 23, 2024
1 parent 0e6e08e commit d224bbd
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/helpers/version-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import process from 'node:process';

const getVersionNumber = () => {
const versionMatch = process.version.match(/^v(\d+)/);
return versionMatch ? parseInt(versionMatch[1], 10) : 0;
};

const version = getVersionNumber();

export const isNode12OrHigher = () => version >= 12;
export const isNode14OrHigher = () => version >= 14;

export const isNode10OrHigher = () => version >= 10;

export const isNode8OrHigher = () => version >= 8;

export const supportsESM = () => isNode14OrHigher();
4 changes: 2 additions & 2 deletions src/modules/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ const notDeepStrictEqual = (
});
};

const ifError = (value: unknown): void => {
const ifError = (value: unknown, message?: string): void => {
parseAssertion(
() => {
nodeAssert.ifError(value);
},
{
defaultMessage: 'Expected no error, but received an error',
defaultMessage: message || 'Expected no error, but received an error',
hideDiff: true,
throw: true,
}
Expand Down
114 changes: 108 additions & 6 deletions test/unit/assert-promise.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assertPromise as assert, describe } from '../../src/index.js';
import {
isNode10OrHigher,
isNode8OrHigher,
} from '../../src/helpers/version-helper.js';

describe('Assert (Promise) Suite', { background: false, icon: '馃敩' });

Expand All @@ -17,12 +21,6 @@ assert.strictEqual('text', 'text', 'strictEqual with same strings');
assert.deepStrictEqual({ a: 1 }, { a: 1 }, 'deepStrictEqual with same objects');
assert.deepStrictEqual([1, 2], [1, 2], 'deepStrictEqual with same arrays');

assert.doesNotThrow(() => 1 + 1, 'doesNotThrow with non-throwing function');

assert.throws(() => {
throw new Error('error');
}, 'throws with throwing function');

assert.notEqual(1, 2, 'notEqual with different numbers');

assert.notDeepEqual({ a: 1 }, { a: 2 }, 'notDeepEqual with different objects');
Expand All @@ -38,3 +36,107 @@ assert.notDeepStrictEqual(
{ a: '1' },
'notDeepStrictEqual with loosely equal but not strictly deep equal objects'
);

(async () => {
const trueValue = true;
await assert.ok(trueValue, 'Should resolve to true');

const oneValue = 1;
await assert.ok(oneValue, 'Should resolve to 1');

const numberOne = 1;
await assert.equal(numberOne, 1, 'Should resolve to equal 1');

const textValue = 'text';
await assert.equal(textValue, 'text', 'Should resolve to equal "text"');

const objectValue = { a: 1 };
await assert.deepEqual(
objectValue,
{ a: 1 },
'Should resolve to deep equal the object'
);

const arrayValue = [1, 2];
await assert.deepEqual(
arrayValue,
[1, 2],
'Should resolve to deep equal the array'
);

const strictNumber = 1;
await assert.strictEqual(
strictNumber,
1,
'Should resolve to strictly equal 1'
);

const strictText = 'text';
await assert.strictEqual(
strictText,
'text',
'Should resolve to strictly equal "text"'
);

const deepStrictObject = { a: 1 };
await assert.deepStrictEqual(
deepStrictObject,
{ a: 1 },
'Should resolve to deep strictly equal the object'
);

const deepStrictArray = [1, 2];
await assert.deepStrictEqual(
deepStrictArray,
[1, 2],
'Should resolve to deep strictly equal the array'
);

if (isNode8OrHigher()) {
await assert.doesNotThrow(
() => Promise.resolve('no error'),
'Should not throw an error'
);

await assert.doesNotThrow(
() => 1 + 1,
'doesNotThrow with non-throwing function'
);

await assert.throws(() => {
throw new Error('error');
}, 'throws with throwing function');
}

if (isNode10OrHigher()) {
await assert.rejects(
async () => await Promise.reject(new Error('error')),
Error('error'),
'Should throw an error'
);
}

const notEqualNumber = 1;
await assert.notEqual(notEqualNumber, 2, 'Should resolve to not equal 2');

const notDeepEqualObject = { a: 1 };
await assert.notDeepEqual(
notDeepEqualObject,
{ a: 2 },
'Should resolve to not deep equal the object'
);

const notStrictEqualNumber = 1;
await assert.notStrictEqual(
notStrictEqualNumber,
'2',
'Should resolve to not strictly equal "2"'
);

const notDeepStrictEqualObject = { a: 1 };
await assert.notDeepStrictEqual(
notDeepStrictEqualObject,
{ a: '2' },
'Should resolve to not deep strictly equal the object'
);
})();

0 comments on commit d224bbd

Please sign in to comment.