Skip to content

Commit a851fd7

Browse files
committed
test: hideConsoleMessages
HCRC-178
1 parent 718e9e6 commit a851fd7

2 files changed

Lines changed: 47 additions & 54 deletions

File tree

jest-setup.ts

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,14 @@ import '@testing-library/jest-dom';
22
import { toHaveNoViolations } from 'jest-axe';
33
import fetchMock from 'jest-fetch-mock';
44
import { loadErrorMessages, loadDevMessages } from '@apollo/client/dev';
5-
6-
function ignoreConsoleWarnings() {
7-
// Store the original console.warn
8-
const originalWarn = console.warn;
9-
10-
// A list of warning messages to completely ignore
11-
const warningsToIgnore = [
12-
'[%s]: `%s` is deprecated and will be removed in Apollo Client 4.0. %s cache.diff canonizeResults Please remove this option.',
13-
];
14-
15-
// Override console.warn
16-
console.warn = (...args) => {
17-
// Check if the warning message includes any of our ignored strings
18-
const shouldIgnore = warningsToIgnore.some((ignoredMsg) =>
19-
args.join(' ').includes(ignoredMsg),
20-
);
21-
22-
// If it's one we want to ignore, do nothing (return early)
23-
if (shouldIgnore) {
24-
return;
25-
}
26-
27-
// For all other warnings, call the original console.warn
28-
originalWarn.apply(console, args);
29-
};
30-
}
31-
32-
function ignoreConsoleErrors() {
33-
// Store the original console.error
34-
const originalError = console.error;
35-
36-
// A list of error messages to completely ignore
37-
const errorsToIgnore = [
38-
'Error: Could not parse CSS stylesheet', // JSDOM cannot understand stylesheets generated by HDS-React.
39-
];
40-
41-
// Override console.warn
42-
console.error = (...args) => {
43-
// Check if the error message includes any of our ignored strings
44-
const shouldIgnore = errorsToIgnore.some((ignoredMsg) =>
45-
args.join(' ').includes(ignoredMsg),
46-
);
47-
48-
// If it's one we want to ignore, do nothing (return early)
49-
if (shouldIgnore) {
50-
return;
51-
}
52-
// For all other warnings, call the original console.warn
53-
originalError.apply(console, args);
54-
};
55-
}
56-
57-
ignoreConsoleWarnings();
58-
ignoreConsoleErrors();
5+
import { hideConsoleMessages } from './src/tests/hideConsoleMessages';
6+
7+
hideConsoleMessages({
8+
warn: [
9+
/`canonizeResults` is deprecated and will be removed in Apollo Client 4.0. Please remove this option./,
10+
],
11+
error: [/Could not parse CSS stylesheet/],
12+
});
5913

6014
fetchMock.enableMocks();
6115

src/tests/hideConsoleMessages.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import util from 'node:util';
2+
3+
export const consoleLevels = ['debug', 'info', 'log', 'warn', 'error'] as const;
4+
export type ConsoleLevel = (typeof consoleLevels)[number];
5+
6+
/**
7+
* Set up console method overrides to hide the given console messages.
8+
* @param consoleMessagesToHide - An object mapping console levels to arrays of RegExp patterns or strings.
9+
* Each pattern or strings represents a message to be hidden for that console level.
10+
* If a string is given, any message that includes that string will be hidden.
11+
* If a RegExp is given, any message that matches that RegExp will be hidden.
12+
* If no patterns are provided for a level, no messages will be hidden for that level.
13+
*/
14+
export const hideConsoleMessages = (
15+
consoleMessagesToHide: Partial<Record<ConsoleLevel, (RegExp | string)[]>>,
16+
) => {
17+
// eslint-disable-next-line no-restricted-syntax
18+
for (const consoleLevel of consoleLevels) {
19+
const hidablePatterns = consoleMessagesToHide[consoleLevel];
20+
if (hidablePatterns && hidablePatterns.length > 0) {
21+
// eslint-disable-next-line no-console
22+
const originalConsoleMethod = console[consoleLevel];
23+
// eslint-disable-next-line no-console, func-names
24+
console[consoleLevel] = function (msg, ...optionalParams) {
25+
const formattedMsg = util.format(msg, ...optionalParams);
26+
const shouldHide = hidablePatterns.some((pattern) =>
27+
typeof pattern === 'string'
28+
? formattedMsg.includes(pattern)
29+
: pattern.test(formattedMsg),
30+
);
31+
if (shouldHide) {
32+
return;
33+
}
34+
// eslint-disable-next-line consistent-return
35+
return originalConsoleMethod(msg, ...optionalParams);
36+
};
37+
}
38+
}
39+
};

0 commit comments

Comments
 (0)