question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jsdom update 5 days ago breaks back compatability

See original GitHub issue

🐛 Bug Report

jest.spyOn(localStorage, “setItem”); doesn’t work;

jest-environment-jsdom has caret dependency on jsdom -> 11.5.1, jsdom latest version is 11.12.0 and it includes localStorage and sessionStorage. Previously we could mock localStorage easily but now we don’t have that option and we need to use the default localStorage implementation. So our option is to spy on it which doesn’t work because it throws TypeError: object[methodName].mockImplementation is not a function

What I have discovered is that in ./node_modules/jsdom/lib/jsdom/living/generated/Storage.js the issue is with the set method of the Proxy

Currently the code below is on line 278.

if (ownDesc === undefined) {
          const parent = Reflect.getPrototypeOf(target);
          if (parent !== null) {
            return Reflect.set(parent, P, V, receiver);
          }
          ownDesc = { writable: true, enumerable: true, configurable: true, value: undefined };
        }

if we remove receiver from return Reflect.set(parent, P, V, receiver); we will be able to spy on it. But I guess that’s coming from webidl converter

To Reproduce

Steps to reproduce the behavior:

  1. Install jest.
  2. Write a simple class that leverage localStorage.setItem() or localStorage.getItem()
  3. Try to spy on it -> jest.spyOn(localStorage, “setItem”); and it will throw an error

Expected behavior

The method should be available for spying.

Link to repl or repo (highly encouraged)

https://github.com/vlad0/jest-localstorage-issue

Run npx envinfo --preset jest

  1. npm install
  2. npm test Paste the results here:
> jest

 FAIL  ./service.spec.js
  Service
    ✕ Service set value (7ms)

  ● Service › Service set value

    TypeError: object[methodName].mockImplementation is not a function

       8 |
       9 |     it('Service set value', () => {
    > 10 |         jest.spyOn(localStorage, "setItem");
      11 |         service.setValue("hello", "world")
      12 |
      13 |         expect(localStorage.setItem).toHaveBeenCalled();

      at ModuleMockerClass.spyOn (node_modules/jest-mock/build/index.js:597:26)
      at Object.it (service.spec.js:10:14)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.785s, estimated 1s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:22
  • Comments:23 (1 by maintainers)

github_iconTop GitHub Comments

48reactions
mr-wildcardcommented, Aug 14, 2018

We found out that localStorage from Jest is an instance of a class called Storage provided by jsdom. And using spyOn this way works :

jest.spyOn(Storage.prototype, 'setItem');
24reactions
omalrajcommented, Nov 22, 2018

jest.spyOn(window.localStorage.__proto__, 'setItem'); should work without any additional imports.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest 28: Shedding weight and improving compatibility
Main breaking changes likely to impact your migration are dropped support for Node 10 and 15 (but not Node 12, which will be...
Read more >
jest-environment-jsdom | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
How to wait until an element exists? - javascript - Stack Overflow
They don't consider @hughsk's awesome input fully, the compatibility argument. Meanwhile I'd recommend simply using Brandon's update on Ryan's ...
Read more >
jsdom - npm
A JavaScript implementation of many web standards. Latest version: 20.0.3, last published: a month ago. Start using jsdom in your project by ...
Read more >
Official jQuery Blog | New Wave Javascript
3 and it is the only functional change in this release. Upgrading. We do not expect compatibility issues when upgrading from a jQuery...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found