Test

We use Jest and SuperTest for test.

Use

npm run test

to run all tests. To run tests stored in awesome.test.js, use

npm run test -- awesome.test.js

To run test named foo, use

npm run test -- --testNamePattern foo

or

npm run test -- -t foo

Setup and Teardown

Before run any test, we populate the jest database. And after the last test, we drop all tables from the jest database.

Configuration file used when by Jest
"use strict";

module.exports = {
    database: {
        host: "db",
        username: "root",
        password: "123.456",
        database: "jest",
        dialect: "mysql",
        timezone: "-03:00",
        define: {
            charset: "utf8",
            collate: "utf8_general_ci",
            underscored: true,
            timestamps: true,
        },
        pool: {
            max: 5,
            idle: 30000,
            acquire: 60000
        },
        logging: false
    },
    nodemailer: {
        host: 'smtp.ethereal.email',
        port: 587,
        auth: {
            user: 'youremail@gmail.com',
            pass: 'yourpassword'
        }
    }
};

Unit Test

src/**/*.test.js stores unit tests, for example, data access object.

Integration Test

src/__tests__/ stores integration tests, for example, HTTP REST API calls.

Continuous Integration

We use Travis CI for Continous Integration.

Travis CI configuration
language: node_js
node_js:
  - "14"
dist: bionic
sudo: required
services:
  - mysql

env:
  - NODE_ENV=travis

install:
  - npm install

before_script:
  - mysql -u root -e 'CREATE DATABASE jest;'

script:
  - npm run lint
  - npm run test

Travis CI provides MySQL to be used during the tests:

Configuration file used when in Travis CI
"use strict";

module.exports = {
    database: {
        host: "127.0.0.1",
        username: "root",
        password: "",
        database: "jest",
        dialect: "mysql",
        timezone: "-03:00",
        define: {
            charset: "utf8",
            collate: "utf8_general_ci",
            underscored: true,
            timestamps: true,
        },
        pool: {
            max: 5,
            idle: 30000,
            acquire: 60000
        },
        logging: false
    },
    nodemailer: {
        host: 'smtp.ethereal.email',
        port: 587,
        auth: {
            user: 'youremail@gmail.com',
            pass: 'yourpassword'
        }
    }
};