Skip to content

Instantly share code, notes, and snippets.

@nerdic-coder
Created April 17, 2018 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdic-coder/ab1738348f762fd8e2bb263f5fdd4ce2 to your computer and use it in GitHub Desktop.
Save nerdic-coder/ab1738348f762fd8e2bb263f5fdd4ce2 to your computer and use it in GitHub Desktop.
Example of jestGlobalMocks.ts
const mock = () => {
let storage = {};
return {
getItem: key => key in storage ? storage[key] : null,
setItem: (key, value) => storage[key] = value || '',
removeItem: key => delete storage[key],
clear: () => storage = {},
};
};
Object.defineProperty(window, 'localStorage', {value: mock()});
Object.defineProperty(window, 'sessionStorage', {value: mock()});
Object.defineProperty(window, 'getComputedStyle', {
value: () => ['-webkit-appearance']
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment