Why are Jest test naming conventions the way they are?
__tests__
, with the underscores?-test.js
as opposed to just something.js
?Test class naming convention usually follows the naming convention of the class being tested, e.g., the class under test is “Order” the corresponding test class will be “OrderTests“. When in doubt, it is a good practice to model the test class name after the production class it is testing.
Test name should be presented as a statement or fact of life that expresses workflows and outputs. Test name could include the name of the tested method or class.
BeforeEach in Jest is part of the setup and teardown process. As the name suggests, if we want to run a function or some other code repeatedly “before each” test that code can be put in the beforeEach function.
Underscores and double underscores are commonly used to denote things which are outside of the regular code. For example, Python uses them to prefix object properties which are private or system-controlled.
In this sense, your tests folder isn't part of your app, it's something which sits alongside it. To stretch a hypothetical example, imagine you were writing an app to control science experiments. You might want a folder called, legitimately, tests
. That would be arguably insane, the point is that __tests__
makes it clear that the tests are not part of the main line of code.
The -test
suffix is just for test discovery. Lots of frameworks use similar prefix or suffix conventions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With