Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document mock error #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,50 @@ test('should not be impacted by the previous test', () => {
});
```

If needing to change the mock implementation of `localStorage` methods, please use jest's
[mockImplementationOnce](https://jestjs.io/docs/en/mock-function-api#mockfnmockimplementationoncefn).
Using other methods may result in the current method mock being permanently overwritten.

```js
// does NOT work
test('should override the setItem mock and reset it to its previous implementation', () => {
const KEY = 'foo',
VALUE = 'bar';

localStorage.setItem.mockImplementation(() => {
throw 'Something went wrong!'
})
expect(() => {
localStorage.setItem(KEY, VALUE)
}).toThrow('Something went wrong!')

localStorage.setItem.mockClear();
expect(() => {
localStorage.setItem(KEY, VALUE)
}).not.toThrow('Something went wrong!')
});

// does work
test('should override the setItem mock and reset it to its previous implementation', () => {
const KEY = 'foo',
VALUE = 'bar';

localStorage.setItem.mockImplementationOnce(() => {
throw 'Something went wrong!'
})
expect(() => {
localStorage.setItem(KEY, VALUE)
}).toThrow('Something went wrong!')

localStorage.setItem.mockClear();
expect(() => {
localStorage.setItem(KEY, VALUE)
}).not.toThrow('Something went wrong!')

expect(localStorage.getItem(KEY)).toEqual(VALUE)
});

```

See the [contributing guide](./CONTRIBUTING.md) for details on how you can
contribute.
2 changes: 1 addition & 1 deletion __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('storage', () =>
[localStorage, sessionStorage].map(storage => {
[localStorage, sessionStorage].map((storage) => {
// https://html.spec.whatwg.org/multipage/webstorage.html#storage
beforeEach(() => {
storage.clear();
Expand Down
18 changes: 8 additions & 10 deletions __tests__/setup.test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import { LocalStorage } from '../src/localstorage';

describe('setup', () => {
const orignalImpGlobsl = {};
const orignalImpGlobals = {};

const setupGloabls = (restore = false) => {
const setupGlobals = (restore = false) => {
[
'_localStorage',
'localStorage',
'_sessionStorage',
'sessionStorage',
].forEach(globalKey => {
].forEach((globalKey) => {
if (restore) {
delete global[globalKey];
global[globalKey] = orignalImpGlobsl[globalKey];
global[globalKey] = orignalImpGlobals[globalKey];
} else {
orignalImpGlobsl[globalKey] = global[globalKey];
orignalImpGlobals[globalKey] = global[globalKey];
delete global[globalKey];
}
});
};

const restoreGlobals = () => setupGloabls(true);
const restoreGlobals = () => setupGlobals(true);

beforeEach(() => {
setupGloabls();
setupGlobals();
jest.resetModuleRegistry();
});

afterEach(() => {
restoreGlobals();
});

['_localStorage', '_sessionStorage'].forEach(gKey => {
['_localStorage', '_sessionStorage'].forEach((gKey) => {
it(`[${gKey}] should define a property on the global object with writable false`, () => {
require('../src/setup');
expect(global[gKey.replace('_', '')].constructor.name).toBe(
Expand Down
8 changes: 4 additions & 4 deletions src/localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export class LocalStorage {
constructor(jest) {
Object.defineProperty(this, 'getItem', {
enumerable: false,
value: jest.fn(key => this[key] || null),
value: jest.fn((key) => this[key] || null),
});
Object.defineProperty(this, 'setItem', {
enumerable: false,
Expand All @@ -13,14 +13,14 @@ export class LocalStorage {
});
Object.defineProperty(this, 'removeItem', {
enumerable: false,
value: jest.fn(key => {
value: jest.fn((key) => {
delete this[key];
}),
});
Object.defineProperty(this, 'clear', {
enumerable: false,
value: jest.fn(() => {
Object.keys(this).map(key => delete this[key]);
Object.keys(this).map((key) => delete this[key]);
}),
});
Object.defineProperty(this, 'toString', {
Expand All @@ -31,7 +31,7 @@ export class LocalStorage {
});
Object.defineProperty(this, 'key', {
enumerable: false,
value: jest.fn(idx => Object.keys(this)[idx] || null),
value: jest.fn((idx) => Object.keys(this)[idx] || null),
});
} // end constructor

Expand Down