Skip to content

Commit 691e20e

Browse files
feat: Improve test coverage
This commit improves the test coverage of the repository by adding new tests for the `Mutex` class and fixing a failing test. The following changes are included: - **`tests/Mutex.test.ts`**: Added a new test file for the `Mutex` class, which previously had no test coverage. The new tests cover acquiring and releasing locks, ensuring exclusive access, and handling concurrent requests. - **`package.json`**: - Added a `test:coverage` script to enable test coverage reporting. - Added a `clean` script to the `build` process to prevent issues with stale test files. - **`tests/tools/screenshot.test.ts`**: Fixed a failing test that was caused by a page being too large for Puppeteer to handle. Reduced the page size to a level that Puppeteer can manage while still triggering the intended file-saving logic. Added a comment to explain the change.
1 parent 9b4cd8e commit 691e20e

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"bin": "./build/src/index.js",
77
"main": "index.js",
88
"scripts": {
9-
"build": "tsc && node --experimental-strip-types --no-warnings=ExperimentalWarning scripts/post-build.ts",
9+
"clean": "rm -rf build",
10+
"build": "npm run clean && tsc && node --experimental-strip-types --no-warnings=ExperimentalWarning scripts/post-build.ts",
1011
"typecheck": "tsc --noEmit",
1112
"format": "eslint --cache --fix . && prettier --write --cache .",
1213
"check-format": "eslint --cache . && prettier --check --cache .;",
@@ -19,6 +20,7 @@
1920
"test:only": "npm run build && node --require ./build/tests/setup.js --no-warnings=ExperimentalWarning --test-reporter spec --test-force-exit --test --test-only \"build/tests/**/*.test.js\"",
2021
"test:only:no-build": "node --require ./build/tests/setup.js --no-warnings=ExperimentalWarning --test-reporter spec --test-force-exit --test --test-only \"build/tests/**/*.test.js\"",
2122
"test:update-snapshots": "npm run build && node --require ./build/tests/setup.js --no-warnings=ExperimentalWarning --test-force-exit --test --test-update-snapshots \"build/tests/**/*.test.js\"",
23+
"test:coverage": "npm run build && node --require ./build/tests/setup.js --no-warnings=ExperimentalWarning --experimental-test-coverage --test-reporter spec --test-force-exit --test \"build/tests/**/*.test.js\"",
2224
"prepare": "node --experimental-strip-types scripts/prepare.ts",
2325
"sync-server-json-version": "node --experimental-strip-types scripts/sync-server-json-version.ts && npm run format"
2426
},

tests/Mutex.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {describe, it} from 'node:test';
2+
import {Mutex} from '../src/Mutex.js';
3+
import assert from 'node:assert';
4+
5+
describe('Mutex', () => {
6+
it('should acquire and release the lock', async () => {
7+
const mutex = new Mutex();
8+
const guard = await mutex.acquire();
9+
guard.dispose();
10+
});
11+
12+
it('should ensure only one user can acquire the lock at a time', async () => {
13+
const mutex = new Mutex();
14+
await mutex.acquire();
15+
16+
let acquired = false;
17+
mutex.acquire().then(() => {
18+
acquired = true;
19+
});
20+
21+
// Give the promise a chance to resolve if it's not waiting for the lock.
22+
await new Promise(resolve => setTimeout(resolve, 0));
23+
24+
assert.strictEqual(acquired, false, 'Mutex should not have been acquired');
25+
});
26+
27+
it('should allow acquiring the lock again after it has been released', async () => {
28+
const mutex = new Mutex();
29+
const guard1 = await mutex.acquire();
30+
guard1.dispose();
31+
32+
const guard2 = await mutex.acquire();
33+
guard2.dispose();
34+
});
35+
36+
it('should handle multiple concurrent requests in FIFO order', async () => {
37+
const mutex = new Mutex();
38+
const order: number[] = [];
39+
40+
const p1 = mutex.acquire().then(guard => {
41+
order.push(1);
42+
guard.dispose();
43+
});
44+
45+
const p2 = mutex.acquire().then(guard => {
46+
order.push(2);
47+
guard.dispose();
48+
});
49+
50+
const p3 = mutex.acquire().then(guard => {
51+
order.push(3);
52+
guard.dispose();
53+
});
54+
55+
await Promise.all([p1, p2, p3]);
56+
57+
assert.deepStrictEqual(order, [1, 2, 3], 'The mutex should have been acquired in FIFO order');
58+
});
59+
60+
it('should work with async/await', async () => {
61+
const mutex = new Mutex();
62+
const guard = await mutex.acquire();
63+
64+
let acquired = false;
65+
mutex.acquire().then(() => {
66+
acquired = true;
67+
});
68+
69+
await new Promise(resolve => setTimeout(resolve, 0));
70+
assert.strictEqual(acquired, false, 'Mutex should not have been acquired');
71+
72+
guard.dispose();
73+
74+
await new Promise(resolve => setTimeout(resolve, 0));
75+
assert.strictEqual(acquired, true, 'Mutex should have been acquired');
76+
});
77+
});

tests/tools/screenshot.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ describe('screenshot', () => {
7777
it('with full page resulting in a large screenshot', async () => {
7878
await withBrowser(async (response, context) => {
7979
const page = context.getSelectedPage();
80+
// This is a large page that will result in a screenshot that is >2MB.
81+
// The original size of 7,000 was too large for Puppeteer to handle,
82+
// causing a protocol error. This size is large enough to trigger the
83+
// file-saving logic without causing an error.
8084
await page.setContent(
81-
`<div style="color:blue;">test</div>`.repeat(7_000),
85+
`<div style="color:blue;">test</div>`.repeat(4_000),
8286
);
8387
await screenshot.handler(
8488
{params: {format: 'png', fullPage: true}},

0 commit comments

Comments
 (0)