Skip to content

Commit 72e7c0f

Browse files
committed
refactor(test): validate bundled dependencies against package.json dependencies
- Update bundle validation to read package.json dependencies field - When dependencies is empty (as it should be for library packages), ensure no external packages are bundled inline - When dependencies exist, validate each one remains external (not bundled) - Detects bundled code by looking for __toCommonJS patterns characteristic of esbuild bundling This ensures the validation is based on the package's actual dependency configuration rather than a hardcoded list.
1 parent 98fb9d5 commit 72e7c0f

File tree

1 file changed

+61
-29
lines changed

1 file changed

+61
-29
lines changed

test/bundle-validation.test.mts

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,65 @@ function hasAbsolutePaths(content: string): {
4444
}
4545

4646
/**
47-
* Check if content is missing external dependencies (they should be require() calls).
48-
* External dependencies should NOT be bundled inline.
47+
* Check if bundle contains inlined dependencies.
48+
* Reads package.json dependencies and ensures they are NOT bundled inline.
4949
*/
50-
function checkExternalDependencies(content: string): {
51-
missingRequires: string[]
52-
hasAllRequires: boolean
53-
} {
54-
// Dependencies that should be external (as require() calls).
55-
const externalDeps = ['@socketsecurity/lib']
56-
57-
const missingRequires: string[] = []
58-
59-
for (const dep of externalDeps) {
60-
// Check if the bundle has require() calls for this dependency.
61-
const requirePattern = new RegExp(
62-
`require\\(["']${dep.replace('/', '\\/')}["']\\)`,
63-
)
64-
const hasRequire = requirePattern.test(content)
65-
66-
if (!hasRequire) {
67-
missingRequires.push(dep)
50+
async function checkBundledDependencies(content: string): Promise<{
51+
bundledDeps: string[]
52+
hasNoBundledDeps: boolean
53+
}> {
54+
// Read package.json to get runtime dependencies.
55+
const pkgJsonPath = path.join(packagePath, 'package.json')
56+
const pkgJson = JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'))
57+
const dependencies = pkgJson.dependencies || {}
58+
59+
const bundledDeps: string[] = []
60+
61+
// If we have NO dependencies, check that no external packages are bundled.
62+
if (Object.keys(dependencies).length === 0) {
63+
// Look for signs of bundled npm packages.
64+
// Bundled packages often have characteristic patterns like:
65+
// - var xxx_exports = {};
66+
// - __toCommonJS(package_name_exports)
67+
// - Multiple functions from same package bundled together.
68+
const bundledPackagePatterns = [
69+
// Socket packages that should always be external.
70+
/@socketsecurity\/lib/,
71+
/@socketsecurity\/sdk/,
72+
/@socketsecurity\/registry/,
73+
/@socketregistry\/packageurl-js/,
74+
]
75+
76+
for (const pattern of bundledPackagePatterns) {
77+
// Check if package name appears in context that suggests bundling.
78+
// Look for: var import_package = require("package") without the actual require call.
79+
// This would indicate the package code is bundled inline.
80+
const bundlePattern = new RegExp(
81+
`var\\s+\\w+\\s*=\\s*__toCommonJS\\([^)]*${pattern.source}`,
82+
)
83+
84+
if (bundlePattern.test(content)) {
85+
bundledDeps.push(pattern.source)
86+
}
87+
}
88+
} else {
89+
// If we have dependencies, check that they remain external (not bundled).
90+
for (const dep of Object.keys(dependencies)) {
91+
const escapedDep = dep.replace(/[/\\^$*+?.()|[\]{}]/g, '\\$&')
92+
// Check if dependency code is bundled by looking for __toCommonJS pattern.
93+
const bundlePattern = new RegExp(
94+
`var\\s+\\w+\\s*=\\s*__toCommonJS\\([^)]*${escapedDep}`,
95+
)
96+
97+
if (bundlePattern.test(content)) {
98+
bundledDeps.push(dep)
99+
}
68100
}
69101
}
70102

71103
return {
72-
missingRequires,
73-
hasAllRequires: missingRequires.length === 0,
104+
bundledDeps,
105+
hasNoBundledDeps: bundledDeps.length === 0,
74106
}
75107
}
76108

@@ -93,22 +125,22 @@ describe('Bundle validation', () => {
93125
)
94126
})
95127

96-
it('should have external dependencies as require() calls', async () => {
128+
it('should not bundle dependencies inline (validate against package.json dependencies)', async () => {
97129
const indexPath = path.join(distPath, 'index.js')
98130
const content = await fs.readFile(indexPath, 'utf8')
99131

100-
const result = checkExternalDependencies(content)
132+
const result = await checkBundledDependencies(content)
101133

102-
if (!result.hasAllRequires) {
103-
console.error('Missing require() calls for external dependencies:')
104-
for (const dep of result.missingRequires) {
134+
if (!result.hasNoBundledDeps) {
135+
console.error('Found bundled dependencies (should be external):')
136+
for (const dep of result.bundledDeps) {
105137
console.error(` - ${dep}`)
106138
}
107139
}
108140

109141
expect(
110-
result.hasAllRequires,
111-
'All external dependencies should be require() calls, not bundled inline',
142+
result.hasNoBundledDeps,
143+
'Dependencies from package.json should be external, not bundled inline',
112144
).toBe(true)
113145
})
114146
})

0 commit comments

Comments
 (0)