next js - ReferenceError: window is not defined
See original GitHub issue🐛 Bug Report
Attempting to run npm run test on first setup for an app and getting below error on getServerSideProps:
ReferenceError: window is not defined
18 |
19 | export default async function getBrands() {
> 20 | const brands = await apiQl(queryQl, variables);
| ^
21 | return brands;
22 | }
23 |
at XMLHttpRequestOverride.send (node_modules/@mswjs/interceptors/src/interceptors/XMLHttpRequest/XMLHttpRequestOverride.ts:237:33) at dispatchXhrRequest (node_modules/axios/lib/adapters/xhr.js:177:13) at xhrAdapter (node_modules/axios/lib/adapters/xhr.js:13:10) at dispatchRequest (node_modules/axios/lib/core/dispatchRequest.js:52:10) at getBrands (src/lib/getBrands.js:20:20) at getServerSideProps (src/pages/data/index.js:282:20) at Object.executeAsIfOnServer (node_modules/next-page-tester/dist/server.js:33:16) at Object.fetchPageData [as default] (node_modules/next-page-tester/dist/fetchData/fetchPageData.js:95:30) at Object.fetchRouteData (node_modules/next-page-tester/dist/fetchData/fetchRouteData.js:13:22) at Object.getPageInfo (node_modules/next-page-tester/dist/page/getPageInfo.js:20:22) at getPage (node_modules/next-page-tester/dist/getPage.js:66:38) at Object.<anonymous> (tests/pages/data/index.test.js:59:24)
The getServerSideProps call it is complaining about is pretty straight forward:
/src/pages/data/index.js
export async function getServerSideProps() {
const brands = await getBrands();
return {
props: {
brands: brands.data.brands,
},
};
}
Reproduction
See this repository
Expected behavior
Should run the test.
Jest patch
Have you patched Jest as described here?
- [ X] yes
- no
I tried applying the patch but got an error. Note that I am using "jest": "^27.0.6". I understand the patch applies to jest v.26
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS 11.5
CPU: (8) x64 Apple M1
Binaries:
Node: 14.17.3 - /usr/local/bin/node
npm: 7.19.1 - /usr/local/bin/npm
npmPackages:
jest: ^27.0.6 => 27.0.6
Issue Analytics
- State:
- Created 2 years ago
- Comments:6

Top Related StackOverflow Question
@thepuzzlemaster Thanks a lot for having shared this; it saved me 😃
In case it’s helpful for others in the future, I ran into this issue as well. I was also using msw-js to mock a back-end server, that my app uses axios to make calls to. For me at least, the issue was traced back to having a relative or malformed url in my api request.
I was calling
axios.get(myUrl). However,myUrlwas built like this:And in my tests, the
env.BACKEND_URLwas evaluating to an empty string, as my .env.local file doesn’t seem to get read before the tests run as I didn’t notice thedotenvFileoption.Well, to test things out, I hardcoded things to look like this:
And the window is not defined error went away.
This was the offending code in the mswjs library. As you can see, it only tries to read from window, if
this.urltriggers an error. XMLHttpRequestOverrideHope this is helpful to someone in the future.