add basic unit test

This commit is contained in:
Brent Schroeter 2026-02-02 07:57:37 +00:00
parent f74684396b
commit a5c1abbbed
6 changed files with 4544 additions and 1 deletions

11
jest.config.js Normal file
View file

@ -0,0 +1,11 @@
const { createDefaultPreset } = require("ts-jest");
const tsJestTransformCfg = createDefaultPreset().transform;
/** @type {import("jest").Config} **/
module.exports = {
testEnvironment: "node",
transform: {
...tsJestTransformCfg,
},
};

View file

@ -3,3 +3,6 @@ node = "24"
[tasks.build]
run = "npx rollup -c"
[tasks.test]
run = "npx jest"

4502
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -19,7 +19,10 @@
"devDependencies": {
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.3.0",
"@types/jest": "^30.0.0",
"jest": "^30.2.0",
"rollup": "^4.57.1",
"ts-jest": "^29.4.6",
"tslib": "^2.8.1",
"typescript": "^5.9.3"
}

24
src/index.test.ts Normal file
View file

@ -0,0 +1,24 @@
import { AsyncTaskQueue } from "./index";
test("example evaluates correctly", async () => {
const q = new AsyncTaskQueue<number>(
async (nums: number[]) => {/* no-op */},
)
// Handler will receive up to 4 items per batch.
.batchSize(4)
// Handler will be called at most once per 100 milliseconds.
.throttleMs(100);
// Add items to the queue.
for (let n = 0; n < 1000; n++) {
q.push(n);
}
await new Promise((resolve) => setTimeout(resolve, 4000));
expect(q.size()).toBeLessThan(900);
expect(q.size()).toBeGreaterThan(800);
q.drain();
expect(q.size()).toEqual(0);
});

View file

@ -102,7 +102,7 @@ export class AsyncTaskQueue<T> {
: Promise.resolve();
const items = this._queue.splice(0, this._batchSize);
await this._handler(items).catch(this._error_handler);
await this._handler(items).catch(this._errorHandler);
await throttleTimeout;
}