|
| 1 | +import { existsSync, readFileSync, statSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +const DIST_DIR = join(process.cwd(), 'dist'); |
| 6 | +const TYPES_DIR = join(DIST_DIR, 'types'); |
| 7 | +const LIB_DIR = join(DIST_DIR, 'lib'); |
| 8 | + |
| 9 | +describe('Build Artifacts', () => { |
| 10 | + describe('TypeScript Declarations', () => { |
| 11 | + it('should generate main index.d.ts file', () => { |
| 12 | + const indexDtsPath = join(TYPES_DIR, 'index.d.ts'); |
| 13 | + expect(existsSync(indexDtsPath), `Expected ${indexDtsPath} to exist`).toBe(true); |
| 14 | + |
| 15 | + const content = readFileSync(indexDtsPath, 'utf-8'); |
| 16 | + expect(content).toContain('export declare function search'); |
| 17 | + expect(content).toContain('export declare function compile'); |
| 18 | + expect(content).toContain('export declare const registerFunction'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should generate all required type declaration files', () => { |
| 22 | + const requiredFiles = [ |
| 23 | + 'AST.type.d.ts', |
| 24 | + 'JSON.type.d.ts', |
| 25 | + 'Lexer.d.ts', |
| 26 | + 'Lexer.type.d.ts', |
| 27 | + 'Parser.d.ts', |
| 28 | + 'Parser.type.d.ts', |
| 29 | + 'Runtime.d.ts', |
| 30 | + 'Scope.d.ts', |
| 31 | + 'TreeInterpreter.d.ts', |
| 32 | + 'utils/index.d.ts', |
| 33 | + 'utils/strings.d.ts', |
| 34 | + 'utils/text.d.ts', |
| 35 | + ]; |
| 36 | + |
| 37 | + for (const file of requiredFiles) { |
| 38 | + const filePath = join(TYPES_DIR, file); |
| 39 | + expect(existsSync(filePath), `Expected ${filePath} to exist`).toBe(true); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + it('should export all main types and functions', () => { |
| 44 | + const indexDtsPath = join(TYPES_DIR, 'index.d.ts'); |
| 45 | + const content = readFileSync(indexDtsPath, 'utf-8'); |
| 46 | + |
| 47 | + // Check for main function exports |
| 48 | + expect(content).toContain('export declare function search'); |
| 49 | + expect(content).toContain('export declare function compile'); |
| 50 | + expect(content).toContain('export declare function tokenize'); |
| 51 | + |
| 52 | + // Check for type exports |
| 53 | + expect(content).toContain('export type { JSONArray, JSONObject, JSONPrimitive, JSONValue }'); |
| 54 | + expect(content).toContain('export type { Options }'); |
| 55 | + expect(content).toContain('export type { BuiltInFunctionNames'); |
| 56 | + |
| 57 | + // Check for constant exports |
| 58 | + expect(content).toContain('export declare const TYPE_ANY'); |
| 59 | + expect(content).toContain('export declare const TYPE_STRING'); |
| 60 | + expect(content).toContain('export declare const TYPE_NUMBER'); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('UMD and ESM Bundles', () => { |
| 65 | + it('should generate UMD bundle', () => { |
| 66 | + const umdPath = join(DIST_DIR, 'jmespath.umd.js'); |
| 67 | + expect(existsSync(umdPath), `Expected ${umdPath} to exist`).toBe(true); |
| 68 | + |
| 69 | + const content = readFileSync(umdPath, 'utf-8'); |
| 70 | + expect(content).toContain('(function (global, factory)'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should generate minified UMD bundle', () => { |
| 74 | + const umdMinPath = join(DIST_DIR, 'jmespath.umd.min.js'); |
| 75 | + expect(existsSync(umdMinPath), `Expected ${umdMinPath} to exist`).toBe(true); |
| 76 | + |
| 77 | + const stat = statSync(umdMinPath); |
| 78 | + expect(stat.size).toBeGreaterThan(0); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should generate ESM bundle', () => { |
| 82 | + const esmPath = join(DIST_DIR, 'jmespath.esm.js'); |
| 83 | + expect(existsSync(esmPath), `Expected ${esmPath} to exist`).toBe(true); |
| 84 | + |
| 85 | + const content = readFileSync(esmPath, 'utf-8'); |
| 86 | + expect(content).toContain('export {'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should generate minified ESM bundle', () => { |
| 90 | + const esmMinPath = join(DIST_DIR, 'jmespath.esm.min.js'); |
| 91 | + expect(existsSync(esmMinPath), `Expected ${esmMinPath} to exist`).toBe(true); |
| 92 | + |
| 93 | + const stat = statSync(esmMinPath); |
| 94 | + expect(stat.size).toBeGreaterThan(0); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('CLI Binary', () => { |
| 99 | + it('should generate CLI binary', () => { |
| 100 | + const binPath = join(LIB_DIR, 'bin', 'jp.js'); |
| 101 | + expect(existsSync(binPath), `Expected ${binPath} to exist`).toBe(true); |
| 102 | + |
| 103 | + const content = readFileSync(binPath, 'utf-8'); |
| 104 | + expect(content).toContain('#! /usr/bin/env node'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should generate source maps for CLI binary', () => { |
| 108 | + const mapPath = join(LIB_DIR, 'bin', 'jp.js.map'); |
| 109 | + expect(existsSync(mapPath), `Expected ${mapPath} to exist`).toBe(true); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('Package.json Configuration', () => { |
| 114 | + it('should have correct main entry points', () => { |
| 115 | + const pkgPath = join(process.cwd(), 'package.json'); |
| 116 | + const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')); |
| 117 | + |
| 118 | + expect(pkg.main).toBe('dist/jmespath.umd.js'); |
| 119 | + expect(pkg.module).toBe('dist/jmespath.esm.js'); |
| 120 | + expect(pkg.types).toBe('dist/types/index.d.ts'); |
| 121 | + expect(pkg.typings).toBe('dist/types/index.d.ts'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should have correct bin configuration', () => { |
| 125 | + const pkgPath = join(process.cwd(), 'package.json'); |
| 126 | + const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')); |
| 127 | + |
| 128 | + expect(pkg.bin).toEqual({ jp: 'dist/lib/bin/jp.js' }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should include dist directory in files', () => { |
| 132 | + const pkgPath = join(process.cwd(), 'package.json'); |
| 133 | + const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')); |
| 134 | + |
| 135 | + expect(pkg.files).toContain('dist/'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should have modern exports field', () => { |
| 139 | + const pkgPath = join(process.cwd(), 'package.json'); |
| 140 | + const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')); |
| 141 | + |
| 142 | + expect(pkg.exports).toBeDefined(); |
| 143 | + expect(pkg.exports['.']).toBeDefined(); |
| 144 | + expect(pkg.exports['.'].types).toBe('./dist/types/index.d.ts'); |
| 145 | + expect(pkg.exports['.'].import).toBe('./dist/jmespath.esm.js'); |
| 146 | + expect(pkg.exports['.'].require).toBe('./dist/jmespath.umd.js'); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments