gpt4 book ai didi

node.js - Mocha 抛出超时,但 Mongoose 将数据保存到 Promise 链中的数据库?

转载 作者:可可西里 更新时间:2023-11-01 10:38:23 24 4
gpt4 key购买 nike

我正在使用 Mocha、Mongoose 和 MongoDB。

我的目标只是围绕创建帐户进行测试。我有以下代码:

require( "./../../config/config" );
var mongoose = require( "mongoose" );
mongoose.connect( process.env.MONGODB_URI );

const expect = require( "expect" );
var { Account } = require( "./../../models/account" );

describe( "Account Creation", () =>
{
it( "should successfully create an account", ( done ) =>
{
var data =
{
username: "PrestonJ",
email: "someEmail@mail.com",
password: "somePassword123!"
};

var account = new Account( data );
account.save().then( ( result ) =>
{
console.log( "(temporary log) account saved" );
done();
}).catch( ( e ) => done( e ) );
});
});

Mongoose promise 链执行并将新帐户保存到数据库中,但是 done() 永远不会到达(即使它被写入,并且上面的控制台调用有效)。

这导致 Mocha 测试失败,给我:错误:超时超过 2000 毫秒。对于异步测试和 Hook ,确保调用“done()”;如果返回一个 Promise,请确保它已解析。

我试图只返回 Promise 链,但这也不起作用。我也尝试过完全删除 done 并返回链,但无济于事。

几个小时过去了,我一直找不到正确的做法/修复方法。

编辑:我试过增加 Mocha 的超时时间。错误是否以某种方式被吞没了?它在我的模型文件中吗?

编辑 2:这是帐户模型的来源:

var mongoose = require( "mongoose" );

const _ = require( "lodash" );
const validator = require( "validator" );
const jwt = require( "jsonwebtoken" );
const bcrypt = require( "bcryptjs" );

let Account_CollectionName = "accounts";
var Account_Schema = new mongoose.Schema(
{
username:
{
type: String,
minLength: 3,
trim: true,
required: true,
unique: true
},
email:
{
type: String,
minlength: 1,
trim: true,
required: false,
unique: true,
sparse: true,
validate: validator.isEmail,
message: "{VALUE} is not a valid email"
},
password:
{
type: String,
required: true,
minLength: 5
},
tokens:
[{
access:
{
type: String,
required: true
},
token:
{
type: String,
required: true
}
}],
},
{
collection: Account_CollectionName
});

Account_Schema.pre( "save", function( next )
{
var account = this;

if( account.isModified( "password" ) )
{
bcrypt.genSalt( 10, ( err, salt ) =>
{
bcrypt.hash( account.password, salt, ( err, hash ) =>
{
account.password = hash;
next();
});
});
}
else
next();
});

Account_Schema.methods.toJSON = function()
{
var account = this;
var accountObject = account.toObject();

return _.pick( accountObject,
[
"_id", "username", "email"
] );
};

Account_Schema.methods.generateAuthToken = function()
{
var account = this;
var access = "auth";
var token = jwt.sign( { _id: account._id.toHexString(), access }, process.env.JWT_SECRET ).toString();

account.tokens = account.tokens.concat( [{ access, token }] );

return account.save().then( () =>
{
return token;
});
};

Account_Schema.statics.findByToken = function( token )
{
var Account = this;
var decoded;

try {
decoded = jwt.verify( token, process.env.JWT_SECRET );
}
catch( e )
{
return Promise.reject();
}

return Account.findOne(
{
"_id" : decoded._id,
"tokens.token": token,
"tokens.access": "auth"
});
};

Account_Schema.statics.findByCredentials = function( username, password )
{
var Account = this;

return Account.findOne( { username } ).then( ( account ) =>
{
if( ! account )
return Promise.reject();

return new Promise( ( resolve, reject ) =>
{
bcrypt.compare( password, account.password, ( err, res ) =>
{
if( res )
resolve( account );
else
reject();
});
});
});
};

Account_Schema.methods.removeToken = function( token )
{
var account = this;

return account.update(
{
$pull:
{
tokens: { token }
}
});
};

var Account = mongoose.model( Account_CollectionName, Account_Schema );

module.exports = { Account };

最佳答案

我想通了。许多事情需要完成;这是 it block 的固定源代码:

it( "should successfully create an account", () =>
{
var data =
{
username: "PrestonJ",
email: "someEmail@mail.com",
password: "somePassword123!"
};

let account = new Account( data );
return account.save().then( ( result ) =>
{
if( result )
{
console.log( "Successful!" );
return Promise.resolve();
}
else
return Promise.reject( "cannot save" );
}).catch( ( e ) =>
{
return Promise.reject( e );
});
});
  1. 删除done/done()的所有痕迹
  2. 返回 account.save().then(...)...
  3. then() 中,使用 return Promise.resolve() 而不是 done()
  4. catch() 中,使用 return Promise.reject(e) 而不是 done(e)

关于node.js - Mocha 抛出超时,但 Mongoose 将数据保存到 Promise 链中的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52069953/

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