Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to node native tests #272

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading