msw: ReferenceError: location is not defined in Node.js

Prerequisites

Environment check

  • I’m using the latest msw version
  • I’m using Node.js version 14 or higher

Node.js version

18.14.2

Reproduction repository

https://github.com/till/msw-location-error

Reproduction steps

npm install
./node_modules/.bin/vitest

Current behavior

 FAIL  tests/request.test.js > request
ReferenceError: location is not defined
 ❯ FetchInterceptor.<anonymous> node_modules/@mswjs/interceptors/src/interceptors/fetch/index.ts:50:22
 ❯ step node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:59:23
 ❯ Object.next node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:40:53
 ❯ fulfilled node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:31:58

Expected behavior

I can mock the request

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 4
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve run into a possibly related issue: When running in Jest, Jest shuts down its jsdom environment as part of test cleanup, so an in-progress mocked fetch request may access location and fail (even with the recent fix, because jsdom’s location uses a getter that won’t work after Jest shuts it down).

See vercel/swr#2463.

I’m not sure if this is MSW’s responsibility to fix, and (as you said) it looks like it won’t be an issue in later versions of @mswjs/interceptors, but I wanted to mention it in case it’s relevant or in case anyone else runs across it.

I am also running into this, and finding it hard to create a consistent reproduction case; as it works locally, but fails in GitHub actions, CI/CD land.

I can confirm I am on 0.17.9:

yarn why @mswjs/interceptors
├─ msw@npm:1.2.1
│  └─ @mswjs/interceptors@npm:0.17.9 (via npm:^0.17.5)
│
└─ msw@npm:1.2.1 [d16b5]
   └─ @mswjs/interceptors@npm:0.17.9 (via npm:^0.17.5)

error observed:

2023-04-28T20:16:00.4884197Z TypeError: Cannot read properties of null (reading '_location')
2023-04-28T20:16:00.4885320Z     at Window.get location [as location] (/home/runner/work/foo-bar/foo-bar/node_modules/jsdom/lib/jsdom/browser/Window.js:376:79)
2023-04-28T20:16:00.4886171Z     at FetchInterceptor.<anonymous> (/home/runner/work/foo-bar/foo-bar/node_modules/@mswjs/interceptors/src/interceptors/fetch/index.ts:50:22)
2023-04-28T20:16:00.4887156Z     at step (/home/runner/work/foo-bar/foo-bar/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:59:23)
2023-04-28T20:16:00.4887959Z     at Object.next (/home/runner/work/foo-bar/foo-bar/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:40:53)
2023-04-28T20:16:00.4888916Z     at fulfilled (/home/runner/work/foo-bar/foo-bar/node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:31:58)
2023-04-28T20:16:00.4889408Z     at runNextTicks (node:internal/process/task_queues:60:5)
2023-04-28T20:16:00.4889800Z     at processImmediate (node:internal/timers:447:9)

unfortunately upgrading to the latest @mswjs/interceptors does not work. Happy to create a new issue if one does not already exist, as I think this case is slightly different than the original #1555 one.

I ran into this issue and could not solve it on pnpm only by running the install again. It did not update the pnpm-lock so I had to override the msw>@mswjs/interceptors to the latest version, installed and then removed the override and it installed again, the correct version this time. Don’t ask me why ¯_(ツ)_/¯

@joshkel, this has been fixed by @till and is published already. Re-install your dependencies and you will get the fix automatically. To verify:

npm ls @mswjs/interceptors

Should print 0.17.8.

@till, that would be awesome! Are you comfortable with branching changes from a specific moment of Git history? As I’ve mentioned, the line we have to fix is not in the latest state of the Interceptors repo. It’s nothing too complex though:

  1. Clone the @mswjs/interceptors repo.
  2. Checkout the latest tag we need to fix: git checkout chore/0.17.7
  3. Create a new branch: git checkout -b fix/fetch-location
  4. Implement the fix.
  5. Open a pull request from fix/fetch-location to chore/0.17.7

I think you need my help with chore/0.17.7 to open a PR to that branch on the original repo and not on your fork.

Edit: I’ve pushed chore/0.17.7 to the origin so you should be able to go from there and open a pull request to that intermediate branch.

I can try to upgrade it? 🥳