Skip to content

Commit

Permalink
Replace the test helper class Deferred with polyfilled `Promise.wit…
Browse files Browse the repository at this point in the history
…hResolvers`.
  • Loading branch information
jaydenseric committed Oct 7, 2024
1 parent 1bc03b9 commit 1135035
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- Integrated a new dev dependency [`eslint-plugin-jsdoc`](https://npm.im/eslint-plugin-jsdoc) and revised types.
- Removed the Node.js CLI option `--unhandled-rejections=throw` in the package script `tests` as it’s now the default for all supported Node.js versions.
- Avoid hardcoding a default value in the type `FileUploadCreateReadStreamOptions` property `highWaterMark` description and use the function `getDefaultHighWaterMark` from `node:stream` in tests.
- Replaced the test helper class `Deferred` with polyfilled `Promise.withResolvers`.
- Omit unused catch bindings in the function `processRequest`.
- Corrected the JSDoc type `FileUploadCreateReadStreamOptions` in the module `processRequest.mjs`.
- Avoid using `return` in the middleware.
Expand Down
18 changes: 13 additions & 5 deletions processRequest.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check

import "./test/polyfillFile.mjs";
import "./test/polyfillPromiseWithResolvers.mjs";

import {
deepStrictEqual,
Expand All @@ -20,7 +21,6 @@ import { ReadStream } from "fs-capacitor";

import processRequest from "./processRequest.mjs";
import abortingMultipartRequest from "./test/abortingMultipartRequest.mjs";
import Deferred from "./test/Deferred.mjs";
import Upload from "./Upload.mjs";

describe(
Expand Down Expand Up @@ -794,11 +794,15 @@ describe(
// request part way through, the server request handler must be manually
// awaited or else the test will resolve and the process will exit before
// it’s done.
const done = new Deferred();

/** @type {PromiseWithResolvers<void>} */
const done = Promise.withResolvers();

// The request must be aborted after it has been received by the server
// request handler, or else Node.js won’t run the handler.
const requestReceived = new Deferred();

/** @type {PromiseWithResolvers<void>} */
const requestReceived = Promise.withResolvers();

const server = createServer(async (request, response) => {
try {
Expand Down Expand Up @@ -936,11 +940,15 @@ describe(
// request part way through, the server request handler must be manually
// awaited or else the test will resolve and the process will exit before
// it’s done.
const done = new Deferred();

/** @type {PromiseWithResolvers<void>} */
const done = Promise.withResolvers();

// The request must be aborted after it has been received by the server
// request handler, or else Node.js won’t run the handler.
const requestReceived = new Deferred();

/** @type {PromiseWithResolvers<void>} */
const requestReceived = Promise.withResolvers();

const server = createServer(async (request, response) => {
try {
Expand Down
20 changes: 0 additions & 20 deletions test/Deferred.mjs

This file was deleted.

20 changes: 20 additions & 0 deletions test/polyfillPromiseWithResolvers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @ts-check

// TODO: Delete this polyfill once all supported Node.js versions implement
// `Promise.withResolvers`.
Promise.withResolvers ??= () => new PromiseWithResolversReturn();

/**
* A promise with resolvers.
* @template T
* @implements {PromiseWithResolvers<T>}
*/
class PromiseWithResolversReturn {
constructor() {
/** @type {Promise<T>} */
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}

0 comments on commit 1135035

Please sign in to comment.