Created
September 24, 2018 10:31
-
-
Save chrisdc/806366baeee51be4f3580c6cc3c23ec2 to your computer and use it in GitHub Desktop.
Mocking globals in Jest.
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
function logMessage(message) { | |
console.log(message); | |
} | |
module.exports = logMessage |
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
const logMessage = require('./log-message.js'); | |
const console = global.console; | |
test('Should call console.log', () => { | |
const logMock = jest.fn(); | |
Object.defineProperty(global.console, 'log', { | |
value: logMock | |
}); | |
logMessage('Hello World!'); | |
expect(logMock).toBeCalledWith('Hello World!'); | |
}); | |
afterEach(() => { | |
global.console = console; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment