I'm new to Mocha and I'm trying to run the tests on this code:
"use strict";
process.env.NODE_ENV = 'test';
var assert = require('assert'),
Browser = require('zombie'),
xapp = require('../app'),
app,
User = require('../models/users').User,
testMail = 'aaa@aaa.aaa',
testPassword = 'aD1$#!_é',
server;
before(function(done) {
xapp(function(sapp) {
app = sapp;
server = app.listen(3000, done);
});
});
after(function(done) {
app.closeMongo(function() {
server.close(done);
});
});
function clearDb(done) {
User.findOne({email: testMail}, function(err, user) {
if(err) return done(err);
if(!user) return done();
else {
user.remove(done);
}
});
}
describe('User signup', function() {
this.timeout(3200);
before(clearDb);
describe('Homepage Form', function() {
this.timeout(3200);
it('should load the trial form', function(done) {
Browser.visit('http://localhost:3000/', function(err, browser) {
if(err) return done(err);
assert.ok(browser.success, "page loaded");
var form = browser.query('form[action="/users/signup"]');
assert.notEqual(form, null, 'Form on homepage exists');
assert.notEqual(browser.query("input[type=email]", form), null, 'has email input on page');
done();
});
});
it('should test the trial form', function(done) {
Browser.visit('http://localhost:3000/', function(err, browser) {
if(err) return done(err);
assert.ok(browser.success, "page loaded");
browser
.fill('email', testMail)
.pressButton('Sign up', function(err) {
if(err) return done(err);
assert.equal(browser.location.pathname, '/users/signup', 'Links to signup page');
assert.notEqual(browser.text('h1').indexOf('Sign up'), -1, 'Title page contains "Sign up"');
var form = browser.query('form#form-signup');
assert.notEqual(form, null, 'Form on signup page exists');
var emailInput = browser.query('input[type=email]#email', form);
assert.notEqual(emailInput, null, 'has email input on signup page');
assert.equal(emailInput.value, testMail, 'email input has entered value on homepage');
done();
});
});
});
});
describe('Signup page form', function() {
this.timeout(3200);
it('should load the signup form', function(done) {
Browser.visit('http://localhost:3000/users/signup', function(err, browser) {
if(err) return done(err);
assert.ok(browser.success, "page loaded");
var form = browser.query('form[action="/users/signup"]#form-signup');
assert.notEqual(form, null, 'Form on signup page exists');
assert.equal(form.method, "post", "Form is POST");
assert.notEqual(browser.query("input[type=email]#email", form), null, 'has email input on page');
assert.notEqual(browser.query("input[type=password]#password", form), null, 'has password input on page');
assert.notEqual(browser.query("input[type=password]#passwordConfirm", form), null, 'has password confirmation input on page');
done();
});
});
it('should sign up a test user', function(done) {
Browser.visit('http://localhost:3000/users/signup', function(err, browser) {
if(err) return done(err);
assert.ok(browser.success, "page loaded");
browser
.fill('Email:', testMail)
.fill('Password:', testPassword)
.fill('Password confirmation:', testPassword)
.pressButton('#form-signup button[type=submit]', function(err) {
if(err) return done(err);
assert.notEqual(browser.query('.alert-success'), null, 'Has success alert');
assert.notEqual(browser.text('.alert-success').indexOf('Sign up successful'), -1, 'Found the text "Sign up successful" in alert');
done();
});
});
});
it('shouldn\'t sign up the same email twice', function(done) {
Browser.visit('http://localhost:3000/users/signup', function(err, browser) {
if(err) return done(err);
assert.ok(browser.success, "page loaded");
browser
.fill('Email:', testMail)
.fill('Password:', testPassword)
.fill('Password confirmation:', testPassword)
.pressButton('#form-signup button[type=submit]', function(err) {
if(err) return done(err);
assert.equal(browser.location.pathname, '/users/signup', 'Still on the signup page');
assert.notEqual(browser.query('.alert-danger'), null, 'Has danger alert');
assert.notEqual(browser.text('.alert-danger').indexOf('This email address already exists!'), -1, 'Found the text "This email address already exists!" in alert');
done();
});
});
});
});
});
describe('User login', function() {
this.timeout(2500);
after(clearDb);
describe('Homepage form', function() {
this.timeout(3200);
it('should appear when we click login', function(done) {
Browser.visit('http://localhost:3000/', function(err, browser) {
if(err) return done(err);
assert.ok(browser.success, "page loaded");
assert.equal(browser.query('form[action="/users/login"]'), null, 'No login form should be visible before we click on login');
browser.clickLink('Login', function(err) {
if(err) return done(err);
var form = browser.query('form[action="/users/login"]');
assert.notEqual(form, null, 'Login form should be visible after click on login');
assert.notEqual(browser.query('input[type=email]#email'), null, 'Has email input');
assert.notEqual(browser.query('a', form), null, 'Forgot your password?');
assert.notEqual(browser.query('input[type=password]#password'), null, 'Has password input');
done();
});
});
});
it('should not work with wrong credentials', function(done) {
this.timeout(3200);
Browser.visit('http://localhost:3000/', function(err, browser) {
if(err) return done(err);
assert.ok(browser.success, "page loaded");
browser.clickLink('Login', function(err) {
if(err) return done(err);
browser
.fill('Email:', testMail)
.fill('Password:', (testPassword + 'a'))
.pressButton('Login', function(err) {
if(err) return done(err);
assert.equal(browser.location.pathname, '/users/login');
assert.notEqual(browser.query('.alert-danger'), null, "Has danger alert");
assert.notEqual(browser.text('.alert-danger').indexOf('Invalid email or password.'), -1, 'alert contain error message "Invalid email or password."');
done();
});
});
});
});
});
describe('Login form', function() {
this.timeout(3200);
it('should login with valid credentials', function(done) {
Browser.visit('http://localhost:3000/users/login', function(err, browser) {
if(err) return done(err);
assert.ok(browser.success, "page loaded");
browser
.fill('#form-signin input[type=email]', testMail)
.fill('#form-signin input[type=password]', testPassword)
.pressButton('Sign in', function(err) {
if(err) return done(err);
assert.equal(browser.location.pathname, '/');
assert.notEqual(browser.query('.alert-success'), null, "Has success alert");
assert.equal(browser.text('.alert-success'), '×Close You are now logged in.');
assert.notEqual(browser.link('Dashboard'), null, 'Has dashboard link');
assert.notEqual(browser.link('Log out'), null, 'Has log out link');
done();
});
});
});
});
});
This is the command I'm using:
$ mocha -b -R spec -s 1000
And it's failing with this message:
1) "before all" hook
0 passing (2s)
1 failing
1) "before all" hook:
Error: timeout of 2000ms exceeded
at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:159:19)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
Could you please help me figure out why this is caused?
I know that if I change the timeout for the test runner:
$ mocha -b -R spec -s 1000 -t 3000
The tests work. But I want to know the actual cause of this error.
Thank you!
Aucun commentaire:
Enregistrer un commentaire