3

I am trying to write test for my API with postgresql.

It seem the test is not hitting the right database for response or maybe my configuration is not proper for the test.

I expect the test to check my token and treat as req from a body but the test is not even recognizing the request body (req.body);

Please I need guidance.

my files are as below. test.js

/* global describe it */
import chai, { expect, assert } from 'chai';

import chaiHttp from 'chai-http';

import jwt from 'jsonwebtoken';

import server from '../app/app';

import parties from '../db/dummy';

process.env.NODE_ENV = 'test';

chai.should();
chai.use(chaiHttp);

const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJ0b3NpbkB5YWhvby5jb20iLCJpc19hZG1pbiI6dHJ1ZSwiaWF0IjoxNTQ5Mjc3MzAxLCJleHAiOjE1NDkzNjM3MDF9.ZJl147IVpw3TakPnR4uw_y6ZQZ6-Heoup5bdBBPs0iE';
const decoded = jwt.verify(token, process.env.SECRET_KEY);

describe('POST/api/v1/parties', () => {
  it('should return 201 for new party', (done) => {
    const newParty = {
      name: 'APGC',
      hqAddress: '1 Iyana Iba ',
      logoUrl: 'www.logo.net.png',
      decoded,
    };
    chai.request(server)
      .post('/api/v1/parties')
      .type('form')
      .send(newParty)
      .end((err, res) => {
        expect(decoded).be.a('string');
        expect(res.body).to.have.property('data');
        assert.isOk(res.body);
      });
    done();
  });
 });

my package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "nyc mocha --compilers js:@babel/register server/test/*.spec.js --exit",
    "start:dev": "npm run build && npm run serve",
    "start": "nodemon --exec babel-node server/app/app.js",
    "heroku": "node server/app/app.js",
    "build": "babel server -d lib",
    "serve": "node lib/app/app.js",
    "coverage": "nyc report --reporter=text-lcov | coveralls",
    "migration": "psql -U postgres -p 5433 -f server/model/table.sql",
    "seed": "babel-node server/model/seeding.js",
    "data:dev": "npm run migration && npm run seed"
  },
  "author": "darot",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "dotenv": "^6.2.0",
    "express": "^4.16.4",
    "express-jwt": "^5.3.1",
    "jsonwebtoken": "^8.4.0",
    "morgan": "^1.9.1",
    "pg": "^7.8.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/node": "^7.0.0",
    "@babel/preset-env": "^7.3.1",
    "@babel/register": "^7.0.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^24.0.0",
    "babel-loader": "^8.0.5",
    "babel-preset-env": "^1.7.0",
    "babel-register": "^6.26.0",
    "chai": "^4.2.0",
    "chai-http": "^4.2.1",
    "codecov": "^3.1.0",
    "coveralls": "^3.0.2",
    "cross-env": "^5.2.0",
    "eslint": "^5.12.1",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-plugin-import": "^2.15.0",
    "istanbul": "^0.4.5",
    "mocha": "^5.2.0",
    "nodemon": "^1.18.9",
    "nyc": "^13.1.0",
    "travis": "^0.1.1"
  }
}

my database config

import pg from 'pg';

import dotenv from 'dotenv';

dotenv.config();

process.env.NODE_ENV = 'test';

let connection;
const string = {
  user: process.env.DB_USER,
  host: process.env.DB_HOST,
  database: process.env.DB_NAME,
  password: process.env.DB_PASS,
  port: process.env.DB_PORT,
  max: 10,
  idleTimeoutMillis: 3000,
};
const stringTest = {
  user: process.env.DB_USER,
  host: process.env.DB_HOST,
  database: process.env.DB_NAME2,
  password: process.env.DB_PASS,
  port: process.env.DB_PORT,
  max: 10,
  idleTimeoutMillis: 3000,
};
if (process.env.NODE_ENV === 'production') {
  connection = {
    connectionString: process.env.DATABASE_URL,
    ssl: true,
  };
} else if (process.env.NODE_ENV === 'test') {
  connection = stringTest;
} else {
  connection = string;
}
const pool = new pg.Pool(connection);

export default pool;
3
  • 1
    done should be called inside the end callback. That's at least one of your problems. Commented Feb 4, 2019 at 11:58
  • 1
    You shouldn't hit real DB in a unit test, you need to mock out all DB requests Commented Feb 4, 2019 at 12:51
  • Thanks alot. I figured out that the token passed could not be parsed. Normally, I use the token on postman and it works fine perhaps the token is being parsed as req.auth or req.headers on postman. How do I parse it for test. Commented Feb 5, 2019 at 6:25

1 Answer 1

1

I was able to resolve this by setting authorization under the post method in the test suite .set('Authorization', token)

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.