// What the end-to-end tests import is declared: a package found only as the // dependency of another (as @noble/hashes under polkadot-api) may vanish with it. import { readdirSync, readFileSync } from 'node:fs'; import { builtinModules } from 'node:module'; import { join } from 'node:path'; import { describe, expect, it } from 'vitest'; const root = join(__dirname, '../..'); const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')) as { dependencies?: Record; devDependencies?: Record; }; const declared = new Set([...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.devDependencies ?? {})]); /** `@scope/name/sub.js` → `@scope/name`; `name/sub` → `name`. */ const packageOf = (spec: string) => spec.split('/').slice(0, spec.startsWith('@') ? 2 : 1).join('/'); describe('the end-to-end tests', () => { it('import only declared packages', () => { const dir = join(root, 'e2e'); for (const file of readdirSync(dir).filter((f) => /\.(mjs|ts)$/.test(f))) { const text = readFileSync(join(dir, file), 'utf8'); for (const [, spec] of text.matchAll(/^import[^'"]*['"]([^'"]+)['"]/gm)) { if (!spec || spec.startsWith('.') || spec.startsWith('node:') || builtinModules.includes(spec)) continue; expect(declared.has(packageOf(spec)), `${file}: ${spec}`).toBe(true); } } }); });