lundi 2 octobre 2017

How to avoid code redundancy in large amounts of Node.JS BDD tests

For the last months I was working on the backend (REST API) of a quite big project that we started from scratch. We were following BDD (behavior-driven-development) standards, so now we have a large amount of tests (~1000). The tests were written using chai - a BDD framework for Node.JS, but I think that this question can be expanded to general good practices when writting tests.

At first we tried to avoid code redundancy as much as possible and it went quite well. As the number of lines of code and people working on the project grew it was becoming more and more chaotical, but readable. Sometimes minor changes in the code that could be applied in 15 minutes caused the need to change e.g. mock data and methods in 30+ files etc which meant 6 hours of changes and running tests (extreme example).

TL:DR

We want to refactor now these BDD tests. As an example we have such a function:

function RegisterUserAndGetJWTToken(user_data, next: any){
    chai.request(server).post(REGISTER_URL).send(user_data).end((err: any, res: any) => {
        token = res.body.token;
        next(token);
    })
}

This function is used in most of our test files. Does it make sense to create something like a test-suite that would contain these kind of functions or are there better ways to avoid redundancy when writing tests? Then we could use imports like these:

import {RegisterUserAndGetJWTToken} from "./test-suite";
import {user_data} from "./test-mock-data";

  • Do you have any good practices that you can share?
  • Are there any npm packages that could be useful (or packages for other programming languages)?
  • Do you think that this approach has also downsides (like chaos when there would be multiple imports)?
  • Maybe there is a way to inject or inherit the test-suite for each file, to avoid imports and have it by default in each file?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire