node.js - Post a form before all tests chai mocha -
if need submit login form before tests, how can chai , mocha;
i've looked @ beforeeach functions cant seem make work
var chai = require('chai'); var chaihttp = require('chai-http'); var app = require('../app'); var should = chai.should(); var expect = chai.expect; chai.use(chaihttp); var username = 't@t.com'; var password = 'test'; before(function() { it('should login details login form / post', function(done) { var request = chai.request(app); request .post('/session/new') .field('email', username) .field('password', password) .end(function(err, res) { res.should.have.status(200); res.should.be.html; done(); }); }); describe('vouchers', function() { it('should list vouchers on / get', function(done) { var request = chai.request(app); request .get('/vouchers') .end(function(err, res) { res.should.have.status(200); res.text.should.match(/welcome voucher homepage/); res.should.be.html; done(); }); }); }); });
you cannot put it
inside before
hook. i've tried see happen , got various results. behavior doing undefined.
looking @ code, looks want code in first it
initialize state , have stuff in describe
run afterwards. so, unwrap code you've put in first it
, make body of before
hook. , since chai-http
produces promises, should able return request produce. should work:
before(function() { var request = chai.request(app); return request .post('/session/new') .field('email', username) .field('password', password) .end(function(err, res) { res.should.have.status(200); res.should.be.html; }); });
Comments
Post a Comment