Fix tests to ensure compatibility w/ should@10

This commit is contained in:
Raymond Feng 2018-05-24 14:16:15 -07:00
parent dafb8a1ab7
commit 2b4b44292a
5 changed files with 35 additions and 23 deletions

View File

@ -1769,7 +1769,7 @@ DataAccessObject._coerce = function(where, options) {
val = utils.toRegExp(val); val = utils.toRegExp(val);
if (val instanceof Error) { if (val instanceof Error) {
val.statusCode = 400; val.statusCode = 400;
throw err; throw val;
} }
break; break;
} }

View File

@ -96,6 +96,19 @@ List.prototype.toObject = function(onlySchema, removeHidden, removeProtected) {
return items; return items;
}; };
/**
* Convert itself to a plain array.
*
* Some modules such as `should` checks prototype for comparison
*/
List.prototype.toArray = function() {
var items = [];
this.forEach(function(item) {
items.push(item);
});
return items;
};
List.prototype.toJSON = function() { List.prototype.toJSON = function() {
return this.toObject(true); return this.toObject(true);
}; };

View File

@ -1032,13 +1032,12 @@ describe('basic-querying', function() {
it('should return an error for invalid data types', function(done) { it('should return an error for invalid data types', function(done) {
// `undefined` is not tested because the `removeUndefined` function // `undefined` is not tested because the `removeUndefined` function
// in `lib/dao.js` removes it before coercion // in `lib/dao.js` removes it before coercion
invalidDataTypes.forEach(function(invalidDataType) { async.each(invalidDataTypes, function(v, cb) {
User.find({where: {name: {regexp: invalidDataType}}}, function(err, User.find({where: {name: {regexp: v}}}, function(err, users) {
users) {
should.exist(err); should.exist(err);
cb();
}); });
}); }, done);
done();
}); });
}); });
}); });

View File

@ -4418,7 +4418,7 @@ describe('relations', function() {
p.passport.toObject().should.eql({name: 'Anonymous'}); p.passport.toObject().should.eql({name: 'Anonymous'});
p.passportItem().should.equal(p.passport); p.passportItem().should.equal(p.passport);
p.passportItem(function(err, passport) { p.passportItem(function(err, passport) {
err.should.not.exist(); should.not.exist(err);
passport.should.equal(p.passport); passport.should.equal(p.passport);
}); });
}); });
@ -6040,8 +6040,8 @@ describe('relations', function() {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.jobs.reverse(function(err, ids) { cat.jobs.reverse(function(err, ids) {
var expected = [job3.id, job2.id]; var expected = [job3.id, job2.id];
ids.should.eql(expected); ids.toArray().should.eql(expected);
cat.jobIds.should.eql(expected); cat.jobIds.toArray().should.eql(expected);
done(); done();
}); });
}); });
@ -6152,7 +6152,7 @@ describe('relations', function() {
'should find items on scope with promises', function(done) { 'should find items on scope with promises', function(done) {
Category.findOne() Category.findOne()
.then(function(cat) { .then(function(cat) {
cat.jobIds.should.eql([job2.id]); cat.jobIds.toArray().should.eql([job2.id]);
return cat.jobs.find(); return cat.jobs.find();
}) })
.then(function(jobs) { .then(function(jobs) {
@ -6352,8 +6352,8 @@ describe('relations', function() {
return cat.jobs.reverse() return cat.jobs.reverse()
.then(function(ids) { .then(function(ids) {
var expected = [job3.id, job2.id]; var expected = [job3.id, job2.id];
ids.should.eql(expected); ids.toArray().should.eql(expected);
cat.jobIds.should.eql(expected); cat.jobIds.toArray().should.eql(expected);
done(); done();
}); });
}) })
@ -6382,9 +6382,9 @@ describe('relations', function() {
.then(function() { .then(function() {
var expected = [job3.id]; var expected = [job3.id];
if (connectorCapabilities.adhocSort !== false) { if (connectorCapabilities.adhocSort !== false) {
cat.jobIds.should.eql(expected); cat.jobIds.toArray().should.eql(expected);
} else { } else {
cat.jobIds.should.containDeep(expected); cat.jobIds.toArray().should.containDeep(expected);
} }
return Job.exists(job2.id); return Job.exists(job2.id);
}) })

View File

@ -848,7 +848,7 @@ describe('validations', function() {
User.validatesFormatOf('name', {with: /[a-z][A-Z]*$/, message: CUSTOM_MESSAGE}); User.validatesFormatOf('name', {with: /[a-z][A-Z]*$/, message: CUSTOM_MESSAGE});
var u = new User({name: 'invalid name string 123'}); var u = new User({name: 'invalid name string 123'});
u.isValid().should.be.false(); u.isValid().should.be.false();
u.errors.should.eql({ u.errors.should.containEql({
name: [CUSTOM_MESSAGE], name: [CUSTOM_MESSAGE],
codes: { codes: {
name: ['format'], name: ['format'],
@ -972,14 +972,14 @@ describe('validations', function() {
User.validatesNumericalityOf('age'); User.validatesNumericalityOf('age');
var user = new User({age: 'notanumber'}); var user = new User({age: 'notanumber'});
user.isValid().should.be.false(); user.isValid().should.be.false();
user.errors.should.eql({age: ['is not a number']}); user.errors.should.containEql({age: ['is not a number']});
}); });
it('fails when given undefined values', function() { it('fails when given undefined values', function() {
User.validatesNumericalityOf('age'); User.validatesNumericalityOf('age');
var user = new User({}); var user = new User({});
user.isValid().should.be.false(); user.isValid().should.be.false();
user.errors.should.eql({age: ['is blank']}); user.errors.should.containEql({age: ['is blank']});
}); });
it('skips undefined values when allowBlank option is true', function() { it('skips undefined values when allowBlank option is true', function() {
@ -992,14 +992,14 @@ describe('validations', function() {
User.validatesNumericalityOf('age', {allowBlank: true}); User.validatesNumericalityOf('age', {allowBlank: true});
var user = new User({age: 'test'}); var user = new User({age: 'test'});
user.isValid().should.be.false(); user.isValid().should.be.false();
user.errors.should.eql({age: ['is not a number']}); user.errors.should.containEql({age: ['is not a number']});
}); });
it('fails when given null values', function() { it('fails when given null values', function() {
User.validatesNumericalityOf('age'); User.validatesNumericalityOf('age');
var user = new User({age: null}); var user = new User({age: null});
user.isValid().should.be.false(); user.isValid().should.be.false();
user.errors.should.eql({age: ['is null']}); user.errors.should.containEql({age: ['is null']});
}); });
it('passes when given null values when allowNull option is true', function() { it('passes when given null values when allowNull option is true', function() {
@ -1353,7 +1353,7 @@ describe('validations', function() {
}); });
var u = new User({email: 'hello'}); var u = new User({email: 'hello'});
Boolean(u.isValid()).should.be.false(); Boolean(u.isValid()).should.be.false();
u.errors.should.eql({email: ['Cannot be `hello`']}); u.errors.should.containEql({email: ['Cannot be `hello`']});
u.errors.codes.should.eql({email: ['invalid-email']}); u.errors.codes.should.eql({email: ['invalid-email']});
}); });
@ -1470,7 +1470,7 @@ describe('validations', function() {
User.validatesDateOf('updatedAt'); User.validatesDateOf('updatedAt');
var u = new User({updatedAt: 'invalid date string'}); var u = new User({updatedAt: 'invalid date string'});
u.isValid().should.not.be.true(); u.isValid().should.not.be.true();
u.errors.should.eql({ u.errors.should.containEql({
updatedAt: ['is not a valid date'], updatedAt: ['is not a valid date'],
codes: { codes: {
updatedAt: ['date'], updatedAt: ['date'],
@ -1494,7 +1494,7 @@ describe('validations', function() {
}); });
var u = new AnotherUser({updatedAt: 'invalid date string'}); var u = new AnotherUser({updatedAt: 'invalid date string'});
u.isValid().should.not.be.true(); u.isValid().should.not.be.true();
u.errors.should.eql({ u.errors.should.containEql({
updatedAt: ['is not a valid date'], updatedAt: ['is not a valid date'],
codes: { codes: {
updatedAt: ['date'], updatedAt: ['date'],
@ -1507,7 +1507,7 @@ describe('validations', function() {
User.validatesDateOf('updatedAt', {message: CUSTOM_MESSAGE}); User.validatesDateOf('updatedAt', {message: CUSTOM_MESSAGE});
var u = new User({updatedAt: 'invalid date string'}); var u = new User({updatedAt: 'invalid date string'});
u.isValid().should.not.be.true(); u.isValid().should.not.be.true();
u.errors.should.eql({ u.errors.should.containEql({
updatedAt: [CUSTOM_MESSAGE], updatedAt: [CUSTOM_MESSAGE],
codes: { codes: {
updatedAt: ['date'], updatedAt: ['date'],