0

I am working on an api using express which interfaces with a MySQL database to store user credentials.

I have an endpoint for registering users, and another for logging in. When I test these in Postman they work as expected.

I want to add unit tests to my API to test these endpoints which I can then integrate with a CI in the future.

I followed this guide to add endpoint testing using jest and supertest. This works somewhat but it is not an ideal solution. Ideally I want to use an in memory database instead of my actual SQL connection to test so that I can use a fresh database state for each test. I am struggling to find info online for how to achieve this in express.

Here is how I am currently querying my database to find a user by username.

const connection = require("./db.js");
User.getByUsername = (username, result) => {
  connection.query(
    `SELECT * FROM users WHERE username = "${username}"`,
    (err, res) => {
      if (err) {
        result(
          {
            status: 500,
            message: "Internal server error",
          },
          null
        );
        return;
      }
      if (res.length < 1) {
        // not found User with the username
        result(
          {
            status: 401,
            message: "Incorrect Login Credentials",
          },
          null
        );
        return;
      }

      //found user
      result(null, res[0]);
      return;
    }
  );
};

db.js:

const mysql = require("mysql");

const dbConfig = {
  dev: {
    driver: process.env.DB_DRIVER,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE,
  },
};

const connection = mysql.createConnection(dbConfig.dev);

connection.connect((error) => {
  if (error) throw error;
  console.log("Successfully connected to the database");
});

module.exports = connection;

What is the correct way to configure an in memory database for testing? and how do I instruct my tests to use the testing database instead of my actual MySQL Database?

Here is an example test:

const app = require('../server');

const supertest = require('supertest');
const request = supertest(app);

describe('testing user registration and login endpoints', () => {
    it('test login without token returns 401', async done => {
        const res = await request.get('/users');
        expect(res.status).toBe(401)
        done();
    });
})

1 Answer 1

2

You don't create an in memory database. You create a regular database.
The tables of this database must be configured to use 'memory' as DB-engine.
For testing, this database must be added in const dbConfig as a second item, named 'test', or something else than 'dev'. In the test setup you use this 'test' DB.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.