Skip to content

Instantly share code, notes, and snippets.

@acatl
Last active January 5, 2023 17:50
Show Gist options
  • Save acatl/965865aae86cf135f1cfc621a4a86fcc to your computer and use it in GitHub Desktop.
Save acatl/965865aae86cf135f1cfc621a4a86fcc to your computer and use it in GitHub Desktop.
jest mock debug.js module
/* eslint-env jest */
const mockDebug = jest.fn()
jest.mock('debug', () => {
return () => mockDebug
})
const foo = require('./foo')
describe('foo.bar', () => {
it('should testName', () => {
mockDebug.mockClear()
mockDebug.mockReset()
foo.bar()
expect(mockDebug).toBeCalled()
expect(mockDebug.mock.calls).toMatchSnapshot()
})
})
const debug = require('debug')('foo')
function bar () {
debug('hello world')
}
module.exports = {
bar
}
@skypesky
Copy link

skypesky commented Nov 9, 2022

exmaple: typescript + jest + mock debug.js module

declare global {
  var mockDebug: jest.Mock<any, any>;
}

jest.mock('debug', () => {
  global.mockDebug = jest.fn();
  return () => global.mockDebug;
});

// usage
describe('xxx', () => {
	test('xxx', () => {
		expect(global.mockDebug.mock.calls.toString()).toContain('ccc');
	})
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment