How can I mock the JavaScript window object using Jest?

I need to test a function which opens a new tab in the browser

openStatementsReport(contactIds) {
  window.open(`a_url_${contactIds}`);
}

I would like to mock the window’s open function so I can verify the correct URL is passed in to the open function.

Using Jest, I don’t know how to mock the window. I tried to set window.open with a mock function but this way doesn’t work. Below is the test case

it('correct url is called', () => {
  window.open = jest.fn();
  statementService.openStatementsReport(111);
  expect(window.open).toBeCalled();
});

But it gives me the error

expect(jest.fn())[.not].toBeCalled()

jest.fn() value must be a mock function or spy.
    Received:
      function: [Function anonymous]

What should I do to the test case?

244 thoughts on “How can I mock the JavaScript window object using Jest?”

  1. you are actually a good webmaster. The web site loading velocity is incredible.
    It sort of feels that you’re doing any distinctive trick.

    Moreover, The contents are masterwork. you’ve done a excellent process in this topic!

    Reply
  2. Saya telah telah berselancar online lebih dari 4 jam hari ini, namun saya tidak pernah menemukan artikel menarik seperti
    milik Anda. It’s cukup berharga bagi saya. Secara
    pribadi , jika semua pemilik situs web dan blogger membuat konten yang baik seperti yang
    Anda lakukan, internet akan menjadi a jauh lebih berguna dari sebelumnya.

    My web blog … Joker http www joker123

    Reply
  3. Hey! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m
    not seeing very good results. If you know of any please share.
    Thanks!

    my web-site lpe88 original

    Reply
  4. I’m really enjoying the theme/design of your weblog.
    Do you ever run into any internet browser compatibility issues?
    A few of my blog audience have complained about my blog not working correctly in Explorer
    but looks great in Firefox. Do you have any recommendations to
    help fix this problem?

    Feel free to visit my web site: live22 ios download 2021 (https://xe88sg.com/live22)

    Reply
  5. Heya i am for the primary time here. I found this board and I to find It
    really useful & it helped me out a lot. I’m hoping to offer one thing again and help
    others such as you helped me.

    Also visit my blog Marcia

    Reply
  6. I wanted to visit and let you know how great I treasured discovering your website today.
    I will consider it a real honor to do things at my company and be able to make use of the tips provided
    on your blog and also get involved in visitors’ remarks like
    this. Should a position regarding guest article writer become on offer at your end, remember to let me know.

    Here is my web blog; Xoth Keto BHB

    Reply
  7. Thank you for sharing superb informations. Your website is so cool.
    I’m impressed by the details that you’ve on this web site.
    It reveals how nicely you understand this
    subject. Bookmarked this website page, will come back for more articles.
    You, my friend, ROCK! I found just the information I already searched
    everywhere and just could not come across. What an ideal web-site.

    Here is my web blog – KetoRedux

    Reply
  8. Hey there! I just wanted to ask if you ever have any issues with
    hackers? My last blog (wordpress) was hacked and I ended
    up losing many months of hard work due to no back up.
    Do you have any methods to stop hackers?

    Reply
  9. Howdy! I know this is kinda off topic but I was wondering which blog platform are you using for this site?
    I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m looking at options for
    another platform. I would be awesome if you could point
    me in the direction of a good platform.

    Here is my website Franchesca

    Reply
  10. Hi there would you mind stating which blog platform you’re using?
    I’m going to start my own blog in the near future but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and I’m looking for something unique.
    P.S My apologies for getting off-topic but I had to ask!

    My blog post :: Libido Boost

    Reply
  11. Hello, I think your site might be having browser compatibility issues.
    When I look at your blog site in Safari, it looks fine but when opening
    in Internet Explorer, it has some overlapping. I just wanted to give you
    a quick heads up! Other then that, wonderful blog!

    Reply
  12. Hmm it looks like your website ate my first comment (it was
    extremely long) so I guess I’ll just sum it up what I wrote and say,
    I’m thoroughly enjoying your blog. I as well am an aspiring
    blog writer but I’m still new to everything.
    Do you have any helpful hints for beginner blog writers?
    I’d definitely appreciate it.

    Reply
  13. Do you mind if I quote a couple of your posts as long as I provide
    credit and sources back to your website? My website is in the exact same niche as yours
    and my users would truly benefit from some of the information you present
    here. Please let me know if this alright with you. Many thanks!

    Reply
  14. Oh my goodness! Impressive article dude! Thank you so much, However
    I am going through troubles with your RSS.

    I don’t understand why I cannot join it. Is there anybody getting the same RSS issues?
    Anybody who knows the solution can you kindly respond?
    Thanx!!

    Reply
  15. Hey there! I know this is somewhat off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form?

    I’m using the same blog platform as yours and I’m
    having difficulty finding one? Thanks a lot!

    Reply
  16. Pingback: buy stromectol 6mg
  17. You really make it appear really easy together with your presentation but I to find this topic to be actually something that I feel I would by no means understand. It kind of feels too complex and very large for me. I am having a look ahead in your subsequent post, I will try to get the grasp of it!

    Reply
  18. Undeniably believe that which you said. Your favorite justification appeared to be on the net the simplest thing to be aware of. I say to you, I definitely get irked while people think about worries that they just don’t know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people can take a signal. Will likely be back to get more. Thanks

    Reply
  19. fantastic post, very informative. I wonder why the other experts of this sector don’t notice this. You must continue your writing. I am confident, you have a huge readers’ base already!

    Reply
  20. Wonderful work! This is the type of information that should be shared around the web. Shame on Google for not positioning this post higher! Come on over and visit my web site . Thanks =)

    Reply
  21. Pingback: clomid usa
  22. Pingback: generic stromectol
  23. My coder is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for about a year and am nervous about switching to another platform. I have heard very good things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any help would be really appreciated!

    Reply
  24. You really make it seem so easy with your presentation but I find this topic to be actually something which I think I would never understand. It seems too complex and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

    Reply
  25. This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your great post. Also, I have shared your web site in my social networks!

    Reply
  26. I just like the valuable info you provide to your articles.
    I’ll bookmark your blog and take a look at once more here regularly.
    I’m quite certain I’ll be informed many new stuff proper here!
    Good luck for the next!

    Reply
  27. I was recommended this blog by my cousin. I am not sure whether this
    post is written by him as nobody else know such detailed about my
    difficulty. You are amazing! Thanks!

    Reply
  28. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your
    point. You clearly know what youre talking about, why throw away
    your intelligence on just posting videos to
    your blog when you could be giving us something enlightening to read?

    Reply
  29. Hello There. I discovered your weblog using msn. This is a really neatly written article.
    I’ll make sure to bookmark it and return to read more of your
    helpful information. Thank you for the post.

    I will definitely return.

    Reply
  30. I’ve been browsing on-line more than 3 hours
    today, yet I never discovered any interesting article like yours.

    It is beautiful price enough for me. In my opinion, if all web owners
    and bloggers made excellent content as you did, the net will probably be much more helpful than ever before.

    Reply
  31. Hi there, just became aware of your blog through Google,
    and found that it is really informative. I am gonna watch out for brussels.
    I will be grateful if you continue this in future.
    Many people will be benefited from your writing. Cheers!

    Reply
  32. I’m extremely impressed along with your writing abilities as well as with the layout in your weblog.
    Is this a paid theme or did you customize it your self?

    Anyway keep up the excellent quality writing,
    it’s uncommon to see a great blog like this one today..

    Reply
  33. Hey there! I’m at work browsing your blog from my new apple iphone!
    Just wanted to say I love reading through your blog and look forward to all
    your posts! Keep up the excellent work!

    Reply
  34. This design is spectacular! You most certainly know how to keep a reader entertained.

    Between your wit and your videos, I was almost moved to
    start my own blog (well, almost…HaHa!) Great job.

    I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

    Reply
  35. I absolutely love your blog and find many of your post’s to be exactly I’m looking for.
    Would you offer guest writers to write content in your case?
    I wouldn’t mind creating a post or elaborating on a few of the subjects you
    write related to here. Again, awesome web log!

    Reply
  36. Greetings I am so delighted I found your web site, I
    really found you by error, while I was browsing
    on Digg for something else, Nonetheless I am here now and would just like to
    say cheers for a marvelous post and a all round entertaining blog (I
    also love the theme/design), I don’t have time to read it all at the moment but I have bookmarked it
    and also added your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the great
    b.

    Reply
  37. I like the valuable information you provide in your articles.
    I’ll bookmark your blog and check again here frequently.

    I am quite certain I’ll learn lots of new stuff right here!
    Good luck for the next!

    Reply
  38. Does your site have a contact page? I’m having a tough time locating it
    but, I’d like to send you an email. I’ve got some suggestions for your blog you might be
    interested in hearing. Either way, great website and I look
    forward to seeing it develop over time.

    Reply
  39. I was pretty pleased to find this great site.

    I want to to thank you for ones time for this particularly fantastic read!!
    I definitely loved every part of it and i also have you saved
    to fav to see new stuff on your web site.

    Reply

Leave a Comment