From db1fb917952093409426325057026076353ef622 Mon Sep 17 00:00:00 2001 From: Ryan Wheale Date: Wed, 10 Jul 2024 22:20:48 -0600 Subject: [PATCH] chore: harden tests and error messages --- test/index.test.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 7960fda..1eda833 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -385,7 +385,7 @@ describe('mongoose-lean-getters', function() { assert.strictEqual(doc.field, '1337'); }); - it('should should allow getters to transform return type', async function() { + it('should should allow array getters to return non-arrays', async function() { const userSchema = new mongoose.Schema({ emails: { type: [String], @@ -395,12 +395,14 @@ describe('mongoose-lean-getters', function() { } }); userSchema.plugin(mongooseLeanGetters); - const User = mongoose.model('transform-arrays', userSchema); + const User = mongoose.model('gh-37-transform-arrays', userSchema); const variants = [ { sourceVal: 'foo', expectedVal: 'foo' }, { sourceVal: ['foo'], expectedVal: 'foo' }, { sourceVal: ['foo', 'bar'], expectedVal: ['foo', 'bar'] }, + { sourceVal: [], expectedVal: undefined }, + { sourceVal: null, expectedVal: undefined }, { sourceVal: undefined, expectedVal: undefined }, ]; @@ -410,8 +412,9 @@ describe('mongoose-lean-getters', function() { await user.save(); const foundUser = await User.findById(user._id).lean({ getters: true }); - assert.deepStrictEqual(user.emails, expectedVal, `user did not have expected value { sourceVal: ${sourceVal}, expectedVal: ${expectedVal} }`); - assert.deepStrictEqual(foundUser.emails, expectedVal, `foundUser did not have expected value { sourceVal: ${sourceVal}, expectedVal: ${expectedVal}`); + const stringified = JSON.stringify({ sourceVal, expectedVal }); + assert.deepStrictEqual(user.emails, expectedVal, `user did not have expected value ${stringified}`); + assert.deepStrictEqual(foundUser.emails, expectedVal, `foundUser did not have expected value ${stringified}`); }) ); });