-
Notifications
You must be signed in to change notification settings - Fork 2.7k
msal-react: only publishing as ESM breaks jest tests #6487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
it would be nice to have such a change also announced in the release notes. e.g. here https://github.com/AzureAD/microsoft-authentication-library-for-js/releases/tag/msal-react-v2.0.0 |
This issue requires attention from the MSAL.js team and has not seen activity in 5 days. @hectormmg please follow up. |
1 similar comment
This issue requires attention from the MSAL.js team and has not seen activity in 5 days. @hectormmg please follow up. |
This issue is affecting my projects as well. Any sense as to if/when it will be possible to resolve it? Thank you!! |
Marking as a feature, feel free to open a pull request if you need this resolved sooner rather than later. |
Does 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." |
hi, same issue on my project! |
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 |
you can mock it like this: 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. |
The fix won't work with Next.js which overwrites jest config.
|
I have been trying different things, still i am getting same issue
|
Still an issue for me
workaround: mocking the entire msal-react/msal-browser libraries. Extremely annoying. |
Issue in jest jestjs/jest#14805 |
Any updates on this? Looks like this issue blames Jest...and Jest blames this library. ...or does anyone have a good workaround? |
It’s a jest problem at heart. Moving to ESM is the right move long-term, jest just doesn’t support it well yet. As a workaround you can add it to your transformIgnorePatterns (eg transformIgnorePatterns: [“node_modules(?!(@azure/msal-react))“]) so that it is transformed. |
Still getting the error with that added. Is there maybe some other jest or tsconfig values that maybe I need to be aware of? Or maybe you can point me to a repo where this is already setup? TIA! |
I'm using:
In my case I had to do two things:
// jest.config.ts
const config: Config = {
// ...
preset: 'ts-jest/presets/js-with-ts',
// ...
transformIgnorePatterns: ['<rootDir>/node_modules/(?!@azure/msal-react)'],
// ...
} // tsconfig.json
{
"compilerOptions": {
// ...
"allowJs": true /* Required by ts-jest/presets/js-with-ts */
},
// ...
} I used this issue as reference: kulshekhar/ts-jest#970 |
What ended up working for me was writing the test to mock MSAL. This resolved my isssue
|
adding |
I'm using jest.mock('./index', () => {
const mockMsalInstance = {
getActiveAccount: jest.fn(),
acquireTokenSilent: jest.fn(),
initialize: jest.fn().mockResolvedValue(undefined),
getAllAccounts: jest.fn().mockReturnValue([]),
setActiveAccount: jest.fn(),
addEventCallback: jest.fn(),
};
return {
msalInstance: mockMsalInstance,
};
}); |
this work for me like a charm with my package.json config
|
You also probably don't need the line This is my configurations, I'm using the // jest.config.ts
import type { JestConfigWithTsJest } from 'ts-jest';
import { defaults as tsjPreset } from 'ts-jest/presets';
const config: JestConfigWithTsJest = {
roots: ['<rootDir>/src'],
setupFilesAfterEnv: ['<rootDir>/src/jest.setup.ts'],
testEnvironment: 'jsdom',
transform: {
...tsjPreset.transform,
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'<rootDir>/config/jest/fileTransform.js',
},
moduleNameMapper: {
'\\.(css|less)$': 'identity-obj-proxy',
},
};
export default config; |
Type resolution is broken for node16 resolution type due to several issues, this PR: - Updates all relative imports to include .js file extension, as required by node16 resolution - Includes type declaration files from lib folder in package publish - Adds package.json file to `lib` folder to indicate contents are commonjs - Updates package exports field to point to the appropriate type declaration files for ESM or CJS - Adds browser and node subpaths to msal-common export to separate node-only and browser-only features Fixes #6781 #6487 #6269 --------- Co-authored-by: Hector Morales <[email protected]>
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)
The text was updated successfully, but these errors were encountered: