Skip to content

build(esm): migrate action and test suite to ESM - #74

Merged
c-vigo merged 2 commits into
devfrom
feature/58-esm-migration
Jul 14, 2026
Merged

c-vigo merged 2 commits into
devfrom
feature/58-esm-migration

Conversation

@c-vigo

@c-vigo c-vigo commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Refs #58. Closes #49, #50, #51. Unblocks #54.

The @actions/* toolkit is ESM-only at its next major — its exports map has no "require" condition at all — so @actions/core v3, @actions/github v9 and @actions/http-client v4 cannot land individually. They move together here.

Changes

  • package.json — "type": "module". Dropped the @actions/http-client and undici overrides: the http-client pin (3.0.2) was what actually blocked the v4 major, and the undici pin is now redundant, since http-client v4 depends on undici: ^6.23.0 which resolves to the same 6.27.0 the pin was added to force. npm audit --omit=dev reports 0 vulnerabilities.
  • tsconfig.json — module/moduleResolution → nodenext; relative imports carry explicit .js extensions.
  • Bundling — ncc handles ESM: it emits an ESM dist/index.js plus a dist/package.json declaring the bundle's module type. That second file is real shipped output, so it is now tracked and re-admitted in .gitignore (which [TASK] Remove accidentally committed tsc byproducts under dist/src/ and fix the .gitignore rule #70 had just narrowed to dist/index.js alone — it would otherwise have been silently dropped).
  • Jest — ESM mode (--experimental-vm-modules). Mocks move from jest.mock + a __mocks__ manual mock to jest.unstable_mockModule + dynamic imports; src/__tests__/setup.ts is deleted and each suite registers its own mocks. All 84 tests preserved — describe/it titles and expect() counts are byte-identical to HEAD, no skips.

Two latent problems this surfaced — both invisible to the test suite

1. The bundled action was a silent no-op. The CJS require.main === module entry guard does not survive bundling to ESM: ncc rewrites it to __nccwpck_require__.c[__nccwpck_require__.s] === module, which evaluates false. Executing that bundle:

$ node dist/index.js          # GITHUB_TOKEN, TARGET_BRANCH, ... all set
$ echo $?
0

No output. main() never ran — the action would have exited 0 having committed nothing. Replaced with an ESM guard comparing import.meta.url against pathToFileURL(process.argv[1]). Same invocation now:

$ node dist/index.js
Using TARGET_BRANCH: dev
No files to commit

Nothing in CI would have caught this: unit tests import main() directly, and the repo dogfoods commit-action at a pinned released SHA, so no workflow ever executes the locally-built bundle.

2. The signed-commit REST flow was never type-checked. Classic moduleResolution: node ignores exports maps, so @octokit/core/types never resolved and every octokit.rest.* call in src/commit.ts silently degraded to any. Probe on the old config — r.data.sha typed as any; under nodenext it types as string. The flow now type-checks clean against real Octokit types (no latent errors surfaced — commit.ts was already correct, just unverified).

Verification

  • npm test — 84/84 passing, 3/3 suites.
  • npx tsc --noEmit — 0 errors.
  • npm run test:coverage — 92.5% statements / 82.1% branches, above the 70% thresholds.
  • npm run bundle — clean ESM bundle.
  • Bundle executed on Node 24 (see above) — loads, main() fires, reaches the API path.
  • just precommit (full suite) — green.

Not yet verified

The live REST flow against the real GitHub API (createBlob → createTree → createCommit → updateRef) under Octokit v7 has not been exercised — only type-checked. Per the issue, unit tests are explicitly not sufficient evidence here. Worth an end-to-end run before this reaches a tag.

Refs: #58

c-vigo added 2 commits July 14, 2026 16:08
The @actions/* toolkit is ESM-only at its next major -- its exports map has
no "require" condition at all -- so @actions/core v3, @actions/github v9 and
@actions/http-client v4 could not land individually and move together here.

- package.json: "type": "module"; drop the @actions/http-client and undici
  overrides. The http-client pin (3.0.2) was what blocked the v4 major, and
  the undici pin is now redundant: http-client v4 depends on undici ^6.23.0,
  which resolves to the same 6.27.0 the pin forced. npm audit is clean.
- tsconfig: module/moduleResolution -> nodenext; relative imports carry
  explicit .js extensions.
- ncc now emits an ESM bundle plus a dist/package.json declaring the bundle's
  module type. Track it alongside dist/index.js and re-admit it in .gitignore,
  which #70 had narrowed to dist/index.js alone.
- Jest runs in ESM mode (--experimental-vm-modules): module mocks move from
  jest.mock + a __mocks__ manual mock to jest.unstable_mockModule with dynamic
  imports, so src/__tests__/setup.ts is gone and each suite registers its own
  mocks. All 84 tests preserved; titles and assertion counts unchanged.

Two things the migration surfaced, both invisible to the test suite:

The CJS `require.main === module` entry guard does not survive bundling to
ESM. ncc rewrites it to a webpack shim that evaluates false, so the bundled
action loaded, exited 0, and committed nothing -- a silent no-op. Replaced
with an import.meta.url/argv[1] comparison and verified by executing the
bundle: it now logs and reaches the API path.

Classic `moduleResolution: node` ignores exports maps, so @octokit/core/types
never resolved and every octokit.rest.* call in commit.ts silently degraded to
`any` -- the whole signed-commit REST flow was unchecked. nodenext restores
real Octokit types and the flow type-checks clean.

Also unblocks TypeScript 7, which removes moduleResolution: node outright.

Closes: #49, #50, #51
Refs: #58, #54
Nothing in this repo ever ran the artifact consumers actually execute. The
unit tests import main() directly, and the repo dogfoods commit-action at a
pinned released SHA -- never at ./ -- so dist/index.js was never exercised.

That gap is not theoretical: the ESM migration found that the CommonJS
`require.main === module` entry guard does not survive bundling to ESM. The
bundled action loaded, exited 0, and committed nothing, with 84 unit tests
passing throughout.

Run the action as a consumer does (`uses: ./`) on every PR: create a scratch
branch, commit a file to it through the real createBlob -> createTree ->
createCommit -> updateRef flow, then assert via the API that the commit
exists, is signed, and carries the expected content. Delete the branch
afterwards, on success or failure.

A non-empty commit-sha output is the specific assertion that catches the
silent no-op. The signature assertion keeps the action's core promise honest:
the repo's "Signed commits" ruleset covers ~ALL branches, so an unsigned
commit would be rejected by the API outright.

Skipped on fork PRs, whose GITHUB_TOKEN is read-only and cannot create the
scratch branch.

Refs: #58
@c-vigo
c-vigo merged commit a624eb2 into dev Jul 14, 2026
10 checks passed
@c-vigo
c-vigo deleted the feature/58-esm-migration branch July 14, 2026 14:26
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