How to make `it.only` environment-aware using mocha?

北城余情 提交于 2019-12-25 12:34:37

问题


Mocha supports it.only syntax to reduce the amount of tests to run. In our team, a developer might use it.only temporally within a code base.

Now it happened that such an only test was approved and deployed by accident. This led the build server to happily run only one test and to declare that build a success.

My question now becomes: Is there a way to tell mocha:

It should only allow only on a developer's machine? Can it be make environment aware so that either all tests always run on a build machine or an only test case would also declare the job a failure.

(I know that there are different ways to reduce the amount of tests run by mocha. E.g. within WebStorm one can run a subset of tests within a project without changing code. The scope of the question is to allow for the fact that a developer might use it.only and they shall be free to do so. I want to detect if such a change might sneak its way into the codebase though.)


回答1:


On your CI, you should run mocha with the forbid-only flag.

--forbid-only    causes test marked with only to fail the suite

Depending on your setup it may look like:

mocha --require babel-register --recursive tests/unit --forbid-only

A developer still may chose to run the test suite without the flag.



来源:https://stackoverflow.com/questions/46911882/how-to-make-it-only-environment-aware-using-mocha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!