Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test runs localy but fails on travis ci.. I'm using mocha, supertest, nyc #134

Open
Jaman-dedy opened this issue Jan 9, 2019 · 1 comment

Comments

@Jaman-dedy
Copy link

I'm setting up a reful api in nodejs, the first part was to set it with data strucutre and all test were running right good localy and on travis but now I'm using posgresql and I've add some instructions in my .travis.yml... find it below

language: node_js
node_js:
 - "stable"

services:
  - postgresql
addons:
  postgresql: "10"
  apt:
    packages:
      - postgresql-10
      - postgresql-client-10
env:
  global:
    - PGPORT=5432
    - DB_NAME=questioner
    - DB_USER=jaman
before_script:
  - psql --command="CREATE USER ${DB_USER};"
  - psql --command="CREATE DATABASE ${DB_NAME} WITH OWNER = ${DB_USER};"

connection instructions are gathered in one file, here it is

const pg = require('pg');

const config = {
  user: 'jaman',
  database: 'questioner',
  password: '123',
  port: 5432
};
const pool = new pg.Pool(config);

module.exports = pool;
```
This connection is imported in user.js and userTest.js respectivly below

```
const express = require('express');
const pool = require('./connection');


const router = express.Router();


router.get('/', (req, res, next) => {
  pool.query('SELECT * FROM users', (err, result) => {
    if (err) {
      throw err;
    }
    res.status(200).json({
      status: 200,
      data: result.rows
    });
  });
});

router.get('/:userId', (req, res, next) => {
  const userId = parseInt(req.params.userId, 10); 
  pool.query('SELECT * FROM users WHERE id_user = $1', [userId], (err, result) => {
    if (err) {
      throw err;
    }
    res.status(200).json({
      status: 200,
      data: result.rows
    });
  });
});

router.post('/', (req, res, next) => {
  const {
    firstname, lastname, othername, email, phonenumber, username, registered, isadmin
  } = req.body;

  pool.query('INSERT INTO users (firstname, lastname, othername, email, phonenumber, username, registered, isadmin) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)', [firstname, lastname, othername, email, phonenumber, username, registered, isadmin], (err, results) => {
    if (err) {
      throw err;
    } else {
      res.status(201).json({
        status: 201,
        data: [req.body]
      });
    }
  });
});

router.patch('/:userId', (req, res, next) => {
  const userId = parseInt(req.params.userId, 10);

  const { firstname, lastname, othername } = req.body;

  pool.query(
    'UPDATE users SET firstname = $1, lastname = $2, othername = $3 WHERE id_user = $4',
    [firstname, lastname, othername, userId],
    (err, results) => {
      if (err) {
        throw err;
      }
      res.status(200).json({
        status: 200,
        data: [req.body]
      });
    }
  );
});
router.delete('/:userId', (req, res, next) => {
  const userId = parseInt(req.params.userId, 10);

  pool.query('DELETE FROM users WHERE id_user = $1', [userId], (err, results) => {
    if (err) {
      throw err;
    }

    res.status(200).json({
      status: 200,
      data: `User deleted with ID: ${userId}`
    });
  });
});
module.exports = router;

```
and the test file

```
const assert = require('chai').assert;
const request = require('supertest');
const app = require('../app');
const connection = require('../api/routes/connection');

describe('Testing user endpoints', () => {
  describe('All users', () => {
    it('All users', (done) => {
      request(app)
        .get('/users')
        .set('Accept', 'application/json')
        .expect('Content-Type', /json/)
        .expect(200, done);
    });
  });
  describe('Find a specific user', () => {
    it('Get a specific user', (done) => {
      request(app)
        .get('/users/1')
        .set('Accept', 'application/json')
        .expect('Content-Type', /json/)
        .expect(200, done);
    });
  });
  
  describe('Post user', () => {
    const user = {
      id: 1,
      firstname: 'Emmanuel',
      lastname: 'Bush',
      othername: 'King',
      email: '[email protected]',
      phonenumber: '+250789813478',
      username: 'EmaBush',
      registered: '2018-02-16',
      isAdmin: true
    };
    it('Create a user', (done) => {
      request(app)
        .post('/users')
        .send(user)
        .set('Accept', 'application/json')
        .expect(201)
        .end((err) => {
          if (err) return done(err);
          done();
        });
    });
  });

  describe('udpate user', () => {
    it('Updated user', (done) => {
      request(app)
        .patch('/users/1')
        .set('Accept', 'application/json')
        .expect('Content-Type', /json/)
        .expect(200, done);
    });
  });

  describe('Delete user', () => {
    it('Deleted user', (done) => {
      request(app)
        .delete('/users/1')
        .set('Accept', 'application/json')
        .expect('Content-Type', /json/)
        .expect(200, done);
    });
  });
});

```
when I run localy npm test i get the following result

![image](https://user-images.githubusercontent.com/46047244/50920276-a2549400-144d-11e9-9189-5d330cc802b9.png)


but when I push up all my files on github travis throws the following error

for good preview find below the screen shot of travis error

![image](https://user-images.githubusercontent.com/46047244/50920329-c57f4380-144d-11e9-80f2-4f828b627552.png)

![image](https://user-images.githubusercontent.com/46047244/50920353-db8d0400-144d-11e9-8935-0e9cac9ea506.png)

![image](https://user-images.githubusercontent.com/46047244/50920393-ef386a80-144d-11e9-9e40-c33ad50ffdbf.png)

need help plz... thx in advance
@Jaman-dedy
Copy link
Author

Images fails to be displayed, find them below

when running localy

oktest

when running on travis

fail1

fail2

fail3

thx for all idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant