Skip to content

refactor: migrate test setup from CJS to ESM#35

Merged
caseycs merged 3 commits intomainfrom
migrate-tests-cjs-to-esm
Mar 1, 2026
Merged

refactor: migrate test setup from CJS to ESM#35
caseycs merged 3 commits intomainfrom
migrate-tests-cjs-to-esm

Conversation

@caseycs
Copy link
Owner

@caseycs caseycs commented Mar 1, 2026

Replace the CJS __mocks__/@actions/core.js hack with proper ESM test infrastructure using jest.unstable_mockModule() and typed fixtures, aligning with the official actions/typescript-action template pattern.

  • Add "type": "module" to package.json
  • Move Jest config from inline to jest.config.js with ESM settings
  • Create __fixtures__/core.ts as typed ESM mock
  • Rewrite test files to use dynamic import() after mock registration
  • Add tsconfig.test.json for test-specific TypeScript settings
  • Add .js extensions to source imports
  • Install ts-jest-resolver for ESM .js.ts resolution
  • Delete __mocks__/ directory

caseycs added 3 commits March 1, 2026 11:48
Replace the CJS `__mocks__/@actions/core.js` hack with proper ESM test
infrastructure using `jest.unstable_mockModule()` and typed fixtures,
aligning with the official actions/typescript-action template pattern.

- Add `"type": "module"` to package.json
- Move Jest config from inline to `jest.config.js` with ESM settings
- Create `__fixtures__/core.ts` as typed ESM mock
- Rewrite test files to use dynamic `import()` after mock registration
- Add `tsconfig.test.json` for test-specific TypeScript settings
- Add `.js` extensions to source imports
- Install `ts-jest-resolver` for ESM `.js` → `.ts` resolution
- Delete `__mocks__/` directory
With "type": "module" in package.json, Node treats dist/index.js as ESM.
esbuild defaults to CJS format with --platform=node, causing a
"require is not defined" error at runtime. Adding --format=esm fixes it.
@actions/core depends on `tunnel` which uses CJS require() for Node
builtins (net, tls, etc). These can't be converted to ESM imports by
esbuild. Output as dist/index.cjs instead so Node treats the bundle as
CJS regardless of the "type": "module" setting.
@caseycs caseycs merged commit 4ff1c90 into main Mar 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant