Skip to content

Commit ed7d510

Browse files
committed
Rename setupPackageTest to isolatePackage
- More descriptive name emphasizing isolation - Consolidate into single function with options parameter - Handle path resolution and package name detection in wrapper - Support both socket-registry packages and local development packages - Add comprehensive documentation with examples
1 parent 49d4ee0 commit ed7d510

1 file changed

Lines changed: 98 additions & 25 deletions

File tree

test/utils/test-helpers.mjs

Lines changed: 98 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,112 @@
1-
/** @fileoverview Shared test utilities for npm package testing. */
1+
/**
2+
* @fileoverview Shared test utilities for npm package testing.
3+
* Provides helpers for setting up isolated test environments for both
4+
* socket-registry packages and local development packages.
5+
*/
26

7+
import { existsSync } from 'node:fs'
38
import path from 'node:path'
9+
import { fileURLToPath } from 'node:url'
410

11+
import constants from '../../scripts/constants.mjs'
512
import { installPackageForTesting } from '../../scripts/utils/package.mjs'
13+
import {
14+
readPackageJson,
15+
resolveOriginalPackageName,
16+
} from '../../registry/dist/lib/packages.js'
17+
18+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
619

720
/**
8-
* Creates a test setup function for npm package tests.
9-
* @param {string} sockRegPkgName - Socket registry package name.
10-
* @returns {Promise<{pkgPath: string}>}
21+
* Isolates a package in a temporary test environment.
22+
*
23+
* Supports two modes:
24+
* 1. Socket registry packages: Pass package name (e.g., '@socketregistry/packageurl-js')
25+
* 2. Local development packages: Pass relative or absolute path (e.g., '../../socket-packageurl-js' or '.')
26+
*
27+
* The helper creates an isolated test environment by:
28+
* - Installing the package in a temporary directory
29+
* - Copying package files to node_modules
30+
* - Installing dependencies
31+
* - Preserving test scripts (for registry packages)
32+
*
33+
* @param {string} packageOrPath - Package name or local path to package directory.
34+
* @param {object} [options] - Optional configuration.
35+
* @param {string[]} [options.entryPoints] - Array of entry point filenames to load.
36+
* @returns {Promise<{pkgPath: string, modules?: any[]}>}
37+
*
38+
* @example
39+
* // Test a socket-registry package
40+
* const { pkgPath } = await isolatePackage('@socketregistry/packageurl-js')
41+
*
42+
* @example
43+
* // Test a local development package
44+
* const { pkgPath } = await isolatePackage('../../socket-packageurl-js')
45+
*
46+
* @example
47+
* // Load multiple entry points
48+
* const { pkgPath, modules } = await isolatePackage('../../socket-packageurl-js', {
49+
* entryPoints: ['package-url.js', 'url-converter.js']
50+
* })
1151
*/
12-
async function setupPackageTest(sockRegPkgName) {
13-
const result = await installPackageForTesting(sockRegPkgName)
52+
async function isolatePackage(packageOrPath, options = {}) {
53+
const { entryPoints } = options
54+
55+
// Determine if this is a path or package name
56+
const isPath = packageOrPath.startsWith('.') || path.isAbsolute(packageOrPath)
57+
58+
let sourcePath
59+
let packageName
60+
let versionSpec
61+
62+
if (isPath) {
63+
// Local development package
64+
sourcePath = path.resolve(__dirname, '..', '..', packageOrPath)
65+
66+
// Read package.json to get the name
67+
const pkgJson = await readPackageJson(sourcePath, { normalize: true })
68+
packageName = pkgJson.name
69+
} else {
70+
// Socket registry package
71+
const socketPkgName = packageOrPath
72+
sourcePath = path.join(constants.npmPackagesPath, socketPkgName)
73+
74+
if (!existsSync(sourcePath)) {
75+
throw new Error(`No Socket override found for ${socketPkgName}`)
76+
}
77+
78+
// Resolve to original npm package name
79+
packageName = resolveOriginalPackageName(socketPkgName)
80+
81+
// Get version spec from test/npm/package.json
82+
const testPkgJson = await readPackageJson(constants.testNpmPkgJsonPath, {
83+
normalize: true,
84+
})
85+
versionSpec = testPkgJson.devDependencies?.[packageName]
86+
87+
if (!versionSpec) {
88+
throw new Error(`${packageName} not in devDependencies`)
89+
}
90+
}
91+
92+
const result = await installPackageForTesting(sourcePath, packageName, {
93+
versionSpec,
94+
})
95+
1496
if (!result.installed) {
1597
throw new Error(`Failed to install package: ${result.reason}`)
1698
}
17-
return {
18-
pkgPath: result.packagePath,
19-
}
20-
}
2199

22-
/**
23-
* Common test helper for packages that require multiple entry points.
24-
* @param {string} sockRegPkgName - Socket registry package name.
25-
* @param {string[]} entryPoints - Array of entry point filenames.
26-
* @returns {Promise<{pkgPath: string, modules: any[]}>}
27-
*/
28-
async function setupMultiEntryTest(sockRegPkgName, entryPoints) {
29-
const { pkgPath } = await setupPackageTest(sockRegPkgName)
30-
const modules = entryPoints.map(entryPoint =>
31-
require(path.join(pkgPath, entryPoint)),
32-
)
33-
return {
34-
pkgPath,
35-
modules,
100+
const pkgPath = result.packagePath
101+
102+
if (entryPoints && entryPoints.length > 0) {
103+
const modules = entryPoints.map(entryPoint =>
104+
require(path.join(pkgPath, entryPoint)),
105+
)
106+
return { pkgPath, modules }
36107
}
108+
109+
return { pkgPath }
37110
}
38111

39-
export { setupMultiEntryTest, setupPackageTest }
112+
export { isolatePackage }

0 commit comments

Comments
 (0)