Monday, January 18, 2021

Jest (just) in Five Minutes

  • What is Jest?
    • Jest is a JavaScript testing framework maintained by Facebook, Inc.
  • Any easy tip to remember the syntactical steps of what you do when you write a test?
    • DIE Matchers (Describe the Test(It) that Expects the result to Match the expectation)
  • How does Jest supports mocking?
    • jest.fn - allows you to mock a function. All mock functions have this '.mock' property.
    • jest.mock - allows you to mock a module (module is the JavaScript module that was exported using module.exports or export default)
    • jest.spyOn - allows you to mock either the whole module or the individual functions of the module. At its most general usage, it can be used to track calls on a method (read this nice article: https://blog.echobind.com/how-to-mock-using-jest-spyon-d13d57a8434d)
    • NOTE: you can't spy something that doesn't exist on the object.
  • How does the mock functions help you in the testing?
    • Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function (and the parameters passed in those calls), capturing instances of constructor functions when instantiated with new, and allowing test-time configuration of return values. Read more: https://jestjs.io/docs/en/mock-functions

What if I try here to cover a little bit on Jasmine?

  • How does Jasmine supports mocking?
    • jasmine.createSpy - to mock a function
    • jasmine.createSpyObj - to mock a module
    • jasmine.spyOn - to mock the individual functions of the module



No comments:

Post a Comment