From ad8769624802103f89225bf47e369e5cf65ba198 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 14 Jun 2026 01:31:12 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=AA=20Add=20unit=20tests=20for=20d?= =?UTF-8?q?ep-versions=20utility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🎯 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 and bypassing module caching, improving overall reliability. Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --- tests/dep-versions.test.ts | 140 +++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 tests/dep-versions.test.ts diff --git a/tests/dep-versions.test.ts b/tests/dep-versions.test.ts new file mode 100644 index 0000000..4f06a59 --- /dev/null +++ b/tests/dep-versions.test.ts @@ -0,0 +1,140 @@ +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; + +describe('depVersions utility', () => { + const originalCwd = process.cwd(); + let testDir: string; + let testCount = 0; + + beforeEach(() => { + testCount++; + testDir = path.join(os.tmpdir(), `temp-test-dep-versions-${Date.now()}-${testCount}`); + fs.mkdirSync(testDir, { recursive: true }); + process.chdir(testDir); + }); + + afterEach(() => { + process.chdir(originalCwd); + fs.rmSync(testDir, { recursive: true, force: true }); + }); + + const loadDepVersions = async () => { + // Bust the module cache by appending a query string + // Since the process.cwd() has changed, we need the absolute path to the module + const modulePath = path.join(originalCwd, 'src', 'utils', 'dep-versions.ts'); + const module = await import(`${modulePath}?t=${Date.now()}_${testCount}`); + return module.depVersions; + }; + + test('should return an empty object if no package.json is found', async () => { + // testDir has no package.json + const deps = await loadDepVersions(); + expect(deps).toEqual({}); + }); + + test('should return an empty object if package.json has no dependencies or devDependencies', async () => { + fs.writeFileSync( + path.join(testDir, 'package.json'), + JSON.stringify({ name: 'test-app' }), + ); + + const deps = await loadDepVersions(); + expect(deps).toEqual({}); + }); + + test('should resolve versions for dependencies and devDependencies and sort keys', async () => { + fs.writeFileSync( + path.join(testDir, 'package.json'), + JSON.stringify({ + dependencies: { + zeta: '^1.0.0', + alpha: '^2.0.0', + }, + devDependencies: { + beta: '^3.0.0', + }, + }), + ); + + const depsToCreate = [ + { name: 'zeta', version: '1.0.5' }, + { name: 'alpha', version: '2.1.0' }, + { name: 'beta', version: '3.0.1' }, + ]; + + for (const dep of depsToCreate) { + const depDir = path.join(testDir, 'node_modules', dep.name); + fs.mkdirSync(depDir, { recursive: true }); + fs.writeFileSync( + path.join(depDir, 'package.json'), + JSON.stringify({ version: dep.version }), + ); + } + + const deps = await loadDepVersions(); + + // Check that versions are resolved + expect(deps).toEqual({ + alpha: '2.1.0', + beta: '3.0.1', + zeta: '1.0.5', + }); + + // Check that keys are sorted alphabetically + const keys = Object.keys(deps); + expect(keys).toEqual(['alpha', 'beta', 'zeta']); + }); + + test('should skip dependencies that cannot be resolved without throwing an error', async () => { + fs.writeFileSync( + path.join(testDir, 'package.json'), + JSON.stringify({ + dependencies: { + exists: '^1.0.0', + missing: '^2.0.0', + }, + }), + ); + + // Only create 'exists' + const existsDir = path.join(testDir, 'node_modules', 'exists'); + fs.mkdirSync(existsDir, { recursive: true }); + fs.writeFileSync( + path.join(existsDir, 'package.json'), + JSON.stringify({ version: '1.0.0' }), + ); + + const deps = await loadDepVersions(); + expect(deps).toEqual({ + exists: '1.0.0', + }); + }); + + test('should deduplicate dependencies appearing in both dependencies and devDependencies', async () => { + fs.writeFileSync( + path.join(testDir, 'package.json'), + JSON.stringify({ + dependencies: { + shared: '^1.0.0', + }, + devDependencies: { + shared: '^1.0.0', + }, + }), + ); + + const sharedDir = path.join(testDir, 'node_modules', 'shared'); + fs.mkdirSync(sharedDir, { recursive: true }); + fs.writeFileSync( + path.join(sharedDir, 'package.json'), + JSON.stringify({ version: '1.0.2' }), + ); + + const deps = await loadDepVersions(); + expect(deps).toEqual({ + shared: '1.0.2', + }); + }); +}); From 3b31fb5c00631e794f8d82009d05889a2cccdc16 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 14 Jun 2026 01:47:55 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=AA=20Add=20unit=20tests=20for=20d?= =?UTF-8?q?ep-versions=20utility=20and=20fix=20CI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🎯 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> --- tests/dep-versions.test.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/dep-versions.test.ts b/tests/dep-versions.test.ts index 4f06a59..2e36f14 100644 --- a/tests/dep-versions.test.ts +++ b/tests/dep-versions.test.ts @@ -1,4 +1,4 @@ -import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import { afterEach, beforeEach, describe, expect, spyOn, test } from 'bun:test'; import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; @@ -7,24 +7,36 @@ describe('depVersions utility', () => { const originalCwd = process.cwd(); let testDir: string; let testCount = 0; + let cwdSpy: ReturnType; beforeEach(() => { testCount++; - testDir = path.join(os.tmpdir(), `temp-test-dep-versions-${Date.now()}-${testCount}`); + testDir = path.join( + os.tmpdir(), + `temp-test-dep-versions-${Date.now()}-${testCount}`, + ); fs.mkdirSync(testDir, { recursive: true }); - process.chdir(testDir); + + // Mock process.cwd() instead of using process.chdir() to avoid affecting parallel tests + cwdSpy = spyOn(process, 'cwd').mockReturnValue(testDir); }); afterEach(() => { - process.chdir(originalCwd); + cwdSpy.mockRestore(); fs.rmSync(testDir, { recursive: true, force: true }); }); const loadDepVersions = async () => { // Bust the module cache by appending a query string - // Since the process.cwd() has changed, we need the absolute path to the module - const modulePath = path.join(originalCwd, 'src', 'utils', 'dep-versions.ts'); - const module = await import(`${modulePath}?t=${Date.now()}_${testCount}`); + // In Bun, using the raw absolute path with query string correctly bypasses the cache + const modulePath = path.join( + originalCwd, + 'src', + 'utils', + 'dep-versions.ts', + ); + const moduleUrl = `${modulePath}?t=${Date.now()}_${testCount}`; + const module = await import(moduleUrl); return module.depVersions; };