-
Override Object Properties to Mock with Monkey-patching in JavaScript
$ node 01-override-object-props-to-mock-with-monkey-patching.no-framework.js # or $ npx jest 01We import both
thumb-warandutils, and mock oututils.getWinnerso that we can evaluate other aspects ofthumb-war. We are makingutils.getWinnerdeterministic, as the randomness of what it usually returns will affect the outcomes of our tests in non-deterministic ways.After mocking, one should always clean up so that subsequent tests are not affected by the mock.
-
Ensure Functions are Called Correctly with JavaScript Mocks
$ node 02-ensure-functions-are-called-correctly-with-javascript-mocks.no-framework.js # or $ npx jest 02We can create a naive
jest.fnwhich takes a mock implementation, and accepts all arguments passed to it.Onto this mock function we can add a
mockobject where we can store calls to the mock function.This can all be done using
jest.fn.Jest allows one to inspect a mocked function using
toHaveBeenCalledWith,toHaveBeenNthCalledWith, or to inspectmockFn.mock.callsto inspect how the function was used for all calls. -
Restore the Original Implementation of a Mocked JavaScript Function with jest.spyOn
$ npx jest 03 # or $ node 03-restore-original-implementation-of-a-mocked-function.no-framework.jsAn alternative to using jest.fn to replace a function with a mock implementation is to use
jest.spyOnto create a mock implementation. This mitigates us having to store the original function and replace it, as Jest will do the heavy lifting for us.- Use
jest.spyOn(myObject, 'myObjectFn') - Use
myObject.myObjectFn.mockImplementation(mockFn)to create a mock implementation ofmyObjectFn - Once done testing the implementation, use
myObject.myObjectFn.mockRestore()to replace the mock implementation with the original so that other tests are not affected..mockRestore()restores the mock implementation to its pre-mocked definition
- Use
-
Mock a JavaScript module in a test
$ npx jest 04 # or $ node 04-mock-a-js-module-in-a-test.no-framework.jsUsing
jest.spyOnis still a form of monkey-patching. Monkey-patching is great for our own modules, because we're using commonjs'srequire. When working in an es environment we'll have to mock an entire module, andjest.mockallows one to do this.When using
jest.mockwe can create the mock implementation inside of the mock, and no longer need to usejest.spyOnand[fnToMock].mockImplementation.Instead of using
mock.mockRestore()to restore the mock implementation, we now usemock.mockReset().mock.mockRestore()is only available for mocks created withjest.spyOnjest.mockcan be placed anywhere in the file, and Jest will hoist it to the top of the file.
jest.mocktakes control of the entire module requiring system. This can be simulated usingrequire.cache.require.cacheis an object that has keys for each import, with each key being associated with an object containining information regarding the import.We use the
exportsproperty to create the mock implementation of the module -
Make a shared JavaScript mock module
$ npx jest 05 # or $ node 05-make-a-shared-mock-module.no-framework.jsWe can have tests automatically use the mock of a module by adding the mock to a
__mocks__folder. Node modules can be placed in theroot__mocks__that Jest inspects, by default adjacent tonode_modules. For user-definedmodulesthey can be placed in a__mocks__folder adjacent to the module. The mock must use the same filename as the mocked module.For user-defined modules, if the test imports the module, then
jest.mock('./path/to/mock')must be added to the test file.
Similarly to how we simulated what Jest is doing when conrolling module requiring by using
require.cache. We create a file containing the mock, which also uses ourfnto allow us to evaluate calls. In our test we import our mock, and then retrieve the cached paths for the actual utils and mock utils, rewritingrequire.cache's key for the actual utils with the mocked utils.
03-javascript-mocking-fundamentals
Directory actions
More options
Directory actions
More options
03-javascript-mocking-fundamentals
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||