Skip to content

Commit 3b31fb5

Browse files
🧪 Add unit tests for dep-versions utility and fix CI
🎯 What: Add unit tests to cover the functionality of the `depVersions` module which dynamically computes and resolves the version of dependencies based on the `package.json` file in the current working directory. 📊 Coverage: Added scenarios for handling a missing `package.json`, a `package.json` with no dependencies, successful versions resolutions with alphabetical sorting, skipping of non-resolvable modules without crashing, and deduplication of packages appearing in both `dependencies` and `devDependencies`. ✨ Result: `src/utils/dep-versions.ts` is now covered effectively by using temporary isolated filesystem setups, spying on CWD, and dynamically cache-busting modules using query parameters. This also resolves the recent CI failure. Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent ad87696 commit 3b31fb5

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

‎tests/dep-versions.test.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
1+
import { afterEach, beforeEach, describe, expect, spyOn, test } from 'bun:test';
22
import * as fs from 'fs';
33
import * as os from 'os';
44
import * as path from 'path';
@@ -7,24 +7,36 @@ describe('depVersions utility', () => {
77
const originalCwd = process.cwd();
88
let testDir: string;
99
let testCount = 0;
10+
let cwdSpy: ReturnType<typeof spyOn>;
1011

1112
beforeEach(() => {
1213
testCount++;
13-
testDir = path.join(os.tmpdir(), `temp-test-dep-versions-${Date.now()}-${testCount}`);
14+
testDir = path.join(
15+
os.tmpdir(),
16+
`temp-test-dep-versions-${Date.now()}-${testCount}`,
17+
);
1418
fs.mkdirSync(testDir, { recursive: true });
15-
process.chdir(testDir);
19+
20+
// Mock process.cwd() instead of using process.chdir() to avoid affecting parallel tests
21+
cwdSpy = spyOn(process, 'cwd').mockReturnValue(testDir);
1622
});
1723

1824
afterEach(() => {
19-
process.chdir(originalCwd);
25+
cwdSpy.mockRestore();
2026
fs.rmSync(testDir, { recursive: true, force: true });
2127
});
2228

2329
const loadDepVersions = async () => {
2430
// Bust the module cache by appending a query string
25-
// Since the process.cwd() has changed, we need the absolute path to the module
26-
const modulePath = path.join(originalCwd, 'src', 'utils', 'dep-versions.ts');
27-
const module = await import(`${modulePath}?t=${Date.now()}_${testCount}`);
31+
// In Bun, using the raw absolute path with query string correctly bypasses the cache
32+
const modulePath = path.join(
33+
originalCwd,
34+
'src',
35+
'utils',
36+
'dep-versions.ts',
37+
);
38+
const moduleUrl = `${modulePath}?t=${Date.now()}_${testCount}`;
39+
const module = await import(moduleUrl);
2840
return module.depVersions;
2941
};
3042

0 commit comments

Comments
 (0)