microsoft-authentication-library-for-js: msal-react: only publishing as ESM breaks jest tests
Core Library
MSAL.js (@azure/msal-browser)
Core Library Version
3.1.0
Wrapper Library
MSAL React (@azure/msal-react)
Wrapper Library Version
2.0.3
Public or Confidential Client?
Public
Description
The latest major release of msal-react is only published as ESM. Currently jest is not able to test code that contains ESM dependencies. Before msal-react V2 we tested our components together with msal-react and only mocked some functions of it. Now with msal-react V2 we have to mock the complete library as jest is unable to load any code from it.
I can see that you are still building other msal libraries as commonjs+ESM (e.g. msal-browser) could you also add a commonjs build to msal-react so it continous to work with jest.
Thank you!
Error Message
No response
Msal Logs
No response
MSAL Configuration
{}
Relevant Code Snippets
-
Reproduction Steps
Expected Behavior
Identity Provider
Azure AD / MSA
Browsers Affected (Select all that apply)
None (Server)
Regression
msal-react 1.5.11
Source
External (Customer)
About this issue
- Original URL
- State: open
- Created 9 months ago
- Reactions: 10
- Comments: 17 (2 by maintainers)
you can mock it like this:
jest.mock('@azure/msal-react', () => ({ useMsal: jest.fn(), }));
EDIT: since I see a few downvotes I would like to explain that my suggestion is not a solution to the issue. I shows how @jansepke and I currently work around it by mocking the entire msal library until the issue is fixed. Keeping this workaround is not desirable.
I’m using create-react-app and I was able to resolve the Jest issue by adding the following into my package.json:
If you’re not using create-react-app you should be able to add this into your jest.config.js file.
Docs: https://jestjs.io/docs/configuration#transformignorepatterns-arraystring
The fix won’t work with Next.js which overwrites jest config.
@azure/msal-react
to your newly vendored moduleDoes anyone have insights into why this problem is impacting my UAT pipeline while it doesn’t seem to have the same effect on my local machine? Also, is there a possible solution or workaround for this issue within a React project?
“Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/@azure/msal-react/dist/index.js from /app/dist/server/assets/all.page.9a5890d9.js not supported.”