Mocha with asyc call back does not execute my before and it blocks

时间:2017-11-20 02:08:32

标签: javascript node.js mocha

I have mocha tests in a test suite. I noticed all my tests where passing even when they should fail. I put some console.log statements and realized mocha does not enter the before or it blocks

When I run the below, 1, 3 and 4 print to the console, with test passing ________________________________________

  process.env.NODE_ENV = 'development';

if (!process.env.PWD) {
  process.env.PWD = process.cwd();
}
const base = process.env.PWD;
const config = require(base + '/config');
const logger = require('mocha-logger');
const mongoose = require('mongoose');
const Org = require(base + '/models/org');
const orgs = require(base + '/controllers/org');
const should = require('chai').should();
const testUtils = require(base + '/test/utils');
let id, dummyOrg;

console.log('Print 1');
describe('Org api test suite', function () {
  before(function (done) {
    console.log('Print 2');
    mongoose.connect(config.localDb, function () {
      console.log('Connected to: ' + config.testDb);
    });
    dummyOrg = new Org({
      'tenatid': Object('59e3d8f09c26d7e9a21c39aa'),
      'name': 'Test Hospital',
      'type': 'Provider',
      'POC': 'Jack Ryan',
      'account_type': 'p3',
      'date_active': new Date('2016-04-30T00:00:00.000+0000'),
      'address': {
        'type': 'work',
        'Address1': '123 Street One',
        'Address2': 'Suite 100',
        'City': 'Minneapolis',
        'State': 'MN',
        'Postal Code': '55418',
        'Country': '55418'
      },
      'Phone1': '000-000-0000',
      'Phone2': '000-000-0000'
    });
    dummyOrg.save(function (err, org) {
      if (err) { console.log(err); }
      id = org._id;
    });
    done();
  });
  after(function (done) {
    Org.remove({}, function (err) {
      if (err) { console.log(err); }
    });
    mongoose.disconnect(done);
  });
  console.log('Print 3');
  describe('Create Org', function () {
    console.log('Print: 4');
    it('should create a new org', function (done) {
      console.log('Print: 5');
      let req = {
        body: {'tenatid': Object('59e3d8f09c26d7e9a21c39aa'), 'name': 'Test Hospital 2', 'type': 'Provider', 'POC': 'Jack Ryan', 'account_type': 'p3', 'date_active': Date('2016-04-30T00:00:00.000+0000'), 'address': {'type': 'work', 'Address1': '123 Street One', 'Address2': 'Suite 100', 'City': 'Minneapolis', 'State': 'MN', 'Postal Code': '55418', 'Country': '55418'}, 'Phone1': '000-000-0000', 'Phone2': '000-000-0000'}
      };
      let res = testUtils.responseValidatorAsync(200, function (org) {
        org.should.have.property('name');
        org.title.should.equal('Test Hospital 2');
        done();
      });
      orgs.postOrg(req, res);
    });
  });

0 个答案:

没有答案