Skip to content

Commit

Permalink
feat(strict): use standard assert for Node.js legacy versions (#761)
Browse files Browse the repository at this point in the history
* feat(strict): use `assert` for Node.js legacy versions

* ci: add tests

* ci: improve imports

* ci: bun is always strict

* docs: add `strict` legacy behavior
  • Loading branch information
wellwelwel authored Sep 11, 2024
1 parent 130dadb commit d4b7b64
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 81 deletions.
8 changes: 7 additions & 1 deletion src/modules/essentials/strict.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import nodeAssert from 'node:assert/strict';
import { createAssert } from '../../builders/assert.js';
import { nodeVersion } from '../../parsers/get-runtime.js';

/* c8 ignore next 4 */ // Platform version
const nodeAssert =
!nodeVersion || nodeVersion >= 16
? require('node:assert/strict')
: require('node:assert');

export const strict = createAssert(nodeAssert);
11 changes: 4 additions & 7 deletions test/e2e/before-and-after-each.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { nodeVersion } from '../../src/parsers/get-runtime.js';
import { skip } from '../../src/modules/helpers/skip.js';

if (nodeVersion && nodeVersion < 16) {
skip();
}

import { test } from '../../src/modules/helpers/test.js';
import { describe } from '../../src/modules/helpers/describe.js';
import { it } from '../../src/modules/helpers/it/core.js';
import { poku } from '../../src/modules/essentials/poku.js';
import { assert } from '../../src/modules/essentials/assert.js';
import { getRuntime } from '../../src/parsers/get-runtime.js';
import { skip } from '../../src/modules/helpers/skip.js';

if (getRuntime() === 'deno') skip();

test(async () => {
const prepareService = () => new Promise((resolve) => resolve(undefined));
Expand Down
11 changes: 4 additions & 7 deletions test/e2e/each-api-order.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { getRuntime, nodeVersion } from '../../src/parsers/get-runtime.js';
import { skip } from '../../src/modules/helpers/skip.js';

if (nodeVersion && nodeVersion < 16) {
skip();
}

import { getRuntime } from '../../src/parsers/get-runtime.js';
import { describe } from '../../src/modules/helpers/describe.js';
import { it } from '../../src/modules/helpers/it/core.js';
import { assert } from '../../src/modules/essentials/assert.js';
import { ext, inspectPoku } from '../__utils__/capture-cli.test.js';
import { skip } from '../../src/modules/helpers/skip.js';

if (getRuntime() === 'deno') skip();

const runtime = getRuntime();
const offset = runtime === 'bun' ? 37 : 33;
Expand Down
6 changes: 2 additions & 4 deletions test/e2e/fail-fast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { it } from '../../src/modules/helpers/it/core.js';
import { assert } from '../../src/modules/essentials/assert.js';
import { inspectPoku, isBuild } from '../__utils__/capture-cli.test.js';
import { skip } from '../../src/modules/helpers/skip.js';
import { nodeVersion } from '../../src/parsers/get-runtime.js';
import { getRuntime } from '../../src/parsers/get-runtime.js';

if (isBuild || (nodeVersion && nodeVersion < 16)) {
skip();
}
if (isBuild || getRuntime() === 'deno') skip();

describe('Fast Fast', async () => {
await it('Parallel / Concurrent', async () => {
Expand Down
11 changes: 4 additions & 7 deletions test/e2e/failure.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { getRuntime, nodeVersion } from '../../src/parsers/get-runtime.js';
import { skip } from '../../src/modules/helpers/skip.js';

if (nodeVersion && nodeVersion < 16) {
skip();
}

import { getRuntime } from '../../src/parsers/get-runtime.js';
import { describe } from '../../src/modules/helpers/describe.js';
import { it } from '../../src/modules/helpers/it/core.js';
import { assert } from '../../src/modules/essentials/assert.js';
import { inspectPoku, isBuild } from '../__utils__/capture-cli.test.js';
import { skip } from '../../src/modules/helpers/skip.js';

if (getRuntime() === 'deno') skip();

describe('Failure', async () => {
await it('Sequential', async () => {
Expand Down
4 changes: 4 additions & 0 deletions test/integration/before-and-after-each/invalid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { test } from '../../../src/modules/helpers/test.js';
import { poku } from '../../../src/modules/essentials/poku.js';
import { ext } from '../../__utils__/capture-cli.test.js';
import { assert } from '../../../src/modules/essentials/assert.js';
import { getRuntime } from '../../../src/parsers/get-runtime.js';
import { skip } from '../../../src/modules/helpers/skip.js';

if (getRuntime() === 'deno') skip();

test('Before and After Each: updating an external file', async () => {
const prepareService = true;
Expand Down
69 changes: 35 additions & 34 deletions test/integration/import.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import { test } from '../../src/modules/helpers/test.js';
import { getRuntime } from '../../src/parsers/get-runtime.js';
import { skip } from '../../src/modules/helpers/skip.js';
import { nodeVersion } from '../../src/parsers/get-runtime.js';

if (nodeVersion && nodeVersion < 16) {
skip('Strict method is available from Node.js 16');
}
if (getRuntime() === 'deno') skip();

import * as index from '../../src/modules/index.js';
test(async () => {
const index = await import('../../src/modules/index.js');

index.test('Import Suite', () => {
index.assert.ok(index.poku, 'Importing poku method');
index.assert.ok(index.assert, 'Importing assert method');
index.assert.ok(index.strict, 'Importing strict method');
index.test('Import Suite', () => {
index.assert.ok(index.poku, 'Importing poku method');
index.assert.ok(index.assert, 'Importing assert method');
index.assert.ok(index.strict, 'Importing strict method');

index.assert.ok(index.defineConfig, 'Importing defineConfig method');
index.assert.ok(index.envFile, 'Importing envFile 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');
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.skip, 'Importing skip helper');
index.assert.ok(index.sleep, 'Importing sleep helper');
index.assert.ok(
index.waitForExpectedResult,
'Importing waitForExpectedResult helper'
);
index.assert.ok(index.waitForPort, 'Importing waitForPort helper');
index.assert.ok(index.exit, 'Importing exit helper');
index.assert.ok(index.listFiles, 'Importing listFiles helper');
index.assert.ok(
typeof index.version === 'string',
'Importing listFiles helper'
);
index.assert.ok(index.defineConfig, 'Importing defineConfig method');
index.assert.ok(index.envFile, 'Importing envFile 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');
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.skip, 'Importing skip helper');
index.assert.ok(index.sleep, 'Importing sleep helper');
index.assert.ok(
index.waitForExpectedResult,
'Importing waitForExpectedResult helper'
);
index.assert.ok(index.waitForPort, 'Importing waitForPort helper');
index.assert.ok(index.exit, 'Importing exit helper');
index.assert.ok(index.listFiles, 'Importing listFiles helper');
index.assert.ok(
typeof index.version === 'string',
'Importing listFiles helper'
);
});
});
11 changes: 8 additions & 3 deletions test/integration/strict/assert-no-message.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { skip } from '../../../src/modules/helpers/skip.js';
import { nodeVersion } from '../../../src/parsers/get-runtime.js';
import { getRuntime, nodeVersion } from '../../../src/parsers/get-runtime.js';

if (nodeVersion && nodeVersion < 16) {
skip('Strict method is available from Node.js 16');
}

if (getRuntime() === 'deno') skip();

import { describe } from '../../../src/modules/helpers/describe.js';
import { it } from '../../../src/modules/helpers/it/core.js';
import { strict as assert } from '../../../src/modules/essentials/strict.js';

describe('Strict Suite (No Message)', () => {
describe('Strict Suite (No Message)', async () => {
const { strict: assert } = await import(
'../../../src/modules/essentials/strict.js'
);

it(() => {
assert(true);
assert(1);
Expand Down
14 changes: 8 additions & 6 deletions test/integration/strict/assert.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { describe } from '../../../src/modules/helpers/describe.js';
import { it } from '../../../src/modules/helpers/it/core.js';
import { getRuntime, nodeVersion } from '../../../src/parsers/get-runtime.js';
import { skip } from '../../../src/modules/helpers/skip.js';
import { nodeVersion } from '../../../src/parsers/get-runtime.js';

if (nodeVersion && nodeVersion < 16) {
if ((nodeVersion && nodeVersion < 16) || getRuntime() === 'deno') {
skip('Strict method is available from Node.js 16');
}

import { describe } from '../../../src/modules/helpers/describe.js';
import { it } from '../../../src/modules/helpers/it/core.js';
import { strict as assert } from '../../../src/modules/essentials/strict.js';

describe('Strict Suite', async () => {
const { strict: assert } = await import(
'../../../src/modules/essentials/strict.js'
);

it(() => {
assert(true, 'ok (default) with true');
assert(1, 'ok (default) with 1');
Expand Down
20 changes: 20 additions & 0 deletions test/integration/strict/ensure-strict.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { skip } from '../../../src/modules/helpers/skip.js';
import { getRuntime, nodeVersion } from '../../../src/parsers/get-runtime.js';
import { describe } from '../../../src/modules/helpers/describe.js';
import { assert } from '../../../src/modules/essentials/assert.js';

if ((nodeVersion && nodeVersion < 16) || getRuntime() !== 'node') {
skip('Strict method is available from Node.js 16');
}

describe('Ensure strict', async () => {
const { strict } = await import('../../../src/modules/essentials/strict.js');

const actual = Object.create(null);
const expected = { name: 'John' };

actual.name = 'John';

assert.deepEqual(actual, expected);
strict.notDeepEqual(actual, expected);
});
23 changes: 12 additions & 11 deletions test/unit/define-configs.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { nodeVersion } from '../../src/parsers/get-runtime.js';
import { test } from '../../src/modules/helpers/test.js';
import { getRuntime } from '../../src/parsers/get-runtime.js';
import { skip } from '../../src/modules/helpers/skip.js';

if (nodeVersion && nodeVersion < 16) {
skip();
}
if (getRuntime() === 'deno') skip();

import { assert } from '../../src/modules/essentials/assert.js';
import { defineConfig } from '../../src/modules/index.js';
test(async () => {
const { assert } = await import('../../src/modules/essentials/assert.js');
const { defineConfig } = await import('../../src/modules/index.js');

assert.deepStrictEqual(
defineConfig({ debug: true }),
{ debug: true },
'Reflect configs'
);
assert.deepStrictEqual(
defineConfig({ debug: true }),
{ debug: true },
'Reflect configs'
);
});
2 changes: 1 addition & 1 deletion website/docs/documentation/assert/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Strict assertions:
+ import { strict as assert } from 'poku';
```

- `strict` method is available for **Node.js 16** onwards, **Bun**, and **Deno**.
- `strict` method is available for **Node.js 16** onwards, **Bun**, and **Deno**. If you use it on unsupported **Node.js** versions, **Poku** will use the standard `assert` instead.

```ts
assert(true, "It's true 🧪");
Expand Down

0 comments on commit d4b7b64

Please sign in to comment.