Open
Conversation
…pplyForShiftService
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.
Unit Tests: Shift Apply & Approve Services
Overview
This PR adds unit tests for
applyForShiftServiceandapproveShiftServiceinshiftApplication.service.js, and includes the configuration changes required to make Jest work with the project's ESM setup.This branch was created from
louisa/feature/shift-apply-approve-service— see PR #332Config Changes
package.json— dependency version bumpsThe following packages were already present but were updated during
npm install:@babel/core@babel/preset-envbabel-jestcross-envcross-envis the only newly added package. It is required to setNODE_OPTIONSin a cross-platform way so the test script works on both Windows and Unix.package.json— test scriptUpdated the
testscript to:--experimental-vm-modulesis required because the project uses"type": "module"(ESM). Without this flag, Jest cannot handle ES module imports.cross-envensures the flag works on both Windows and Unix systems.--runInBandruns tests serially to avoid race conditions.jest.config.cjsConfigured Jest to:
silent: trueadded to suppress console output (MongoDB connection warnings, SMTP warnings) during test runsWhy
jest.unstable_mockModuleinstead ofjest.mock?The project uses
"type": "module"inpackage.json, which means all files are treated as ESM. The standardjest.mock()API relies on CommonJSrequire()under the hood, which does not exist in ESM and causes arequire is not definederror.jest.unstable_mockModule()is the ESM-compatible alternative.Additionally, the service is imported using a dynamic
await import()instead of a static import. This is necessary because static imports are hoisted to the top of the file by JavaScript, meaning they would run before the mocks are set up and load the real dependencies instead of the mocked ones.Test Cases
applyForShiftService(12 tests)Validation
shiftIduserIdis missinguserIdis invalidShift Lookup
Business Rules
openSuccess
applied, saves the shift, and logs the audit eventnull,undefined,'') from the applicants array before pushingapproveShiftService(13 tests)Validation
shiftIdguardIduser._idis missingShift Lookup
Authorization
Business Rules
assignedcompletedSuccess
assigned, and removes other applicants by defaultkeepOthers: trueis passedNotes
timeUtilsfunctions are mocked with real implementations so the overlap detection logic is genuinely exercised.