Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.empty fails for mongoose arrays #537

Closed
tusbar opened this issue Oct 21, 2015 · 6 comments · Fixed by gluckgames/pixi-packer#15
Closed

.empty fails for mongoose arrays #537

tusbar opened this issue Oct 21, 2015 · 6 comments · Fixed by gluckgames/pixi-packer#15

Comments

@tusbar
Copy link
Contributor

tusbar commented Oct 21, 2015

Hey,

The new .empty assertion that was introduced in #499 fails for mongoose arrays.

You can reproduce with the following:

var mongoose = require('mongoose');

var schema = new mongoose.Schema({
    array: [{
        prop: String
    }]
});

var Foo = mongoose.model('Foo', schema);
var foo = new Foo();

it('should be empty', function () {
    expect(foo.array).to.be.empty;
});

It fails with a AssertionError: expected [] to be empty.

I am using mongoose @ 4.1.12 but I’m not sure that’s relevant to the issue.

@keithamus
Copy link
Member

Hey @tusbar thanks for the issue.

This is probably because the array has some keys assigned to it. What do you get back from Object.keys(foo.array)?

@tusbar
Copy link
Contributor Author

tusbar commented Oct 21, 2015

@keithamus it does have keys as it is not a real Array, but it implements the Array API (or at least some of it).

> console.log(Object.keys(foo.array));
[ '_cast',
  'id',
  'toObject',
  'inspect',
  'create',
  'notify',
  '_atomics',
  '_parent',
  '_markModified',
  '_registerAtomic',
  '$__getAtomics',
  'hasAtomics',
  '_mapCast',
  'push',
  'nonAtomicPush',
  '$pop',
  'pop',
  '$shift',
  'shift',
  'pull',
  'splice',
  'unshift',
  'sort',
  'addToSet',
  'set',
  'indexOf',
  'remove',
  'isMongooseArray',
  'isMongooseDocumentArray',
  'validators',
  '_path',
  '_schema',
  '_handlers' ]

@keithamus
Copy link
Member

Okay, so its not empty then? In that case, expect(foo.array).to.be.empty is failing correctly, right?

Is your issue that the message isn't good enough? I guess it should say something like AssertionError: expected [ isMongooseArray: true, _atomics: [Object], validators: [], _path: '' ] to be empty. instead.

@tusbar
Copy link
Contributor Author

tusbar commented Oct 21, 2015

@keithamus the issue is that this used to work pre-3.3.0. And it is a breaking change.

I am sure that many people use chai and mongoose together and will pull their hair out trying to understand what’s going on (especially with that expected [] to be empty error message).

I seems that you want .empty to be strictly for Array and not have compatibility for Array-like objects – if so, 3.3.0 should probably be tagged 4.0.0.

@keithamus
Copy link
Member

Hmm, ok. If you'd like to make a PR reverting #499, and we can release a patch release fixing this breakage.

We'll then release #499 again but inside 4.0.0. /cc @Daio-io

@keithamus
Copy link
Member

Also @tusbar - if you get a PR done ASAP I can cut a release today

tusbar added a commit to tusbar/chai that referenced this issue Oct 21, 2015
The change introduced in chaijs#499 breaks compatibility with Array-like
objects (e.g. mongoose arrays).
It should be applied in the next major version.

Fix chaijs#537
lucasfcosta pushed a commit to lucasfcosta/chai that referenced this issue Mar 14, 2016
The change introduced in chaijs#499 breaks compatibility with Array-like
objects (e.g. mongoose arrays).
It should be applied in the next major version.

Fix chaijs#537
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants