|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { readFileSync, existsSync, statSync } from 'fs' |
| 3 | +import { join } from 'path' |
| 4 | +import { execSync } from 'child_process' |
| 5 | +import { createRequire } from 'module' |
| 6 | + |
| 7 | +/** |
| 8 | + * Package publish readiness tests |
| 9 | + * |
| 10 | + * Comprehensive gate tests that catch issues before publishing. |
| 11 | + * These validate npm pack contents, CJS/ESM exports, rule loading, |
| 12 | + * and package.json correctness. |
| 13 | + */ |
| 14 | + |
| 15 | +const root = process.cwd() |
| 16 | +const packageJsonPath = join(root, 'package.json') |
| 17 | +const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf-8')) |
| 18 | + |
| 19 | +describe('Package Publish Readiness', () => { |
| 20 | + describe('npm pack contents', () => { |
| 21 | + let packFiles: string[] |
| 22 | + |
| 23 | + beforeAll(() => { |
| 24 | + // npm pack --json output gets contaminated by the prepare script's build output, |
| 25 | + // so we parse the human-readable dry-run output instead. |
| 26 | + const output = execSync('npm pack --dry-run 2>&1', { |
| 27 | + cwd: root, |
| 28 | + encoding: 'utf-8', |
| 29 | + }) |
| 30 | + packFiles = output |
| 31 | + .split('\n') |
| 32 | + .filter(line => line.startsWith('npm notice') && /\d+(\.\d+)?[kKmMgG]?B\s/.test(line)) |
| 33 | + .map(line => line.replace(/^npm notice\s+[\d.]+[kKmMgG]?B\s+/, '').trim()) |
| 34 | + .filter(Boolean) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should include all export-referenced files', () => { |
| 38 | + for (const [exportPath, conditions] of Object.entries(pkg.exports)) { |
| 39 | + for (const [condition, filePath] of Object.entries(conditions as Record<string, string>)) { |
| 40 | + const normalized = (filePath as string).replace(/^\.\//, '') |
| 41 | + expect( |
| 42 | + packFiles.includes(normalized), |
| 43 | + `Export "${exportPath}" -> "${condition}": ${normalized} should be in npm pack output` |
| 44 | + ).toBe(true) |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + it('should not leak source files', () => { |
| 50 | + const leakedSrc = packFiles.filter(f => f.startsWith('src/')) |
| 51 | + expect(leakedSrc, 'src/ files should not be in the tarball').toHaveLength(0) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should not leak test files', () => { |
| 55 | + const leakedTests = packFiles.filter(f => f.startsWith('tests/')) |
| 56 | + expect(leakedTests, 'tests/ files should not be in the tarball').toHaveLength(0) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should not leak config or env files', () => { |
| 60 | + const forbidden = ['.env', '.eslintrc', 'tsconfig.json', 'tsup.config.ts', 'vitest.config.ts'] |
| 61 | + for (const name of forbidden) { |
| 62 | + expect( |
| 63 | + packFiles.includes(name), |
| 64 | + `${name} should not be in the tarball` |
| 65 | + ).toBe(false) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + it('should include README.md and LICENSE', () => { |
| 70 | + expect(packFiles.includes('README.md')).toBe(true) |
| 71 | + expect(packFiles.includes('LICENSE')).toBe(true) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should include bin/eslint-with-progress.js', () => { |
| 75 | + expect(packFiles.includes('bin/eslint-with-progress.js')).toBe(true) |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + describe('CJS require works', () => { |
| 80 | + it('should be requireable and have rules and configs', () => { |
| 81 | + const require = createRequire(import.meta.url) |
| 82 | + const plugin = require(join(root, 'dist/linter/eslint-plugin/index.js')) |
| 83 | + |
| 84 | + expect(plugin).toBeDefined() |
| 85 | + expect(plugin.rules).toBeDefined() |
| 86 | + expect(typeof plugin.rules).toBe('object') |
| 87 | + expect(plugin.configs).toBeDefined() |
| 88 | + expect(typeof plugin.configs).toBe('object') |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + describe('Export content validation', () => { |
| 93 | + it('. export should contain rules, configs, meta', () => { |
| 94 | + const content = readFileSync(join(root, 'dist/linter/eslint-plugin/index.js'), 'utf-8') |
| 95 | + expect(content.length).toBeGreaterThan(0) |
| 96 | + expect(content).toMatch(/rules/) |
| 97 | + expect(content).toMatch(/configs/) |
| 98 | + expect(content).toMatch(/meta/) |
| 99 | + }) |
| 100 | + |
| 101 | + it('./core export should contain A11yChecker', () => { |
| 102 | + const content = readFileSync(join(root, 'dist/index.js'), 'utf-8') |
| 103 | + expect(content.length).toBeGreaterThan(0) |
| 104 | + expect(content).toContain('A11yChecker') |
| 105 | + }) |
| 106 | + |
| 107 | + it('./formatter export should contain format function', () => { |
| 108 | + const content = readFileSync(join(root, 'dist/linter/eslint-plugin/formatter.js'), 'utf-8') |
| 109 | + expect(content.length).toBeGreaterThan(0) |
| 110 | + expect(content).toMatch(/format/) |
| 111 | + }) |
| 112 | + |
| 113 | + it('./formatter-progress export should contain format function', () => { |
| 114 | + const content = readFileSync(join(root, 'dist/linter/eslint-plugin/formatter-with-progress.js'), 'utf-8') |
| 115 | + expect(content.length).toBeGreaterThan(0) |
| 116 | + expect(content).toMatch(/format/) |
| 117 | + }) |
| 118 | + |
| 119 | + it('all built files referenced in exports should be non-empty', () => { |
| 120 | + for (const [exportPath, conditions] of Object.entries(pkg.exports)) { |
| 121 | + for (const [condition, filePath] of Object.entries(conditions as Record<string, string>)) { |
| 122 | + const fullPath = join(root, filePath as string) |
| 123 | + const stat = statSync(fullPath) |
| 124 | + expect( |
| 125 | + stat.size, |
| 126 | + `Export "${exportPath}" -> "${condition}": ${filePath} should be non-empty` |
| 127 | + ).toBeGreaterThan(0) |
| 128 | + } |
| 129 | + } |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + describe('All 36 rules loadable', () => { |
| 134 | + it('every rule in the built plugin should have meta and create', () => { |
| 135 | + const require = createRequire(import.meta.url) |
| 136 | + const plugin = require(join(root, 'dist/linter/eslint-plugin/index.js')) |
| 137 | + const ruleNames = Object.keys(plugin.rules) |
| 138 | + |
| 139 | + expect(ruleNames.length).toBe(36) |
| 140 | + |
| 141 | + for (const name of ruleNames) { |
| 142 | + const rule = plugin.rules[name] |
| 143 | + expect(rule.meta, `Rule "${name}" should have meta`).toBeDefined() |
| 144 | + expect(typeof rule.create, `Rule "${name}" should have create function`).toBe('function') |
| 145 | + } |
| 146 | + }) |
| 147 | + }) |
| 148 | + |
| 149 | + describe('Version consistency', () => { |
| 150 | + it('package.json version should match plugin meta version', () => { |
| 151 | + const require = createRequire(import.meta.url) |
| 152 | + const plugin = require(join(root, 'dist/linter/eslint-plugin/index.js')) |
| 153 | + |
| 154 | + expect(plugin.meta.version).toBe(pkg.version) |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + describe('Peer dependencies', () => { |
| 159 | + it('should list eslint as a peer dependency', () => { |
| 160 | + expect(pkg.peerDependencies).toHaveProperty('eslint') |
| 161 | + }) |
| 162 | + |
| 163 | + it('should list vitest as a peer dependency', () => { |
| 164 | + expect(pkg.peerDependencies).toHaveProperty('vitest') |
| 165 | + }) |
| 166 | + }) |
| 167 | + |
| 168 | + describe('Package.json required fields', () => { |
| 169 | + const requiredFields = [ |
| 170 | + 'name', |
| 171 | + 'version', |
| 172 | + 'description', |
| 173 | + 'license', |
| 174 | + 'repository', |
| 175 | + 'main', |
| 176 | + 'module', |
| 177 | + 'types', |
| 178 | + 'exports', |
| 179 | + ] |
| 180 | + |
| 181 | + for (const field of requiredFields) { |
| 182 | + it(`should have "${field}" field`, () => { |
| 183 | + expect(pkg[field], `Missing required field: ${field}`).toBeDefined() |
| 184 | + }) |
| 185 | + } |
| 186 | + }) |
| 187 | +}) |
0 commit comments