Trigger an ESLiint error if you leave a function in the code
ESLint can give an error if you leave a console.log
or console.error
in your codebase.
We can do the same with any other function!
Example: suppress console
const suppressConsoleWarnAndError = () => {
const consoleErrorSpy = jest.spyOn(console, 'error');
const consoleWarnSpy = jest.spyOn(console, 'warn');
consoleErrorSpy.mockImplementation(() => {});
consoleWarnSpy.mockImplementation(() => {});
};
Very useful when writing/debugging tests (so the console isn't cluttered)
Now if we forget to remove this line in our test, our CI/CD will complain ๐
I added this to my ESLint
module.exports = {
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "CallExpression[callee.name='suppressConsoleWarnAndError']",
"message": "Please remember to remove suppressConsoleWarnAndError()! ๐"
}
]
}
};
ย