Fix backend test suite failures#2137
Fix backend test suite failures#2137trillium wants to merge 7 commits intochore/backend-esm-migrationfrom
Conversation
Vitest fails on empty test suites, unlike Jest which silently skips them. This block had no test cases and was leftover scaffolding.
…y in setup The ESM migration incorrectly rewrote user.test.js test logic. Restored findOne queries and correct assertions from development branch. Added missing await to deleteMany() in setup-test.js to prevent race conditions.
…ting ESM evaluates all imports before setup files run, so assertEnv() in app.js throws before dotenv can populate process.env. Mock it like the worker modules.
Remove callback from mongoose.connect() so await works correctly. Force-close mongoose connection in afterAll to prevent teardown errors.
Router tests need this env var to test the custom header middleware. Set it directly in each test file rather than relying on a global .env file.
Remove isActive assertion (field doesn't exist on User model) and accessLevel enum test (model has no enum constraint on this field).
This test only checked that REACT_APP_PROXY matched BACKEND_PORT from the .env file — no code logic tested. assert-env already validates required env vars at startup.
Code Review: Fix backend test suite failures1. setup-test.js Changes
Callback removal from 2. Router Test
|
Fixes #2136
Depends on: #2134 (ESM migration) must be merged first. This PR is based on that branch.
What changes did you make and why did you make them ?
The ESM migration exposed several test failures. Here is every failure that was addressed:
models/event.test.js— "Cannot save simple data"Error: No test found in suitedescribeblock with noit()calls. Jest silently skipped it; Vitest fails on it.describeblock (leftover scaffolding).models/user.test.js— "Create a simple user"TypeError: Cannot read properties of undefinedUser.find()[0]instead ofUser.findOne({ email }), and asserted on fields that weren't set. Combined with an un-awaiteddeleteMany()insetup-test.jscausing a race condition.awaittodeleteMany()insetup-test.js.models/user.test.js— "should serialize user data correctly"expected undefined to be trueisActive, which doesn't exist on the User model schema.isActiveassertion and property from test data.models/user.test.js— "should fail if accessLevel is not in enum"promise resolved instead of rejectingaccessLevelto have an enum constraint, but the User model defines it as{ type: String }with no enum.routers/auth.router.test.js— all 7 testsInvalid value "undefined" for header "x-customrequired-header"process.env.CUSTOM_REQUEST_HEADERis undefined without a.envfile. Under CJS/Jest, theauthorizationUtilsESM import error crashed these tests before they reached this point, masking the issue.vi.hoisted()so it's available before ESM imports evaluate.routers/events.router.test.js— all 5 testsrouters/projects.router.test.js— all 4 testsrouters/users.router.test.js— all 6 testscontrollers/project.controller.test.jsMongoNetworkError: connection closed(unhandled rejection)mongoose.connect()mixedawaitwith a callback (making await a no-op), andmongoose.connection.close()didn't force-kill pending operations.mongoose.connect(), changed tomongoose.connection.close(true).config/auth.config.test.jstest.skip)REACT_APP_PROXYmatchedBACKEND_PORTfrom.env— no code logic tested.assert-envalready validates required env vars at startup.Result: 40 tests passing, 0 skipped, 0 failures (up from 70 passing, 4 failing, 1 skipped under Jest on development).
Screenshots of Proposed Changes Of The Website (if any, please do not screen shot code changes)
N/A — no visual changes, backend only.