fix(backend): complete ESM migration — runtime crash fix + test suite conversion#2158
Open
trillium wants to merge 4 commits into
Open
fix(backend): complete ESM migration — runtime crash fix + test suite conversion#2158trillium wants to merge 4 commits into
trillium wants to merge 4 commits into
Conversation
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>
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.
Fixes #
What changes did you make and why did you make them?
By @eunicode (original branch author):
backend/workers/lib/generateEventData.js,refreshToken.model.js,tokenCleanup.js, andgrantpermission.router.jswere left behind by PR Migrate backend from CommonJS to ES Modules #2134 still usingmodule.exports/requirewhile their callers had been converted to ESMimportauth.middleware.js— switched to named imports fromauthorizationUtils.jsand corrected ahasAnyRolespread bug where roles were being passed as an array instead of spread arguments, silently breaking role checksbackend/env.bootstrap.jsto fix a dotenv timing bug introduced by ESM — in ESM, static imports are hoisted and execute before module body code, sodotenv.config()inapp.jswas running after config modules had already readprocess.env; this side-effect module is imported first inserver.jsto guarantee env vars load before any config module runsBy @trillium (follow-up to complete the branch):
jest.mock,jest.fn,jest.spyOn); converted all of them to Vitest equivalents with correct ESM mock path resolution,vi.hoisted()for TDZ safety, andvi.importActual()where neededverifyUser.isAdminByEmailasserted on a route that does not use that middleware;verifyCookieasserted on routes where it was later commented out)test/old-tests/— these had broken import paths and required a live database; replaced withdescribe.skipstubs pointing to issue Skip Failing Tests and Suites #2036Screenshots of Proposed Changes Of The Website (if any)
N/A — backend-only changes.