Reputation: 20668
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?
Upvotes: 6
Views: 2396
Reputation: 15709
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.
Upvotes: 1