gpt4 book ai didi

javascript - Mocha 因 "timeout exceeded"失败

转载 作者:行者123 更新时间:2023-11-29 16:10:57 24 4
gpt4 key购买 nike

我是 Mocha 的新手,我正在尝试对此代码运行测试:

"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();
});
});
});
});
});

这是我正在使用的命令:

$ mocha -b -R spec -s 1000

失败并显示此消息:

  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)

你能帮我弄清楚这是为什么造成的吗?

我知道如果我更改测试运行器的超时时间:

$ mocha -b -R spec -s 1000 -t 3000

测试有效。但是我想知道这个错误的真正原因。

谢谢!

最佳答案

您收到您收到的错误,因为您的顶级 before Hook 需要超过默认的 2000 毫秒超时延迟才能运行。如果您不知道:before 的意思是“在此套件中的所有测试之前执行此操作”,这就是为什么错误消息会提到“before all” Hook 。

当您执行 $ mocha -b -R spec -s 1000 -t 3000 时它会起作用,因为您已将超时增加到 3000 毫秒。 before 钩子(Hook)中的代码需要 2 到 3 秒才能运行。您可以通过将此设置放入 test/mocha.opts 中来永久设置。

关于javascript - Mocha 因 "timeout exceeded"失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28212042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com