vendredi 24 novembre 2017

mocha test not returning mongod save

Im having trouble testing a user verification method i am implementing using nodejs, mongodb, and mocha.

Error

^CHarrys-MacBook-Pro:Phonex hcbh96$ npm run test-watch

> ls@1.0.0 test-watch /Users/hcbh96/Desktop/PhonexDevelopment/Phonex
> nodemon --exec 'npm test'

[nodemon] 1.12.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm test`

> ls@1.0.0 test /Users/hcbh96/Desktop/PhonexDevelopment/Phonex
> mocha tests/**/*.test.js



Node app is running at localhost: + 3000
  Post /tempUser
{ phone: '5005550006',
  countryCode: '+1',
  email: 'test@test.com' }
create temp:{ phone: '5005550006',
  countryCode: '+1',
  email: 'test@test.com',
  _id: 5a18499b182f435b58eed843,
  createdAt: 2017-11-24T16:32:27.089Z,
  isVerified: false }
    1) should create a new tempUser


  0 passing (2s)
  1 failing

  1) Post /tempUser
       should create a new tempUser:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

The code below is the part of code that seems with the test to be causing the issue:

tempUser.js -- controller

// create a new user based on the request
exports.create = function(request, response) {
    const params = request.body;
    console.log(params);
    // Create a new user based on params
    const tempUser = new TempUser({
        phone: params.phone,
        countryCode: params.countryCode,
        email: params.email
    });
    console.log(`create temp:${tempUser}`);

    // tempUser.save(function(err, doc) {
    //     if (err) {
    //       console.log(doc);
    //         response.send(err);
    //     } else {
    //         // If the user is created successfully, send them an account
    //         // verification token
    //         tempUser.sendAuthyToken(function(err) {
    //             if (err) {
    //                 console.log("err");
    //                 response.send(err);
    //             }
    //         });
    //     response.send(doc);
    //   }
    // });

    tempUser.save().then((doc)=> {
      // If the user is created successfully, send them an account verification token
      // tempUser.sendAuthyToken((err)=>{
      //   response.send(err)
      // });
      console.log(`IMHERE2 ${doc}`);
      response.send(doc);

    }, (err) => {
      console.log(err);
      response.send(err)
    });
};

Here is the test:

The test is being run via the command line using test-watch so nodemon is running which I think could be part of the issue:

Config

"test": "mocha tests/**/*.test.js",
"test-watch": "nodemon --exec 'npm test'"

TempUserTest.js

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


//local dependencies
const {app} = require('../../config/initialisers/server.js');
const {User} = require('../../api/models/user/user.js');
const {TempUser} = require('../../api/models/user/tempUser.js')


describe("Post /tempUser", ()=>{

  it('should create a new tempUser',(done)=>{
    var userEmail = "test@test.com";
    var userPhone = "5005550006";
    var userCountryCode = "+1";

    request(app)
    .post('/api/users/temp-user/create')
    .send({
      phone: userPhone,
      countryCode: userCountryCode,
      email : userEmail,
    })
    .expect(200)
    .expect('Content-Type', /json/)

    .expect((res)=>{
****NO RES COMING IN HERE**********************************************
      expect(res.body).toInclude({
        email: userEmail,
        countryCode: userCountryCode,
        phone: userPhone
      });
    })
    .end((err,res) => {
      if(err){
        return done(err);
      }
      TempUser.find().then((tempUsers)=>{
        expect(tempUsers.length).toBe(1);
        expect(tempUsers[0].countryCode).toBe(userCountryCode);
        expect(tempUsers[0].email).toBe(userEmail);
        expect(tempUsers[0].phone).toBe(userPhone);
        done();
      }).catch((e)=>done(e))
    });
  });
});


POSTMAN SUCCESSFUL POST

{
    "phone": "5005550006",
    "countryCode": "+1",
    "email":"test@test.com"
}

POSTMAN SUCCESSFUL RESPONSE

{
    "__v": 0,
    "phone": "5005550006",
    "countryCode": "+1",
    "email": "test@test.com",
    "_id": "5a18485aa191f65b445341c1",
    "createdAt": "2017-11-24T16:27:06.133Z",
    "isVerified": false
}

Thank you in advance for responses to this.

My feeling is that this is a silly little error somewhere in the testfile but unfortunately I am not experienced with Nodejs to spot it.

Aucun commentaire:

Enregistrer un commentaire