lundi 25 avril 2016

nodejs koa request async test

In my nodejs/koa project there is an /api/healt-check/server endpoint. I would like to test it with a real request (without stub/mock).

I start the test with gulp:

gulp.task('test', () => {
    gulp.src('./backend/**/*.spec.js')
        .pipe(mocha())
        .once('error', (ex) => {
            console.log('ERROR IN TEST');
            console.log(ex);
            process.exit(1);
        })
        .once('end', () => {
            process.exit();
        });
});

I would like to create something like this in my test file:

'use strict';

let request = require('co-supertest');
let app = require('../../../index');

describe('Health check', function() {
  it('server should respond with success', function* () {
    let response = yield request(app.listen())
      .get('/api/health-check/server')
      .expect('Content-Type', /json/)
      .expect(200)
      .end();

    expect({ success: true }).to.eql(response.body);
  });
});

But this test passing even if I change the expectation to false. Could it be some async problem? Do I need some setup before I can use this request?

Aucun commentaire:

Enregistrer un commentaire