Skip to content

Instantly share code, notes, and snippets.

@forivall
Created August 16, 2019 23:32
Show Gist options
  • Save forivall/7be567da5cd566d9980a06524c30c6e9 to your computer and use it in GitHub Desktop.
Save forivall/7be567da5cd566d9980a06524c30c6e9 to your computer and use it in GitHub Desktop.
Mocking debug() calls to ensure that portions of code are/aren't called when testing with jest
const targetDebuggerNs = 'my-debug-namespace';
jest.mock('debug', () => {
// tslint:disable-next-line: no-shadowed-variable
const D = jest.requireActual('debug') as typeof import('debug');
const mocks: jest.Mock[] = [];
const m = jest.fn((ns) => {
if (ns !== targetDebuggerNs) {
return D(ns);
}
const debugInner = D(ns);
const debugFn = Object.assign(jest.fn(debugInner), debugInner);
mocks.push(debugFn);
return debugFn.mockName(`debug('${ns}')`);
});
return Object.assign(m, D, {
default: m,
debug: m,
mocks,
});
});
const getDebugFn = (ns: string = targetDebuggerNs) => {
const debugFn = ((D as any).mocks as IDebugger[]).find(
(d) => d.namespace === ns
);
expect(debugFn).toHaveProperty('mock');
return debugFn;
};
import D, { IDebugger } from 'debug';
// Test implementation goes here.
// usage is like `expect(getDebugFn()).not.toHaveBeenCalled()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment