Skip to content

fix(backend): complete ESM migration — runtime crash fix + test suite conversion#2158

Open
trillium wants to merge 4 commits into
hackforla:developmentfrom
trillium:eunicode/fix-esm-export-import-mismatch
Open

fix(backend): complete ESM migration — runtime crash fix + test suite conversion#2158
trillium wants to merge 4 commits into
hackforla:developmentfrom
trillium:eunicode/fix-esm-export-import-mismatch

Conversation

@trillium
Copy link
Copy Markdown
Member

Fixes #

What changes did you make and why did you make them?

By @eunicode (original branch author):

  • Fixed the runtime crash caused by an incomplete ESM migration — backend/workers/lib/generateEventData.js, refreshToken.model.js, tokenCleanup.js, and grantpermission.router.js were left behind by PR Migrate backend from CommonJS to ES Modules #2134 still using module.exports/require while their callers had been converted to ESM import
  • Fixed auth.middleware.js — switched to named imports from authorizationUtils.js and corrected a hasAnyRole spread bug where roles were being passed as an array instead of spread arguments, silently breaking role checks
  • Added backend/env.bootstrap.js to fix a dotenv timing bug introduced by ESM — in ESM, static imports are hoisted and execute before module body code, so dotenv.config() in app.js was running after config modules had already read process.env; this side-effect module is imported first in server.js to guarantee env vars load before any config module runs

By @trillium (follow-up to complete the branch):

  • Completed the test suite migration from Jest to Vitest — PR Migrate backend from CommonJS to ES Modules #2134 switched the test runner but left all 16 active test files using Jest globals (jest.mock, jest.fn, jest.spyOn); converted all of them to Vitest equivalents with correct ESM mock path resolution, vi.hoisted() for TDZ safety, and vi.importActual() where needed
  • Fixed stale and incorrect mock assertions uncovered during migration (e.g. verifyUser.isAdminByEmail asserted on a route that does not use that middleware; verifyCookie asserted on routes where it was later commented out)
  • Stubbed broken legacy tests in test/old-tests/ — these had broken import paths and required a live database; replaced with describe.skip stubs pointing to issue Skip Failing Tests and Suites #2036

Screenshots of Proposed Changes Of The Website (if any)

N/A — backend-only changes.

eunicode and others added 4 commits May 11, 2026 16:57
Convert all remaining test files from Jest/CJS to Vitest/ESM:
- Replace jest.mock/jest.fn/jest.spyOn with vi.* equivalents
- Convert require() to import statements
- Fix vi.mock() path specifiers to match exact ESM imports
- Use vi.hoisted() for variables referenced in vi.mock factories (TDZ fix)
- Use vi.importActual() for createRecurringEvents tests (ESM mock isolation)
- Mock cors with default export shape
- Stub old-tests and integration tests with describe.skip (broken paths)

Fix mock correctness issues:
- auth.router.test.js: mock '../middleware/index.js' (not '../middleware')
  and add '../middleware/auth.middleware.js' mock for authenticateRefreshToken
- projects.router.test.js: remove verifyCookie assertions on routes that
  don't use that middleware (POST, PUT, PATCH /:ProjectId)
- users.router.test.js: add middleware mock so Auth.authUser doesn't block
  UPDATE/DELETE routes in tests
- recurringEvents.router.test.js: mock correct path '../middleware/index.js'
  and provide cors default export

Result: 17 test files passing, 89 tests passing, 34 skipped (old/integration)

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
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.

2 participants