Skip to content

Commit

Permalink
resolves deephaven#1369 - disableConsoleOutput test util
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Jun 12, 2023
1 parent bcce269 commit ee2eea5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/utils/src/TestUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ describe('asMock', () => {
expect(someFunc('a,b,c')).toEqual(3);
});

describe('disableConsoleOutput', () => {
it.each([
[['log']],
[['warn']],
[['error']],
[['info']],
[['debug']],
[['log', 'warn', 'error', 'info', 'debug']],
] as const)('should mock console method implementations: %s', methodNames => {
const arg0 = 'mock arg';

TestUtils.disableConsoleOutput(...methodNames);

methodNames.forEach(methodName => {
// eslint-disable-next-line no-console
console[methodName](arg0);

// eslint-disable-next-line no-console
expect(console[methodName]).toHaveBeenCalledWith(arg0);
});
});
});

describe('findLastCall', () => {
it('should return undefined if call not matched', () => {
const fn = jest.fn<void, [string, number]>();
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/src/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ class TestUtils {
fn: (...args: TArgs) => TResult
): jest.Mock<TResult, TArgs> => (fn as unknown) as jest.Mock<TResult, TArgs>;

/**
* Selectively disable logging methods on `console` object. Uses spyOn so that
* changes will be reverted after leaving the test scope that it is set in.
*/
static disableConsoleOutput = (
...methodNames: ('log' | 'warn' | 'error' | 'info' | 'debug')[]
): void => {
methodNames.forEach(methodName => {
jest.spyOn(console, methodName).mockImplementation();
});
};

/**
* Find the last mock function call matching a given predicate.
* @param fn jest.Mock function
Expand Down

0 comments on commit ee2eea5

Please sign in to comment.