Reputation: 3084
I am having trouble changing the behaviour of a mocked module in Jest. I want to mock different behaviours to test how my code will act under those differing circumstances. I don't know how to do this because calls to jest.mock()
are hoisted to the top of the file, so I can't just call jest.mock()
for each test. Is there a way to change the behaviour of a mocked module for one test?
jest.mock('the-package-to-mock', () => ({
methodToMock: jest.fn(() => console.log('Hello'))
}))
import * as theThingToTest from './toTest'
import * as types from './types'
it('Test A', () => {
expect(theThingToTest.someAction().type).toBe(types.SOME_TYPE)
})
it('Test B', () => {
// I need the-package-to-mock.methodToMock to behave differently here
expect(theThingToTest.someAction().type).toBe(types.OTHER_TYPE)
})
Internally, as you can imagine, theThingToTest.someAction()
imports and uses the-package-to-mock.methodToMock()
.
Upvotes: 141
Views: 135194
Reputation: 2664
Adding the answer that has worked for me.
It is built on top of three key ideas from Jest:
Using jest.requireActual
— since we want a method to be mocked conditionally for different tests, we need to ensure that our mocked method behaves by default as per its original behaviour.
Mocking using jest.fn()
— suggestions of using jest.SpyOn
won't work. The former is designed for functions, the latter is designed for objects.
Access mock using jest.mocked(methodToMock)
— since methodToMock
's import has been mocked, to access the mock for this method we use jest.mocked
.
import * as theThingToTest from './toTest';
import * as types from './types';
import { methodToMock } from 'the-package-to-mock';
jest.mock('the-package-to-mock', () => ({
// Ensure all default behaviour has been imported
...jest.requireActual('the-package-to-mock'),
// Mocked method defaults to original behaviour if no mock responses specified
methodToMock: jest.fn();
}))
// This test will run with default behaviour as if no mocking has been configured
it('Test A', () => {
expect(theThingToTest.someAction().type).toBe(types.SOME_TYPE)
})
// Override behaviour by configuring mock
it('Test B', () => {
jest.mocked(methodToMock).mockReturnValue('hello world');
expect(theThingToTest.someAction().type).toBe('hello world');
})
Upvotes: 1
Reputation: 110922
After you've mocked the module and replaced the methodToMock
with a spy, you need to import it. Then, at each test, you can change the behaviour of methodToMock
by calling the mockImplementation
spy method.
jest.mock('the-package-to-mock', () => ({
methodToMock: jest.fn()
}))
import { methodToMock } from 'the-package-to-mock'
it('Test A', () => {
methodToMock.mockImplementation(() => 'Value A')
// Test your function that uses the mocked package's function here.
})
it('Test B', () => {
methodToMock.mockImplementation(() => 'Value B')
// Test the code again, but now your function will get a different value when it calls the mocked package's function.
})
Upvotes: 239
Reputation: 417
import React from 'react';
import Component from 'component-to-mock';
jest.mock('component-to-mock', () => jest.fn());
describe('Sample test', () => {
it('first test', () => {
Component.mockImplementation(({ props }) => <div>first mock</div>);
});
it('second test', () => {
Component.mockImplementation(({ props }) => <div>second mock</div>);
});
});
Upvotes: -1
Reputation: 14823
In my scenario I tried to define the mock function outside of the jest.mock
which will return an error about trying to access the variable before it's defined. This is because modern Jest will hoist jest.mock
so that it can occur before imports. Unfortunately this leaves you with const
and let
not functioning as one would expect since the code hoists above your variable definition. Some folks say to use var
instead as it would become hoisted, but most linters will yell at you, so as to avoid that hack this is what I came up with:
This allows us to handle cases like new S3Client()
so that all new instances are mocked, but also while mocking out the implementation. You could likely use something like jest-mock-extended
here to fully mock out the implementation if you wanted, rather than explicitly define the mock.
This example will return the following error:
eferenceError: Cannot access 'getSignedUrlMock' before initialization
Test File
const sendMock = jest.fn()
const getSignedUrlMock = jest.fn().mockResolvedValue('signedUrl')
jest.mock('@aws-sdk/client-s3', () => {
return {
S3Client: jest.fn().mockImplementation(() => ({
send: sendMock.mockResolvedValue('file'),
})),
GetObjectCommand: jest.fn().mockImplementation(() => ({})),
}
})
jest.mock('@aws-sdk/s3-request-presigner', () => {
return {
getSignedUrl: getSignedUrlMock,
}
})
You must defer the call in a callback like so:
getSignedUrl: jest.fn().mockImplementation(() => getSignedUrlMock())
I don't want to leave anything up to the imagination, although I phaked the some-s3-consumer
from the actual project, but it's not too far off.
Test File
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'
import { SomeS3Consumer } from './some-s3-consumer'
const sendMock = jest.fn()
const getSignedUrlMock = jest.fn().mockResolvedValue('signedUrl')
jest.mock('@aws-sdk/client-s3', () => {
return {
S3Client: jest.fn().mockImplementation(() => ({
send: sendMock.mockResolvedValue('file'),
})),
GetObjectCommand: jest.fn().mockImplementation(() => ({})),
}
})
jest.mock('@aws-sdk/s3-request-presigner', () => {
return {
// This is weird due to hoisting shenanigans
getSignedUrl: jest.fn().mockImplementation(() => getSignedUrlMock()),
}
})
describe('S3Service', () => {
const service = new SomeS3Consumer()
describe('S3 Client Configuration', () => {
it('creates a new S3Client with expected region and credentials', () => {
expect(S3Client).toHaveBeenCalledWith({
region: 'AWS_REGION',
credentials: {
accessKeyId: 'AWS_ACCESS_KEY_ID',
secretAccessKey: 'AWS_SECRET_ACCESS_KEY',
},
})
})
})
describe('#fileExists', () => {
describe('file exists', () => {
it('returns true', () => {
expect(service.fileExists('bucket', 'key')).resolves.toBe(true)
})
it('calls S3Client.send with GetObjectCommand', async () => {
await service.fileExists('bucket', 'key')
expect(GetObjectCommand).toHaveBeenCalledWith({
Bucket: 'bucket',
Key: 'key',
})
})
})
describe('file does not exist', () => {
beforeEach(() => {
sendMock.mockRejectedValue(new Error('file does not exist'))
})
afterAll(() => {
sendMock.mockResolvedValue('file')
})
it('returns false', async () => {
const response = await service.fileExists('bucket', 'key')
expect(response).toBe(false)
})
})
})
describe('#getSignedUrl', () => {
it('calls GetObjectCommand with correct bucket and key', async () => {
await service.getSignedUrl('bucket', 'key')
expect(GetObjectCommand).toHaveBeenCalledWith({
Bucket: 'bucket',
Key: 'key',
})
})
describe('file exists', () => {
it('returns the signed url', async () => {
const response = await service.getSignedUrl('bucket', 'key')
expect(response).toEqual(ok('signedUrl'))
})
})
describe('file does not exist', () => {
beforeEach(() => {
getSignedUrlMock.mockRejectedValue('file does not exist')
})
afterAll(() => {
sendMock.mockResolvedValue('file')
})
it('returns an S3ErrorGettingSignedUrl with expected error message', async () => {
const response = await service.getSignedUrl('bucket', 'key')
expect(response.val).toStrictEqual(new S3ErrorGettingSignedUrl('file does not exist'))
})
})
})
})
Upvotes: 3
Reputation: 102297
Another way is to use jest.doMock(moduleName, factory, options).
E.g.
the-package-to-mock.ts
:
export function methodToMock() {
return 'real type';
}
toTest.ts
:
import { methodToMock } from './the-package-to-mock';
export function someAction() {
return {
type: methodToMock(),
};
}
toTest.spec.ts
:
describe('45006254', () => {
beforeEach(() => {
jest.resetModules();
});
it('test1', () => {
jest.doMock('./the-package-to-mock', () => ({
methodToMock: jest.fn(() => 'type A'),
}));
const theThingToTest = require('./toTest');
expect(theThingToTest.someAction().type).toBe('type A');
});
it('test2', () => {
jest.doMock('./the-package-to-mock', () => ({
methodToMock: jest.fn(() => 'type B'),
}));
const theThingToTest = require('./toTest');
expect(theThingToTest.someAction().type).toBe('type B');
});
});
unit test result:
PASS examples/45006254/toTest.spec.ts
45006254
✓ test1 (2016 ms)
✓ test2 (1 ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
toTest.ts | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 3.443 s
source code: https://github.com/mrdulin/jest-v26-codelab/tree/main/examples/45006254
Upvotes: 13
Reputation: 4082
Andreas answer work well with functions, here is what I figured out using it:
// You don't need to put import line after the mock.
import {supportWebGL2} from '../utils/supportWebGL';
// functions inside will be auto-mocked
jest.mock('../utils/supportWebGL');
const mocked_supportWebGL2 = supportWebGL2 as jest.MockedFunction<typeof supportWebGL2>;
// Make sure it return to default between tests.
beforeEach(() => {
// set the default
supportWebGL2.mockImplementation(() => true);
});
it('display help message if no webGL2 support', () => {
// only for one test
supportWebGL2.mockImplementation(() => false);
// ...
});
It won't work if your mocked module is not a function. I haven't been able to change the mock of an exported boolean for only one test :/. My advice, refactor to a function, or make another test file.
export const supportWebGL2 = /* () => */ !!window.WebGL2RenderingContext;
// This would give you: TypeError: mockImplementation is not a function
Upvotes: 0
Reputation: 5828
I use the following pattern:
'use strict'
const packageToMock = require('../path')
jest.mock('../path')
jest.mock('../../../../../../lib/dmp.db')
beforeEach(() => {
packageToMock.methodToMock.mockReset()
})
describe('test suite', () => {
test('test1', () => {
packageToMock.methodToMock.mockResolvedValue('some value')
expect(theThingToTest.someAction().type).toBe(types.SOME_TYPE)
})
test('test2', () => {
packageToMock.methodToMock.mockResolvedValue('another value')
expect(theThingToTest.someAction().type).toBe(types.OTHER_TYPE)
})
})
Explanation:
You mock the class you are trying to use on test suite level, make sure the mock is reset before each test and for every test you use mockResolveValue to describe what will be return when mock is returned
Upvotes: 19
Reputation: 4528
spyOn
worked best for us. See previous answer:
https://stackoverflow.com/a/54361996/1708297
Upvotes: 6