|
| 1 | +// npx vitest run src/__tests__/package-exports.test.ts |
| 2 | + |
| 3 | +import { resolve } from "path" |
| 4 | + |
| 5 | +describe("Package Exports Integration Tests", () => { |
| 6 | + const packageRoot = resolve(__dirname, "../..") |
| 7 | + const distPath = resolve(packageRoot, "dist") |
| 8 | + |
| 9 | + it("should import from built ESM file", async () => { |
| 10 | + const esmPath = resolve(distPath, "index.mjs") |
| 11 | + |
| 12 | + // Dynamic import of the built ESM file |
| 13 | + const module = await import(esmPath) |
| 14 | + |
| 15 | + expect(module.GLOBAL_STATE_KEYS).toBeDefined() |
| 16 | + expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true) |
| 17 | + expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0) |
| 18 | + }) |
| 19 | + |
| 20 | + it("should import from built CJS file", () => { |
| 21 | + const cjsPath = resolve(distPath, "index.js") |
| 22 | + |
| 23 | + // Clear require cache to ensure fresh import |
| 24 | + delete require.cache[cjsPath] |
| 25 | + |
| 26 | + // Require the built CJS file |
| 27 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 28 | + const module = require(cjsPath) |
| 29 | + |
| 30 | + expect(module.GLOBAL_STATE_KEYS).toBeDefined() |
| 31 | + expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true) |
| 32 | + expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0) |
| 33 | + }) |
| 34 | + |
| 35 | + it("should have consistent exports between ESM and CJS builds", async () => { |
| 36 | + const esmPath = resolve(distPath, "index.mjs") |
| 37 | + const cjsPath = resolve(distPath, "index.js") |
| 38 | + |
| 39 | + // Clear require cache. |
| 40 | + delete require.cache[cjsPath] |
| 41 | + |
| 42 | + // Import both versions. |
| 43 | + const esmModule = await import(esmPath) |
| 44 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 45 | + const cjsModule = require(cjsPath) |
| 46 | + |
| 47 | + // Compare key exports. |
| 48 | + expect(esmModule.GLOBAL_STATE_KEYS).toEqual(cjsModule.GLOBAL_STATE_KEYS) |
| 49 | + expect(esmModule.SECRET_STATE_KEYS).toEqual(cjsModule.SECRET_STATE_KEYS) |
| 50 | + |
| 51 | + // Ensure both have the same export keys. |
| 52 | + const esmKeys = Object.keys(esmModule).sort() |
| 53 | + const cjsKeys = Object.keys(cjsModule).sort() |
| 54 | + expect(esmKeys).toEqual(cjsKeys) |
| 55 | + }) |
| 56 | + |
| 57 | + it("should import using package name resolution (simulated)", async () => { |
| 58 | + // This simulates how the package would be imported by consumers. |
| 59 | + // We test the source files since we can't easily test the published package. |
| 60 | + const module = await import("../index.js") |
| 61 | + |
| 62 | + // Verify the main exports that consumers would use. |
| 63 | + expect(module.GLOBAL_STATE_KEYS).toBeDefined() |
| 64 | + expect(module.SECRET_STATE_KEYS).toBeDefined() |
| 65 | + |
| 66 | + // Test some common type exports exist. |
| 67 | + expect(typeof module.GLOBAL_STATE_KEYS).toBe("object") |
| 68 | + expect(typeof module.SECRET_STATE_KEYS).toBe("object") |
| 69 | + }) |
| 70 | + |
| 71 | + it("should have TypeScript definitions available", () => { |
| 72 | + const dtsPath = resolve(distPath, "index.d.ts") |
| 73 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 74 | + const fs = require("fs") |
| 75 | + |
| 76 | + // Check that the .d.ts file exists and has content. |
| 77 | + expect(fs.existsSync(dtsPath)).toBe(true) |
| 78 | + |
| 79 | + const dtsContent = fs.readFileSync(dtsPath, "utf8") |
| 80 | + expect(dtsContent.length).toBeGreaterThan(0) |
| 81 | + expect(dtsContent).toContain("export") |
| 82 | + }) |
| 83 | +}) |
0 commit comments