Trigger an ESLiint error if you leave a function in the code

ยท

1 min read

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()! ๐Ÿ˜Š"
      }
    ]
  }
};
ย