Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jasmine.createSpy equivalent in jest

I am migrating from jasmine to jest in my application. I have the following line to test

JSON.parse(window.document.querySelector(SELECTOR).innerHTML)

In my test I used jasmine.

document.querySelector = jasmine.createSpy('HTML Element').and.returnValue(dummyEl)

But now with jest I get the following error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: Cannot read property 'innerHTML' of null

Can you help me?

>Solution :

I think you need a combination of jest.fn and .mockReturnValue.

I am not good with jest but I think it can be:

document.querySelector = jest.fn().mockReturnValue(dummyEl);

Check out the documentation here:

https://jestjs.io/docs/jest-object#jestfnimplementation

https://jestjs.io/docs/mock-function-api#mockfnmockimplementationfn

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading