Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize my spec files?

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?

like image 942
Aron Woost Avatar asked Aug 29 '12 10:08

Aron Woost


People also ask

Can we have multiple describe in one spec file?

You can have more than one describe per file. describe and context are synonyms and can be used interchangeably.

What is a spec file?

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.

Where the test files are located into project structure in Cypress?

Test files are located in cypress/e2e by default, but can be configured to another directory. Test files may be written as: .js. .jsx.


1 Answers

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.

like image 110
Finglas Avatar answered Nov 05 '22 14:11

Finglas