refactor: migrate test setup from CJS to ESM#35
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace the CJS
__mocks__/@actions/core.jshack with proper ESM test infrastructure usingjest.unstable_mockModule()and typed fixtures, aligning with the official actions/typescript-action template pattern."type": "module"to package.jsonjest.config.jswith ESM settings__fixtures__/core.tsas typed ESM mockimport()after mock registrationtsconfig.test.jsonfor test-specific TypeScript settings.jsextensions to source importsts-jest-resolverfor ESM.js→.tsresolution__mocks__/directory