How to structure node.js mocha tests

I am very new to unit testing in node.js, I want to know what is the best practice of writing unit testing in node.js, for example, using the 'it' method, how many statements I can check are there any standard for writing only one test case in one method. Please give me an idea to write a unit test case. Thanks in advance.:)

+4
source share
2 answers

Test one piece of functionality in one call to it () and use only a few statements if it is really necessary.

If you use 2 statements in one call to it (), failure of the first will block the execution of the second of them, thus hiding some of your tests and, therefore, prevent you from getting a complete picture of a possible error.

Learn how to use before / after and before each / after each inside the described block - this will really help you only perform tests on small parts of your code in each of them (). See the Hooks chapter in the mocha documentation .

If necessary, create your own set of helper functions to prepare your code for one test, to prevent (too much) code duplication in your tests. I believe that code duplication in tests is as bad as code duplication in your real one. ”

+1

Chai Mocha .

Mocha - , Chai - . , ( it() ), .

, , . :

  • Mocha - ( , , ..),
  • Unit.js - , (, ), script. ( , rnning)

mocha.js unit.js (. ).

0

Source: https://habr.com/ru/post/1612018/


All Articles