mock-local-storage icon indicating copy to clipboard operation
mock-local-storage copied to clipboard

localStorage is not defined 1.1.8

Open a-vilmin opened this issue 6 years ago • 8 comments

Hey I'm using mock-local-storage for testing with mocha through the Vue CLI 3 and am getting undefined localStorage issues. I added the --require jsdom-global --require mock-local-storage lines to the vue-cli-service test:unit command in package.json, yet I can't access the localStorage object in my tests. Not sure if I'm missing something here.

a-vilmin avatar Feb 05 '19 20:02 a-vilmin

Me too I then tried to import 'mock-local-storage', still nothing when I printed out window, sessionStorage is empty Object.

I changed sessionStorage to localStorage and now I am have other errors, but I need session Storage.
More later

greenpdx avatar Mar 25 '19 19:03 greenpdx

Same problem here, guys. Unfortunately, no solution found yet.

stas-kh avatar Jun 04 '19 11:06 stas-kh

Maybe you can mock a localStorage by yourself.....sorry for my poor English. But as vue-test-utils' doc says, 'Note the tests are run inside Node.js with browser environment simulated with JSDOM.', we can mount a mocked localStorage on global. In fact, JSDOM is based on a mocked global. I succeeded with the following code, and I hope this can help you:

// localStorage.js
global.localStorage = {
    data: {},
    getItem(key) {
        return this.data[key];
    },
    setItem(key, value) {
        this.data[key] = value;
    },
    removeItem(key) {
        delete this.data[key];
    }
};
// Test.spec.js
import "./localStorage";
// ...do more

And it works. It can also work when using TypeScript.

HermitSun avatar Jun 25 '19 10:06 HermitSun

@HermitSun Thank you :)

andrefox333 avatar Sep 09 '19 14:09 andrefox333

I had to adjust the nice snippet by @HermitSun to get it working properly. For example, the real localStorage.getItem returns null if the item is not found where the snippet's getItem returns undefined. Also I had to define the window object and correct code style to fit my environment (Node v10, StandardJS, and Tape tests). See the improved version below:

// mock-local-storage.js
global.window = {}

global.window.localStorage = {
  data: {},
  getItem: function (key) {
    const val = this.data[key]
    if (val) {
      return val
    }
    return null
  },
  setItem: function (key, value) {
    this.data[key] = value
  },
  removeItem: function (key) {
    delete this.data[key]
  }
}
// in the beginning of test.js
require('./mock-local-storage')

axelpale avatar May 23 '20 11:05 axelpale

I'm new in testing and I was wondering how to access localStorage using Vue test utils. After reading the answers here I found I was able to use localStorage just by adding global in front, instead of window, without mocking it. Am I doing wrong?

hey guys I was having the same problem . i just did this and made it to work on .mocharc.json

`"require":["@babel/register","ignore-styles","mock-local-storage","jsdom-global/register"]`

just import mock-local-storage before jsdom

Arrow66 avatar Nov 26 '20 16:11 Arrow66

I had to adjust the nice snippet by @HermitSun to get it working properly. For example, the real localStorage.getItem returns null if the item is not found where the snippet's getItem returns undefined. Also I had to define the window object and correct code style to fit my environment (Node v10, StandardJS, and Tape tests). See the improved version below:

// mock-local-storage.js
global.window = {}

global.window.localStorage = {
  data: {},
  getItem: function (key) {
    const val = this.data[key]
    if (val) {
      return val
    }
    return null
  },
  setItem: function (key, value) {
    this.data[key] = value
  },
  removeItem: function (key) {
    delete this.data[key]
  }
}
// in the beginning of test.js
require('./mock-local-storage')

Yeah, this project started from something like this. See original stackoverflow topic for some other nuances.

Initial idea was just to wrap this code into npm package to avoid reinventing the wheel and to have it collectively tested and fixed.

As I said in README, we (github project creators) don't use this code ourselves currently and have no time to invest into it. I occasionally merge incoming PRs and let Travis to build and deploy package. So, if someone want to see issues fixed, you are welcome to fork, fix, change docs etc and send a PR. Of course, you can always fork it and maintain a better version of it.

Also, if you were able to solve some issue with some config / workaround, please don't hesitate to put it into README page for others to find it quickly. Just send me a PR and I'll merge it. @Arrow66, if you think you comment deserve to be put on the front page, please edit it. Put it under the "Mocha" header or add "Troubleshooting" sub header or something.

Please also signal me if issue can be closed.

Thanks!

nikolay-turpitko avatar Jan 17 '21 05:01 nikolay-turpitko