I'm using mocha to test my node.js application.
I notice that my spec files getting bigger and bigger over time. Are there any pattern to organize the test files (e.g. one specs file per test)? Or are there other frameworks on top of mocha to help me structure the tests? Or do you prefere other test frameworks for that reason?
You can have more than one describe per file. describe and context are synonyms and can be used interchangeably.
Spec files are plain-text files that are used to construct spec strings. They consist of a sequence of directives separated by blank lines. The type of directive is determined by the first non-whitespace character on the line, which can be one of the following: % command.
Test files are located in cypress/e2e by default, but can be configured to another directory. Test files may be written as: .js. .jsx.
Large test/spec files tend to mean the code under test might be doing too much. This is not always the case though, often your test code will always out weigh the code under test, but if you are finding them hard to manage this might be a sign.
I tend to group tests based on functionality. Imagine if we have example.js
, I would expect example.tests.js
to begin with.
Rather than one spec called ExampleSpec
I tend to have many specs/tests based around different contexts. For example I might have EmptyExample
, ErrorExample
, and DefaultExample
which have different pre-condidtions. If these become too large, you either have missing abstractions, or should then think about splitting the files out. So you could end up with a directory structure such as:
specs/ Example/ EmptyExample.js ErrorExample.js DefaultExample.js
To begin with though, one test/spec file per production file should be the starting point. Only separate if needs be.
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