launchdarkly / jest-launchdarkly-mock

Easily unit test LaunchDarkly feature flagged components with jest
Apache License 2.0
25 stars 19 forks source link

mockUseFlags should return an empty object #55

Closed hjoelh closed 1 year ago

hjoelh commented 1 year ago

Describe the bug mockUseFlags returns undefined, this breaks application code as the expected type of useFlags is a function that returns LDFlagSet type (empty object/object of string keys and values of any).

This has the unfortunate effect of breaking any unmocked tests that include a useFlags call, which would without this package added to jest setup pass/run fine as useFlags will just return {} with a property access returning undefined (essentially flag is false/off if handled with a fasly check). When this package is added it will break any unmocked useFlags calls.

To reproduce Fork this https://replit.com/@hjoelh/launchdarklybugtest?v=1 run npm run test , you should see a fail go to jest.config.js comment out "jest-launchdarkly-mock" run npm run test, you should see pass with someFlag logging as undefined

Expected behavior When adding "jest-launchdarkly-mock" to jest setup, mockUseFlags should return a function that returns an empty object so that application code behaves as expected.

Way to fix I think: set mockUseFlags.mockImplementation(() => ({})); after initialising mockUseFlags ensure this mockImplementation is also set after mockUseFlags.mockReset() call, (I am assuming mockReset will make mockUseFlags undefined again)

SDK version See replit

Language version, developer tools See replit

OS/platform See replit

Additional context Please let me know if I have got this wrong/misunderstood, otherwise I am happy to open a PR.

Thanks! 😀

hjoelh commented 1 year ago

@yusinto Does this make sense? Could I open a PR?

louis-launchdarkly commented 1 year ago

Hello @hjoelh, sorry for the late reply. The description sounds reason, so if you can, it will be a great help to Open a PR for this.

yusinto commented 1 year ago

This has been fixed in #57 (thank you @hjoelh for the pr!). A new version will be released soon which will include this fix. Stay tuned for an update.

yusinto commented 1 year ago

This has been fixed in 2.1.0.