javascript - MockSpy sessionStorage in jasmine when executing tests in Nodejs and not Browser - Stack Overflow

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

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: '[email protected]',
        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 pletely 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 ?

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: '[email protected]',
        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 pletely 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 ?

Share Improve this question edited Jan 7, 2018 at 11:27 Sai Surya Kattamuri asked Nov 17, 2017 at 13:07 Sai Surya KattamuriSai Surya Kattamuri 1,08613 silver badges23 bronze badges 1
  • 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 Commented Jan 7, 2018 at 11:26
Add a ment  | 

1 Answer 1

Reset to default 3

try this

spyOn(window.sessionStorage, 'getItem').and.callFake(()=>{})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745295729a4621129.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信