0

I am trying to assert the values of windows object using jest. For example: Asserting google tag manager's window.dataLayer values.

How can I implement that using unit testing jest framework?

Modekai
  • 1
  • 2

1 Answers1

0

If you want to call a object which you will find in window.dataLayer object, just mock the function you will call:

window.dataLayer = jest.fn(() => { 
     push: jest.fn(),
});

before you test your functionality.

In case you want to use in multiple test, you can set in beforeAll

Jose Rojas
  • 3,490
  • 3
  • 26
  • 40
  • But here is my issue. I don't want to mock it, because I want to see and assert the actual values in the `window.dataLayer`. that is the purpose of the test. Right now the window object in my test file has different values than the window object in my actual file. Since `window.dataLayer` is for google tag manager, the `dataLayer` property doesn't even exist in the `window` object inside test file. How do I import and test the actual window object to the test file? to see it's `dataLayer` actual values? – Modekai Sep 30 '21 at 22:27