|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +// Tests the actual built IIFE, not the TypeScript source. Catches failure |
| 5 | +// modes that src/cdn.test.ts cannot: a broken tsup.cdn.js config, a missing |
| 6 | +// noExternal entry, an unreplaced __SDK_VERSION__ placeholder, or an IIFE |
| 7 | +// wrapper that clobbers the side-effect global assignment. |
| 8 | +// |
| 9 | +// Runs under testEnvironment: 'jsdom' so `window`, `navigator`, and |
| 10 | +// `globalThis` are all present — matching a real <script>-tag load. |
| 11 | + |
| 12 | +const ARTIFACT_PATH = path.resolve( |
| 13 | + __dirname, |
| 14 | + '../dist/cdn/imtbl-audience.global.js', |
| 15 | +); |
| 16 | + |
| 17 | +type AudienceGlobal = { |
| 18 | + Audience: { init: unknown }; |
| 19 | + AudienceError: unknown; |
| 20 | + AudienceEvents: Record<string, string>; |
| 21 | + IdentityType: Record<string, string>; |
| 22 | + canIdentify: unknown; |
| 23 | + canTrack: unknown; |
| 24 | + version: string; |
| 25 | +}; |
| 26 | + |
| 27 | +describe('CDN bundle artifact', () => { |
| 28 | + let g: AudienceGlobal; |
| 29 | + |
| 30 | + beforeAll(() => { |
| 31 | + if (!fs.existsSync(ARTIFACT_PATH)) { |
| 32 | + throw new Error( |
| 33 | + `CDN artifact not found at ${ARTIFACT_PATH}. ` |
| 34 | + + 'Run `pnpm transpile:cdn` (or `pnpm build`) before this test.', |
| 35 | + ); |
| 36 | + } |
| 37 | + const source = fs.readFileSync(ARTIFACT_PATH, 'utf8'); |
| 38 | + // Evaluates the pre-built bundle in the test's realm, the same way a |
| 39 | + // <script> tag does. Not user input — it's our own build output — so |
| 40 | + // the implied-eval rule doesn't apply. vm.runInThisContext was tried |
| 41 | + // first but runs in Node's root context, bypassing jsdom's window. |
| 42 | + // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func |
| 43 | + new Function(source)(); |
| 44 | + g = (globalThis as unknown as { ImmutableAudience: AudienceGlobal }) |
| 45 | + .ImmutableAudience; |
| 46 | + }); |
| 47 | + |
| 48 | + afterAll(() => { |
| 49 | + delete (globalThis as unknown as { ImmutableAudience?: unknown }) |
| 50 | + .ImmutableAudience; |
| 51 | + }); |
| 52 | + |
| 53 | + it('attaches ImmutableAudience to globalThis as a side effect', () => { |
| 54 | + expect(g).toBeDefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('exposes every runtime value that the npm entry exports', () => { |
| 58 | + expect(typeof g.Audience).toBe('function'); |
| 59 | + expect(typeof g.Audience.init).toBe('function'); |
| 60 | + expect(typeof g.AudienceError).toBe('function'); |
| 61 | + expect(typeof g.AudienceEvents).toBe('object'); |
| 62 | + expect(typeof g.IdentityType).toBe('object'); |
| 63 | + expect(typeof g.canIdentify).toBe('function'); |
| 64 | + expect(typeof g.canTrack).toBe('function'); |
| 65 | + expect(typeof g.version).toBe('string'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('replaces the __SDK_VERSION__ placeholder at build time', () => { |
| 69 | + expect(g.version).not.toBe('__SDK_VERSION__'); |
| 70 | + expect(g.version.length).toBeGreaterThan(0); |
| 71 | + }); |
| 72 | + |
| 73 | + it('populates the IdentityType enum', () => { |
| 74 | + expect(g.IdentityType.Passport).toBe('passport'); |
| 75 | + expect(g.IdentityType.Steam).toBe('steam'); |
| 76 | + expect(g.IdentityType.Custom).toBe('custom'); |
| 77 | + }); |
| 78 | +}); |
0 commit comments