Skip to content

Commit

Permalink
refactor: simple
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Dec 21, 2023
1 parent 67edb0e commit 68300f7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
4 changes: 2 additions & 2 deletions test/node-fetch/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
})
Expand Down
10 changes: 7 additions & 3 deletions test/node-fetch/utils/chai-timeout.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
18 changes: 1 addition & 17 deletions test/utils/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,6 @@ async function timeout (time, options = {}) {
}
}

/**
* @type {<T, R = T>(promise: Promise<T>, time: number, fallback?: () => R | Promise<R>) => Promise<Awaited<R> | 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
}

0 comments on commit 68300f7

Please sign in to comment.