Skip to content

Commit

Permalink
chore: switch to node test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Jun 17, 2024
1 parent 323f598 commit 629b3df
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 48 deletions.
30 changes: 30 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Build watcher",
"group": "build",
"type": "shell",
"command": "npm run build:watch",
"runOptions": {
"runOn": "folderOpen"
}
},
{
"label": "Test watcher",
"group": "build",
"type": "shell",
"command": "npm run test:watch",
"windows": {
"command": "npm run test:watch"
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"runOptions": {
"runOn": "folderOpen"
}
}
]
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
},
"scripts": {
"build": "./scripts/build.sh",
"build:watch": "tsc --watch -p tsconfig.esm.json",
"bundlewatch": "npm run pretest:browser && bundlewatch --config bundlewatch.config.json",
"docs:diff": "npm run docs && git diff --quiet README.md",
"docs": "npm run build && npx runmd --output=README.md README_js.md",
Expand All @@ -115,7 +116,8 @@
"test:matching": "BABEL_ENV=commonjsNode node --throw-deprecation node_modules/.bin/jest test/unit/ -t",
"test:node": "npm-run-all --parallel examples:node:**",
"test:pack": "./scripts/testpack.sh",
"test": "BABEL_ENV=commonjsNode node --throw-deprecation node_modules/.bin/jest dist/esm/test/"
"test:watch": "node --test --watch dist/esm/test",
"test": "BABEL_ENV=commonjsNode node --throw-deprecation node_modules/.bin/jest dist/esm/test"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/test/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import * as gen from 'random-seed';
import parse from '../parse.js';
import stringify from '../stringify.js';
Expand Down
2 changes: 1 addition & 1 deletion src/test/rng.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import rng from '../rng.js';

describe('rng', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/stringify.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import stringify, { unsafeStringify } from '../stringify.js';

const BYTES = Uint8Array.of(
Expand Down
2 changes: 1 addition & 1 deletion src/test/v1-random.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import v1 from '../v1.js';

// Since the clockseq is cached in the module this test must run in a separate file in order to
Expand Down
2 changes: 1 addition & 1 deletion src/test/v1-rng.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import v1 from '../v1.js';

// Since the clockseq is cached in the module this test must run in a separate file in order to
Expand Down
37 changes: 30 additions & 7 deletions src/test/v1.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import v1 from '../v1.js';

// Verify ordering of v1 ids created with explicit times
Expand Down Expand Up @@ -39,7 +39,10 @@ describe('v1', () => {

test('msec', () => {
// eslint-disable-next-line no-self-compare
assert(v1({ msecs: TIME }) !== v1({ msecs: TIME }), 'IDs created at same msec are different');
assert.ok(
v1({ msecs: TIME }) !== v1({ msecs: TIME }),
'IDs created at same msec are different'
);
});

test('exception thrown when > 10k ids created in 1ms', () => {
Expand All @@ -52,7 +55,7 @@ describe('v1', () => {
// Verify clock regression bumps clockseq
const uidt = v1({ msecs: TIME });
const uidtb = v1({ msecs: TIME - 1 });
assert(
assert.ok(
parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
'Clock regression by msec increments the clockseq'
);
Expand All @@ -62,7 +65,7 @@ describe('v1', () => {
// Verify clock regression bumps clockseq
const uidtn = v1({ msecs: TIME, nsecs: 10 });
const uidtnb = v1({ msecs: TIME, nsecs: 9 });
assert(
assert.ok(
parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
'Clock regression by nsec increments the clockseq'
);
Expand All @@ -78,7 +81,10 @@ describe('v1', () => {
test('explicit options produce expected id', () => {
// Verify explicit options produce expected id
const id = v1(fullOptions);
assert(id === 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
assert.ok(
id === 'd9428888-122b-11e1-b85c-61cd3cbb3210',
'Explicit options produce expected id'
);
});

test('ids spanning 1ms boundary are 100ns apart', () => {
Expand All @@ -89,10 +95,27 @@ describe('v1', () => {
const before = u0.split('-')[0];
const after = u1.split('-')[0];
const dt = parseInt(after, 16) - parseInt(before, 16);
assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
assert.ok(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
});

const expectedBytes = [217, 66, 136, 136, 18, 43, 17, 225, 184, 92, 97, 205, 60, 187, 50, 16];
const expectedBytes = Uint8Array.of(
217,
66,
136,
136,
18,
43,
17,
225,
184,
92,
97,
205,
60,
187,
50,
16
);

test('fills one UUID into a buffer as expected', () => {
const buffer = new Uint8Array(16);
Expand Down
24 changes: 12 additions & 12 deletions src/test/v35.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import md5Browser from '../md5-browser.js';
import md5 from '../md5.js';
import sha1Browser from '../sha1-browser.js';
Expand Down Expand Up @@ -43,27 +43,27 @@ describe('v35', () => {
return chars.join('');
}

test('sha1 node', () => {
HASH_SAMPLES.forEach(function (sample) {
HASH_SAMPLES.forEach(function (sample, i) {
test(`sha1(node) HASH_SAMPLES[${i}]`, () => {
assert.equal(hashToHex(sha1(sample.input)), sample.sha1);
});
});

test('sha1 browser', () => {
HASH_SAMPLES.forEach(function (sample) {
HASH_SAMPLES.forEach(function (sample, i) {
test('sha1(browser) HASH_SAMPLES[${i}]', () => {
assert.equal(hashToHex(sha1Browser(sample.input)), sample.sha1);
});
});

test('md5 node', () => {
HASH_SAMPLES.forEach(function (sample) {
HASH_SAMPLES.forEach(function (sample, i) {
test('md5(node) HASH_SAMPLES[${i}]', () => {
assert.equal(hashToHex(md5(sample.input)), sample.md5);
});
});

test('md5 browser', () => {
HASH_SAMPLES.forEach(function (sample) {
assert.equal(hashToHex(md5Browser(sample.input)), sample.md5);
HASH_SAMPLES.forEach(function (sample, i) {
test(`md5(browser) (HASH_SAMPLES[${i}])`, () => {
assert.equal(hashToHex(md5Browser(sample.input) as unknown as Uint8Array), sample.md5);
});
});

Expand Down Expand Up @@ -151,7 +151,7 @@ describe('v35', () => {
assert.strictEqual(result, buf);

// test offsets as well
buf = new Uint8Array(19);
buf = new Uint8Array(19).fill(0xaa);

const expectedBuf = new Uint8Array(19).fill(0xaa);
expectedBuf.set(expectedUuid, 3);
Expand Down Expand Up @@ -256,7 +256,7 @@ describe('v35', () => {
assert.strictEqual(result, buf);

// test offsets as well
buf = new Uint8Array(19);
buf = new Uint8Array(19).fill(0xaa);

const expectedBuf = new Uint8Array(19).fill(0xaa);
expectedBuf.set(expectedUuid, 3);
Expand Down
8 changes: 4 additions & 4 deletions src/test/v4.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import v4 from '../v4.js';

const randomBytesFixture = Uint8Array.of(
Expand Down Expand Up @@ -44,8 +44,8 @@ describe('v4', () => {
test('subsequent UUIDs are different', () => {
const id1 = v4();
const id2 = v4();
console.log('FJAFDSJKLFDSJA', id1, id2);
assert(id1 !== id2);

assert.ok(id1 !== id2);
});

test('explicit options.random produces expected result', () => {
Expand All @@ -67,7 +67,7 @@ describe('v4', () => {
});

test('fills two UUIDs into a buffer as expected', () => {
const buffer = new Uint8Array(16);
const buffer = new Uint8Array(32);
v4({ random: randomBytesFixture }, buffer, 0);
v4({ random: randomBytesFixture }, buffer, 16);

Expand Down
4 changes: 2 additions & 2 deletions src/test/v6.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import v1ToV6 from '../v1ToV6.js';
import v6 from '../v6.js';
import v6ToV1 from '../v6ToV1.js';
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('v6', () => {
test('default behavior', () => {
// Verify explicit options produce expected id
const id = v6();
assert(
assert.ok(
/[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/.test(id),

'id is valid v6 UUID'
Expand Down
13 changes: 7 additions & 6 deletions src/test/v7.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import v7 from '../v7.js';

/**
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('v7', () => {
test('subsequent UUIDs are different', () => {
const id1 = v7();
const id2 = v7();
assert(id1 !== id2);
assert.ok(id1 !== id2);
});

test('explicit options.random and options.msecs produces expected result', () => {
Expand Down Expand Up @@ -111,7 +111,8 @@ describe('v7', () => {
});

test('fills two UUIDs into a buffer as expected', () => {
const buffer = new Uint8Array(16);
const buffer = new Uint8Array(32);

v7(
{
random: randomBytesFixture,
Expand Down Expand Up @@ -154,7 +155,7 @@ describe('v7', () => {
id = v7({ msecs });

if (prior !== undefined) {
assert(prior < id, `${prior} < ${id}`);
assert.ok(prior < id, `${prior} < ${id}`);
}

prior = id;
Expand All @@ -172,7 +173,7 @@ describe('v7', () => {

const c = v7({ msecs });

assert(a < c, `${a} < ${c}`);
assert.ok(a < c, `${a} < ${c}`);
});

test('can supply seq', () => {
Expand Down Expand Up @@ -203,6 +204,6 @@ describe('v7', () => {
msecs: msecsFixture + 1,
});

assert(uuid.indexOf('fff') !== 15);
assert.ok(uuid.indexOf('fff') !== 15);
});
});
2 changes: 1 addition & 1 deletion src/test/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import validate from '../validate.js';
import { TESTS } from './test_constants.js';

Expand Down
6 changes: 3 additions & 3 deletions src/test/version.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { describe } from 'node:test';
import test, { describe } from 'node:test';
import version from '../version.js';
import { TESTS } from './test_constants.js';

Expand All @@ -10,10 +10,10 @@ describe('version() tests', () => {
// @ts-expect-error
const actualVersion = version(value);

assert(expectedValidate, `version(${value}) should throw`);
assert.ok(expectedValidate, `version(${value}) should throw`);
assert.strictEqual(actualVersion, expectedVersion);
} catch (err) {
assert(!expectedValidate, `version(${value}) threw unexpectedly`);
assert.ok(!expectedValidate, `version(${value}) threw unexpectedly`);
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/uuid-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ switch (version) {
const name = args.shift();
let namespace = args.shift();

assert(name != null, 'v3 name not specified');
assert(namespace != null, 'v3 namespace not specified');
assert.ok(name != null, 'v3 name not specified');
assert.ok(namespace != null, 'v3 namespace not specified');

if (namespace === 'URL') {
namespace = URL;
Expand All @@ -64,8 +64,8 @@ switch (version) {
const name = args.shift();
let namespace = args.shift();

assert(name != null, 'v5 name not specified');
assert(namespace != null, 'v5 namespace not specified');
assert.ok(name != null, 'v5 name not specified');
assert.ok(namespace != null, 'v5 namespace not specified');

if (namespace === 'URL') {
namespace = URL;
Expand Down
3 changes: 0 additions & 3 deletions src/v35.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ export default function v35(
) {
const valueBytes: Uint8Array = typeof value === 'string' ? stringToBytes(value) : value;
const namespaceBytes: Uint8Array = typeof namespace === 'string' ? parse(namespace) : namespace;
if (typeof value === 'string') {
value = stringToBytes(value);
}

if (typeof namespace === 'string') {
namespace = parse(namespace);
Expand Down

0 comments on commit 629b3df

Please sign in to comment.