From b09d21b06bea5d8130d7f26c389c6425e1e1639b Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 13 Feb 2021 07:32:58 -0800 Subject: [PATCH] test: enable no-restricted-syntax rule for test-timers-promisified PR-URL: https://github.com/nodejs/node/pull/37357 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Darshan Sen --- test/parallel/test-timers-promisified.js | 90 +++++++++++++----------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/test/parallel/test-timers-promisified.js b/test/parallel/test-timers-promisified.js index 3e68470c7dae97..b802f655f08db0 100644 --- a/test/parallel/test-timers-promisified.js +++ b/test/parallel/test-timers-promisified.js @@ -11,41 +11,39 @@ const { NodeEventTarget } = require('internal/event_target'); const timerPromises = require('timers/promises'); -/* eslint-disable no-restricted-syntax */ - -const setTimeout = promisify(timers.setTimeout); -const setImmediate = promisify(timers.setImmediate); +const setPromiseTimeout = promisify(timers.setTimeout); +const setPromiseImmediate = promisify(timers.setImmediate); const exec = promisify(child_process.exec); -assert.strictEqual(setTimeout, timerPromises.setTimeout); -assert.strictEqual(setImmediate, timerPromises.setImmediate); +assert.strictEqual(setPromiseTimeout, timerPromises.setTimeout); +assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate); const { setInterval } = timerPromises; process.on('multipleResolves', common.mustNotCall()); { - const promise = setTimeout(1); + const promise = setPromiseTimeout(1); promise.then(common.mustCall((value) => { assert.strictEqual(value, undefined); })); } { - const promise = setTimeout(1, 'foobar'); + const promise = setPromiseTimeout(1, 'foobar'); promise.then(common.mustCall((value) => { assert.strictEqual(value, 'foobar'); })); } { - const promise = setImmediate(); + const promise = setPromiseImmediate(); promise.then(common.mustCall((value) => { assert.strictEqual(value, undefined); })); } { - const promise = setImmediate('foobar'); + const promise = setPromiseImmediate('foobar'); promise.then(common.mustCall((value) => { assert.strictEqual(value, 'foobar'); })); @@ -94,7 +92,7 @@ process.on('multipleResolves', common.mustNotCall()); { const ac = new AbortController(); const signal = ac.signal; - assert.rejects(setTimeout(10, undefined, { signal }), /AbortError/) + assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/) .then(common.mustCall()); ac.abort(); } @@ -103,14 +101,14 @@ process.on('multipleResolves', common.mustNotCall()); const ac = new AbortController(); const signal = ac.signal; ac.abort(); // Abort in advance - assert.rejects(setTimeout(10, undefined, { signal }), /AbortError/) + assert.rejects(setPromiseTimeout(10, undefined, { signal }), /AbortError/) .then(common.mustCall()); } { const ac = new AbortController(); const signal = ac.signal; - assert.rejects(setImmediate(10, { signal }), /AbortError/) + assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/) .then(common.mustCall()); ac.abort(); } @@ -119,7 +117,7 @@ process.on('multipleResolves', common.mustNotCall()); const ac = new AbortController(); const signal = ac.signal; ac.abort(); // Abort in advance - assert.rejects(setImmediate(10, { signal }), /AbortError/) + assert.rejects(setPromiseImmediate(10, { signal }), /AbortError/) .then(common.mustCall()); } @@ -167,18 +165,17 @@ process.on('multipleResolves', common.mustNotCall()); // Check that aborting after resolve will not reject. const ac = new AbortController(); const signal = ac.signal; - assert.doesNotReject(setTimeout(10, undefined, { signal }) - .then(common.mustCall(() => { - ac.abort(); - }))).then(common.mustCall()); + setPromiseTimeout(10, undefined, { signal }) + .then(common.mustCall(() => { ac.abort(); })) + .then(common.mustCall()); } { // Check that aborting after resolve will not reject. const ac = new AbortController(); const signal = ac.signal; - assert.doesNotReject(setImmediate(10, { signal }).then(common.mustCall(() => { - ac.abort(); - }))).then(common.mustCall()); + setPromiseImmediate(10, { signal }) + .then(common.mustCall(() => { ac.abort(); })) + .then(common.mustCall()); } { @@ -205,7 +202,7 @@ process.on('multipleResolves', common.mustNotCall()); // Check that timer adding signals does not leak handlers const signal = new NodeEventTarget(); signal.aborted = false; - setTimeout(0, null, { signal }).finally(common.mustCall(() => { + setPromiseTimeout(0, null, { signal }).finally(common.mustCall(() => { assert.strictEqual(signal.listenerCount('abort'), 0); })); } @@ -214,7 +211,7 @@ process.on('multipleResolves', common.mustNotCall()); // Check that timer adding signals does not leak handlers const signal = new NodeEventTarget(); signal.aborted = false; - setImmediate(0, { signal }).finally(common.mustCall(() => { + setPromiseImmediate(0, { signal }).finally(common.mustCall(() => { assert.strictEqual(signal.listenerCount('abort'), 0); })); } @@ -258,39 +255,52 @@ process.on('multipleResolves', common.mustNotCall()); { Promise.all( - [1, '', false, Infinity].map((i) => assert.rejects(setImmediate(10, i)), { - code: 'ERR_INVALID_ARG_TYPE' - })).then(common.mustCall()); + [1, '', false, Infinity].map( + (i) => assert.rejects(setPromiseImmediate(10, i), { + code: 'ERR_INVALID_ARG_TYPE' + }) + ) + ).then(common.mustCall()); Promise.all( [1, '', false, Infinity, null, {}].map( - (signal) => assert.rejects(setImmediate(10, { signal })), { + (signal) => assert.rejects(setPromiseImmediate(10, { signal }), { code: 'ERR_INVALID_ARG_TYPE' - })).then(common.mustCall()); + }) + ) + ).then(common.mustCall()); Promise.all( [1, '', Infinity, null, {}].map( - (ref) => assert.rejects(setImmediate(10, { ref })), { + (ref) => assert.rejects(setPromiseImmediate(10, { ref }), { code: 'ERR_INVALID_ARG_TYPE' - })).then(common.mustCall()); + }) + ) + ).then(common.mustCall()); Promise.all( [1, '', false, Infinity].map( - (i) => assert.rejects(setTimeout(10, null, i)), { + (i) => assert.rejects(setPromiseTimeout(10, null, i), { code: 'ERR_INVALID_ARG_TYPE' - })).then(common.mustCall()); + }) + ) + ).then(common.mustCall()); Promise.all( [1, '', false, Infinity, null, {}].map( - (signal) => assert.rejects(setTimeout(10, null, { signal })), { + (signal) => assert.rejects(setPromiseTimeout(10, null, { signal }), { code: 'ERR_INVALID_ARG_TYPE' - })).then(common.mustCall()); + }) + ) + ).then(common.mustCall()); Promise.all( [1, '', Infinity, null, {}].map( - (ref) => assert.rejects(setTimeout(10, null, { ref })), { + (ref) => assert.rejects(setPromiseTimeout(10, null, { ref }), { code: 'ERR_INVALID_ARG_TYPE' - })).then(common.mustCall()); + }) + ) + ).then(common.mustCall()); } { @@ -357,7 +367,7 @@ process.on('multipleResolves', common.mustNotCall()); const delay = 10; let totalIterations = 0; const timeoutLoop = runInterval(async (iterationNumber) => { - await setTimeout(delay * 4); + await setPromiseTimeout(delay * 4); if (iterationNumber <= 2) { assert.strictEqual(signal.aborted, false); } @@ -382,8 +392,8 @@ process.on('multipleResolves', common.mustNotCall()); // Check that the timing is correct let pre = false; let post = false; - setTimeout(1).then(() => pre = true); - const iterable = setInterval(2); + setPromiseTimeout(1).then(() => pre = true); + const iterable = setInterval(() => {}, 2); const iterator = iterable[Symbol.asyncIterator](); iterator.next().then(common.mustCall(() => { @@ -395,5 +405,5 @@ process.on('multipleResolves', common.mustNotCall()); return iterator.return(); })); - setTimeout(3).then(() => post = true); + setPromiseTimeout(3).then(() => post = true); }