Skip to content

Commit

Permalink
Migrate to node native tests
Browse files Browse the repository at this point in the history
- `test.assert` -> `assert`
- `test.strictSame` -> `assert.strictEqual` / `assert.deepStrictEqual`
- `test.error` -> `assert.ifError`
- `test.throws` -> `assert.throws`
- Remove `test.end`
- Remove `test.plan`

Refs: #261
  • Loading branch information
leonid-shutov authored Dec 17, 2024
1 parent d90f5ea commit d2368a4
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 399 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"url": "https://www.patreon.com/tshemsedinov"
},
"scripts": {
"test": "npm run lint && npm run types && node --test",
"test": "npm run lint && npm run types && node --test --test-reporter=tap",
"types": "tsc -p tsconfig.json",
"lint": "eslint . && prettier --check \"**/*.js\" \"**/*.json\" \"**/*.md\" \"**/*.ts\"",
"fix": "eslint . --fix && prettier --write \"**/*.js\" \"**/*.json\" \"**/*.md\" \"**/*.ts\""
Expand Down
47 changes: 22 additions & 25 deletions test/async.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,69 @@
'use strict';

const metatests = require('metatests');
const test = require('node:test');
const assert = require('node:assert');
const { toBool, timeout, delay, timeoutify } = require('..');

metatests.test('Async: toBool', async (test) => {
test('Async: toBool', async () => {
const success = await Promise.resolve('success').then(...toBool);
test.strictSame(success, true);
assert.strictEqual(success, true);
const rejected = await Promise.reject(new Error('Ups')).then(...toBool);
test.strictSame(rejected, false);
test.end();
assert.strictEqual(rejected, false);
});

metatests.test('Async: Abortable timeout', async (test) => {
test('Async: Abortable timeout', async () => {
try {
await timeout(10);
test.error(new Error('Should not be executed'));
assert.ifError(new Error('Should not be executed'));
} catch (err) {
test.strictSame(err.code, 'ETIMEOUT');
test.strictSame(err.message, 'Timeout of 10ms reached');
assert.strictEqual(err.code, 'ETIMEOUT');
assert.strictEqual(err.message, 'Timeout of 10ms reached');
}
const ac = new AbortController();
setTimeout(() => {
ac.abort();
}, 10);
try {
await timeout(100, ac.signal);
test.error(new Error('Should not be executed'));
assert.ifError(new Error('Should not be executed'));
} catch (err) {
test.strictSame(err.message, 'Timeout aborted');
test.end();
assert.strictEqual(err.message, 'Timeout aborted');
}
});

metatests.test('Async: Abortable delay', async (test) => {
test('Async: Abortable delay', async () => {
try {
const res = await delay(10);
test.strictSame(res, undefined);
assert.strictEqual(res, undefined);
} catch {
test.error(new Error('Should not be executed'));
assert.ifError(new Error('Should not be executed'));
}
const ac = new AbortController();
setTimeout(() => {
ac.abort();
}, 10);
try {
await delay(100, ac.signal);
test.error(new Error('Should not be executed'));
assert.ifError(new Error('Should not be executed'));
} catch (err) {
test.strictSame(err.message, 'Delay aborted');
test.end();
assert.strictEqual(err.message, 'Delay aborted');
}
});

metatests.test('Async: timeoutify', async (test) => {
test('Async: timeoutify', async () => {
try {
const request = delay(1000);
await timeoutify(request, 10);
test.error(new Error('Should not be executed'));
assert.ifError(new Error('Should not be executed'));
} catch (err) {
test.strictSame(err.code, 'ETIMEOUT');
test.strictSame(err.message, 'Timeout of 10ms reached');
assert.strictEqual(err.code, 'ETIMEOUT');
assert.strictEqual(err.message, 'Timeout of 10ms reached');
}
try {
const request = delay(10);
const response = await timeoutify(request, 1000);
test.strictSame(response, undefined);
test.end();
assert.strictEqual(response, undefined);
} catch {
test.error(new Error('Should not be executed'));
assert.ifError(new Error('Should not be executed'));
}
});
Loading

0 comments on commit d2368a4

Please sign in to comment.