Skip to content

Instantly share code, notes, and snippets.

@chrisdc
Created September 24, 2018 10:31
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 chrisdc/806366baeee51be4f3580c6cc3c23ec2 to your computer and use it in GitHub Desktop.
Save chrisdc/806366baeee51be4f3580c6cc3c23ec2 to your computer and use it in GitHub Desktop.
Mocking globals in Jest.
function logMessage(message) {
console.log(message);
}
module.exports = logMessage
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