Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
chai-enzyme delayed loading example
import chai from 'chai';
// This is not really a mock, but jest prevents out of scope
// variables unless prefixed with "mock".
const mockChai = chai;
let mockSetup = false;
// Here we're "mocking" enzyme, so that we can delay loading of
// the utility functions until emzyme is imported in tests.
jest.mock('enzyme', () => {
const actualEnzyme = require.requireActual('enzyme');
if (!mockSetup) {
const chaiEnzyme = require.requireActual('chai-enzyme');
if (typeof chaiEnzyme === 'function' && !mockSetup) {
mockSetup = true;
return actualEnzyme;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment