Asserting values of window object using jest

849 Views Asked by At

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?

1

There are 1 best solutions below

1
Jose Rojas On

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