1

I was trying to spyOn sessionStorage object in jasmine. I've written the following code to achieve spying on real sessionStorage

   fakeStore = {
      employee: {
        id: 123456,
        emailId: 'admin@domain.com',
        role: 'admin'
      }
    };

    fakeStorage = {
      getItem: (key: string): string => fake Store[key]
    };

spyOn(sessionStorage, 'getItem').and.callFake(fakeStorage.getItem);

But after encountering the error sessionStorage is undefined
I suddenly realised that I'm executing the jasmine tests on NodeJs environment rather than browser.
Since there is not such thing called sessionStorage in NodeJs global context test case failed.

I have to either run this test alone in browser or completely ignore.
Is there any way I can simulate browser's global window context as NodeJs's global

I've tried to add sessionStorage into global of nodejs,but typescript didn't let me....

How to solve this ?

Sai Surya Kattamuri
  • 1,046
  • 12
  • 22
  • the question is not about how to mock localStorage,instead it is how to run browser related unit tests(which has dependency related to browser's objects such as window,location etc) on node.js – Sai Surya Kattamuri Jan 07 '18 at 11:26

1 Answers1

3

try this

spyOn(window.sessionStorage, 'getItem').and.callFake(()=>{})
jwpfox
  • 5,124
  • 11
  • 45
  • 42