Week 5 - Session 2 - Test Doubles & jest.fn()
blog image source |
A test double is an object that can stand in for a real object in a test (think stunt doubles), or where you replace a production object for testing purposes. Common types of Test Doubles:
Jest Mock Functions - https://facebook.github.io/jest/docs/en/mock-functions.html
The Test Double Rule of Thumb - http://engineering.pivotal.io/post/the-test-double-rule-of-thumb/
- Stubs: mock with some pre-programmed behaviour, object with given properties, function with given return value.
- Mocks: passed into a function and used to make an assertion in the test.
- Spies: record interactions with other objects, test if a function has been called (how many times and with what parameters), useful for testing functions with side-effects.
- Dummy: act as a placeholder - passed around, but never used in a test.
Jest Mock Functions - https://facebook.github.io/jest/docs/en/mock-functions.html
The Test Double Rule of Thumb - http://engineering.pivotal.io/post/the-test-double-rule-of-thumb/
Test Doubles - Fakes, Mocks and Stubs - https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da