Skip to content

Too simple check for localStorage causes msw to crash on Node.js 25 #2615

@wojtekmaj

Description

@wojtekmaj

Prerequisites

Environment check

  • I'm using the latest msw version
  • 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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds:triageIssues that have not been investigated yet.scope:nodeRelated to MSW running in Node

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions