-
-
Notifications
You must be signed in to change notification settings - Fork 581
Closed
Labels
bugSomething isn't workingSomething isn't workingneeds:triageIssues that have not been investigated yet.Issues that have not been investigated yet.scope:nodeRelated to MSW running in NodeRelated to MSW running in Node
Description
Prerequisites
- I confirm my issue is not in the opened issues
- I confirm the Frequently Asked Questions didn't contain the answer to my issue
Environment check
- I'm using the latest
mswversion - I'm using Node.js version 20 or higher
Node.js version
25.0.0
Reproduction repository
n/a
Reproduction steps
Currently on Node.js 25.0.0 msw fails with the following error:
FAIL src/components/common/like_button.spec.tsx
● Test suite failed to run
TypeError: localStorage.getItem is not a function
> 1 | import { setupServer } from 'msw/node';
| ^
2 |
3 | import handlers from './handlers';
4 |
at CookieStore.getCookieStoreIndex (node_modules/msw/src/core/utils/cookieStore.ts:44:32)
at new CookieStore (node_modules/msw/src/core/utils/cookieStore.ts:25:34)
at Object.<anonymous> (node_modules/msw/lib/core/utils/cookieStore.js:90:21)
at Object.<anonymous> (node_modules/msw/src/core/utils/request/storeResponseCookies.ts:1:29)
at Object.<anonymous> (node_modules/msw/src/core/utils/handleRequest.ts:8:38)
at Object.<anonymous> (node_modules/msw/src/node/SetupServerCommonApi.ts:15:31)
at Object.require (mocks/server.ts:1:1)
at Object.require (jest.setup.ts:2:1)
(node:33156) Warning: `--localstorage-file` was provided without a valid path
(Use `node --trace-warnings ...` to show where the warning was created)
This error can be tracked down to these lines:
msw/src/core/utils/cookieStore.ts
Lines 39 to 43 in d4c287c
| if (typeof localStorage === 'undefined') { | |
| return {} | |
| } | |
| const cookiesString = localStorage.getItem(this.#storageKey) |
The problem is that in Node.js 25.0.0, localStorage is present in globalThis at all times, even if not set up properly using --localstorage-file. In such cases, an it will be an object, I believe, equal to Object.create(null).
The check in lines linked needs to be expanded to
if (typeof localStorage === 'undefined' || typeof localStorage.getItem !== 'function') {
and poof! The error is gone.
Current behavior
Fails on Node.js 25.0.0
Expected behavior
Should not fail on Node.js 25.0.0
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingneeds:triageIssues that have not been investigated yet.Issues that have not been investigated yet.scope:nodeRelated to MSW running in NodeRelated to MSW running in Node