Categories
JavaScript Answers

How to mock object for document element with Jasmine and JavaScript?

Spread the love

To mock object for document element with Jasmine and JavaScript, we use the createSpy method.

For instance, we write

const dummyElement = document.createElement("div");
document.getElementById = jasmine
  .createSpy("HTML Element")
  .and.returnValue(dummyElement);

to call createSpy to create a spy and make it return a value with returnValue.

And we set that as the value of document.getElementById.

Conclusion

To mock object for document element with Jasmine and JavaScript, we use the createSpy method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *