describe('API mapping module', () => { // Runs before all tests, used to prepare environment for the current describe module, e.g., global database beforeAll(() => {
})
// Runs before each test, used to prepare operations for each case (it), e.g., reset server app beforeEach(() => {
})
// Runs after each test, used to clean up variables for each case (it), e.g., reset all module caches afterEach(() => { jest.resetModules() })
// Runs after all tests, used to clean up environment, e.g., clean up "environment preparations" generated for tests afterAll(() => {
})
// Note: All four methods above support returning a Promise, in which case Jest will wait for the Promise to resolve before continuing
it('When env is the default development environment, returns localhost address', async() => { process.env.NODE_ENV = ''
constAPI = require('lib/api')
expect(API).toThrow() // Expect API to throw expect(API('')).toMatch(/localhost/) // Expect return to contain 'localhost' })
it.only('When env is test environment, returns test environment address', async() => { // Only execute this test case, commonly used for debugging process.env.NODE_ENV = 'test'
.toBe(value) // Expect value to be value .toEqual(value) // Expect two objects to be deeply equal .toBeDefined() // Expect to be defined .toBeFalsy() // Expect to be Falsy .toBeTruthy() // Expect to be Truthy .toMatch() // Expect to match, supports strings and regex .toThrow() // Expect to throw
.toHaveBeenCalled() // Method was called .toHaveBeenCalledWith(arg1, arg2, ...) // Method was called with arguments arg1, arg2, ... .toHaveBeenCalledTimes(number) // Method was called number times
// All expect statements above can be negated: not.toBe()
module.exports = (a, b) => { debug('value a: ', a) debug('value b: ', b)
return a + b }
...
// number-add.test.js // Mock the debug module, so each require returns an auto-generated mock instance jest.mock('debug') ... it('Returns the sum of a and b', () => { const add = require('utils/number-add') const total = add(1, 2)
// Method 2 describe('Testing string-add-async module 2', () => { it('Returns concatenation of strings from API a and API b', async () => { // Mock node-fetch module, so each require returns a mock instance jest.mock('node-fetch')
Note: Method 3 is strongly discouraged as it has a broad impact scope, though it’s suitable for “shielding external effects” scenarios
Mock Instances
When a module is mocked, it returns a mock instance with rich methods for further mocking; it also provides rich properties for assertions
mockImplementation(fn) where fn is the implementation of the mocked module
mockImplementationOnce(fn) Similar to 1, but only effective once. Can be chained so each mock returns differently
mockReturnValue(value) Directly define a mock module’s return value
mockReturnValueOnce(value) Directly define a mock module’s return value (one-time)
mock.calls Call property, e.g., a mock function fun called twice: fun(arg1, arg2); fun(arg3, arg4);, then mock.calls is [['arg1', 'arg2'], ['arg3', 'arg4']]