Jest + Node + SharePoint + MSAL = Error: Cross origin http://localhost forbidden

Sebastian Rogers
1 min readApr 8, 2021

--

TL;DR Add * @jest-environment node to the Jest file JSDoc.

If you are getting the following errors when you try and use MSAL with JEST to connect to SharePoint:

Error: Cross origin http://localhost forbidden

Or

ClientAuthError: network_error: Network request failed. Please check network trace to determine root cause. | Fetch client threw: Error: Network Error | Attempted to reach: https://login.microsoftonline.com/[tenant_id_obfuscarated]/oauth2/v2.0/token

Then this is caused by Jest running by default in a browser like environment see Configuring Jest · Jest (jestjs.io) it needs to be set to use node.

You can set this in your jest.config.js or directly in the header

The key here is the line:

* @jest-environment node

You can also do this in your jest.config.js if it applies to all tests.

The rest of it is an example of how you would do MSAL for SharePoint based on: NodeJS Auth — PnP/PnPjs

--

--

Sebastian Rogers

Technical Director for Simple Innovations Ltd. First paid for code in 1980, but still has all his own hair.