简体   繁体   中英

Create manually triggerable Eslint rule

On the project that I work on, I want to add a comment which will make the linter to detect error and fail the build.

One use case for this is that sometimes I replace real endpoints with mock endpoints, for example:

axios.get(MOCK_URL)
  .then(function (response) {

It would be useful if I can add Eslint comment here like this to fail the build, so the mock URL won't get deloyed:

axios.get(MOCK_URL) // Eslint: fail
  .then(function (response) {

I think you're asking the wrong question:)

Mocks should be used in tests, not in production code, so "by definition" such a case should never happen, unless you're testing manually your production code by modifying some of the code only for testing reasons, and then modify it back.

If that's what you're doing I encourage you to stop doing it: it is error-prone as well as mundane task that repeats itself. Write proper tests instead, tests which can be run automatically upon any commit using a pre-commit hook .

As for replacing real endpoints with mock endpoints, you can use libraries like sinon to do it dynamically inside your tests!

UPDATE (after reading your comment):

Sounds like what you need is different environments : you're testing in prod instead of having a development/staging environment.

If/once you do have multiple environment you can set the behavior of the application according to the environment it's deployed in, so instead of:

axios.get(MOCK_URL).then(function (response)...

you can do something like:

axios.get(config.url.env).then(function (response)

and set your configuration to hold url per environment, eg

... url: {
      staging: mock_url,
      production: url, 
    }
 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM