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

Error spying on sessionStorage #1368

Closed
vcmiranda opened this issue Mar 26, 2024 · 2 comments · Fixed by #1370
Closed

Error spying on sessionStorage #1368

vcmiranda opened this issue Mar 26, 2024 · 2 comments · Fixed by #1370
Assignees
Labels
bug Something isn't working

Comments

@vcmiranda
Copy link

vcmiranda commented Mar 26, 2024

Describe the bug
When using spyOn in sessionStorage (vitest), the following message is displayed: 'defineProperty' on proxy: trap returned falsish for property 'getItem'

To Reproduce
Sample code

export const useTestFunc = (
  initialValue: string,
  sessionKey: string
): { value: string } => {
  const value = window.sessionStorage.getItem(sessionKey) ?? initialValue;

  return {value};
};

it('should default to session state value when present', () => {
  const sessionKey = 'some:key';
  const getItemSpy = vi.spyOn(sessionStorage, 'getItem');
  sessionStorage.setItem(sessionKey, 'Test value');

  const {
    result: {
      current: {
        value
      },
    },
  } = renderHook(() => useTestFunc('default', sessionKey));

  expect(getItemSpy).toBeCalledWith(sessionKey);
  expect(value).toBe('Test value');
});
@vcmiranda vcmiranda added the bug Something isn't working label Mar 26, 2024
@capricorn86 capricorn86 self-assigned this Mar 26, 2024
capricorn86 added a commit that referenced this issue Mar 26, 2024
…nstorage

fix: [#1368] Fixes problem with spying on properties in Storage
@vcmiranda
Copy link
Author

That was fast. Thanks.

@sybers
Copy link

sybers commented Mar 28, 2024

Hey,
I'm trying to add a spy on localstorage.setItem and I'm facing the same issue TypeError: 'deleteProperty' on proxy: trap returned falsish for property 'setItem'.

I'm using @happy-dom/jest-environment@14.3.9.

Here's the test code:

jest.spyOn(localStorage, 'setItem').mockImplementationOnce(() => {
    throw new Error('error');
});

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants