From 68300f7cef03dc3284b973a1d0fd63c3f28470f5 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 21 Dec 2023 17:25:56 +0900 Subject: [PATCH] refactor: simple --- test/node-fetch/main.js | 4 ++-- test/node-fetch/utils/chai-timeout.js | 10 +++++++--- test/utils/timeout.js | 18 +----------------- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/test/node-fetch/main.js b/test/node-fetch/main.js index 30e6360b680..380868b1ba4 100644 --- a/test/node-fetch/main.js +++ b/test/node-fetch/main.js @@ -10,7 +10,7 @@ const crypto = require('crypto') const chaiPromised = require('chai-as-promised') const chaiIterator = require('chai-iterator') const chaiString = require('chai-string') -const { delay } = require('../utils/timeout') +const { timeout } = require('../utils/timeout') const { Blob } = require('buffer') const { @@ -761,7 +761,7 @@ describe('node-fetch', () => { it('should collect handled errors on the body stream to reject if the body is used later', () => { const url = `${base}invalid-content-encoding` - return fetch(url).then(delay(20)).then(res => { + return fetch(url).then(timeout(20, { rejection: false })).then(res => { expect(res.headers.get('content-type')).to.equal('text/plain') return expect(res.text()).to.eventually.be.rejected }) diff --git a/test/node-fetch/utils/chai-timeout.js b/test/node-fetch/utils/chai-timeout.js index bb76c80d93f..6e909f5d830 100644 --- a/test/node-fetch/utils/chai-timeout.js +++ b/test/node-fetch/utils/chai-timeout.js @@ -1,11 +1,15 @@ -const { pTimeout } = require('../../utils/timeout') +const { timeout } = require('../../utils/timeout') module.exports = ({ Assertion }, utils) => { utils.addProperty(Assertion.prototype, 'timeout', async function () { let timeouted = false - await pTimeout(this._obj, 150, () => { - timeouted = true + await timeout(150, { + promise: this._obj, + fallback: () => { + timeouted = true + } }) + return this.assert( timeouted, 'expected promise to timeout but it was resolved', diff --git a/test/utils/timeout.js b/test/utils/timeout.js index 877272c22b9..4e9beaabd8c 100644 --- a/test/utils/timeout.js +++ b/test/utils/timeout.js @@ -44,22 +44,6 @@ async function timeout (time, options = {}) { } } -/** - * @type {(promise: Promise, time: number, fallback?: () => R | Promise) => Promise | T>} - */ -function pTimeout (promise, time, fallback) { - return timeout(time, { promise, fallback }) -} - -/** - * @param {number} time - */ -function delay (time) { - return timeout(time, { rejection: false }) -} - module.exports = { - timeout, - delay, - pTimeout + timeout }