How to get localStorage working in vue testing How to get localStorage working in vue testing vue.js vue.js

How to get localStorage working in vue testing


jsdom-global is using an old version of jsdom. jsdom has supported localStorage since 11.12.0.

To use jsdom 11.12+ with localStorage support, you can add jsdom window properties to the global scope yourself in a test setup file that runs before your tests:

/* setup.js */const { JSDOM } = require('jsdom');const jsdom = new JSDOM('<!doctype html><html><body></body></html>');const { window } = jsdom;function copyProps(src, target) {  Object.defineProperties(target, {    ...Object.getOwnPropertyDescriptors(src),    ...Object.getOwnPropertyDescriptors(target),  });}global.window = window;global.document = window.document;global.navigator = {  userAgent: 'node.js',};global.requestAnimationFrame = function (callback) {  return setTimeout(callback, 0);};global.cancelAnimationFrame = function (id) {  clearTimeout(id);};copyProps(window, global);