Skip to content
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

msal-browser throws error when running unit tests in Jest #1840

Closed
3 of 13 tasks
nick-romano0 opened this issue Jun 26, 2020 · 4 comments
Closed
3 of 13 tasks

msal-browser throws error when running unit tests in Jest #1840

nick-romano0 opened this issue Jun 26, 2020 · 4 comments
Assignees
Labels
bug A problem that needs to be fixed for the feature to function as intended. msal-browser Related to msal-browser package

Comments

@nick-romano0
Copy link

Please follow the issue template below. Failure to do so will result in a delay in answering your question.

Library

  • msal@1.x.x or @azure/msal@1.x.x
  • @azure/msal-browser@2.0.0-beta.3
  • @azure/msal-angular@0.x.x
  • @azure/msal-angular@1.x.x
  • @azure/msal-angularjs@1.x.x

Important: Please fill in your exact version number above, e.g. msal@1.1.3.

Framework

React 16.13.0
create-react-app
jest

Description

new msal-browser package throws error when running unit tests in jes.

Error Message

 Test suite failed to run

    BrowserAuthError: crypto_nonexistent: The crypto object or function is not available. Detail:Browser crypto or msCrypto object not available.

      42 | };
      43 | 
    > 44 | export const msalApp = new PublicClientApplication({
         |                        ^
      45 |     auth: {
      46 |         clientId: "58a18e2e-f3cb-4c6d-b07f-77703e6a215b",
      47 |         authority: "https://login.microsoftonline.com/b36c7ed3-4a9c-4ba9-bdf9-9db03fcbd392",

      at BrowserAuthError.AuthError [as constructor] (node_modules/@azure/msal-common/dist/index.es.js:109:24)

Security

  • Is this issue security related?

Regression

  • Did this behavior work before?
    Version:
    Yes, msal@^1.3.0

MSAL Configuration

// Provide configuration values here.
// For Azure B2C issues, please include your policies.
{
    auth: {
        clientId: "<>",
        authority: "<>",
        validateAuthority: true,
        redirectUri: window.location.origin
    },
    cache: {
        cacheLocation: "localStorage",
        storeAuthStateInCookie: false
    },
    system: {
        navigateFrameWait: 0
    }
}

Reproduction steps

export const msalApp = new PublicClientApplication({
    auth: {
        clientId: "<>",
        authority: "<>",
        validateAuthority: true,
        redirectUri: window.location.origin
    },
    cache: {
        cacheLocation: "localStorage",
        storeAuthStateInCookie: false
    },
    system: {
        navigateFrameWait: 0
    }
});
...

Expected behavior

Browsers/Environment

  • Chrome
  • Firefox
  • Edge
  • Safari
  • IE
  • Other (Node / jest)
@nick-romano0 nick-romano0 added the bug A problem that needs to be fixed for the feature to function as intended. label Jun 26, 2020
@jmckennon
Copy link
Contributor

jmckennon commented Jun 26, 2020

If you're upgrading from 1.3.0, we never used any of the crypto objects/APIs in that version and previous. It's possible that you need to mock the instantiation of the crypto objects in your tests now after not having to do so before. This can be seen in PublicClientApplication.

If this does not work or if it's a separate problem entirely, please let us know and we can advise further (or potentially push some fixes).

@jmckennon jmckennon self-assigned this Jun 26, 2020
@jmckennon jmckennon added the msal-browser Related to msal-browser package label Jun 26, 2020
@pkanher617
Copy link
Contributor

pkanher617 commented Jun 26, 2020

@nick-romano-kci Specifically, you will need to mock the window.crypto object. See here. You can also try mocking the BrowserCrypto APIs themselves.

@jasonnutter jasonnutter changed the title msal-browser msal-browser throws error when running unit tests in Jest Jul 1, 2020
@jmckennon
Copy link
Contributor

Closing this as we're assuming the problem is solved due to inactivity. Please open a new issue if you have further questions. Thanks!

@ArtemSkliar
Copy link

To solve this problem the following jest config settings helped me

module.exports = {
  globals: {
    crypto: require('crypto')
  }
};

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug A problem that needs to be fixed for the feature to function as intended. msal-browser Related to msal-browser package
Projects
None yet
Development

No branches or pull requests

4 participants