Skip to content

Instantly share code, notes, and snippets.

@gdborton
Last active June 1, 2017 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdborton/ec0fe3fbae082fe9c67ccfb54f326530 to your computer and use it in GitHub Desktop.
Save gdborton/ec0fe3fbae082fe9c67ccfb54f326530 to your computer and use it in GitHub Desktop.
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;
mockChai.use(chaiEnzyme());
}
}
return actualEnzyme;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment