Last active
June 1, 2017 19:42
-
-
Save gdborton/ec0fe3fbae082fe9c67ccfb54f326530 to your computer and use it in GitHub Desktop.
chai-enzyme delayed loading example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
mockChai.use(chaiEnzyme()); | |
} | |
} | |
return actualEnzyme; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment