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

Official `length` workaround doesn't work #30

Closed
endash opened this issue Apr 17, 2013 · 16 comments
Closed

Official `length` workaround doesn't work #30

endash opened this issue Apr 17, 2013 · 16 comments

Comments

@endash
Copy link

@endash endash commented Apr 17, 2013

About to dig deeper into this, but the be.of.length(x) workaround for have.length(x) no longer seems to work.

I just cloned fresh, npm installed and added a single test for

$('<div></div>').should.be.of.length(1);

And get the following failure:

TypeError: Property 'length' of object function (selector) {
    var obj = flag(this, 'object');
    if (obj instanceof $) {
      this.assert(
          obj.is(selector)
        , 'expected #{this} to be #{exp}'
        , 'expected #{this} not to be #{exp}'
        , selector
      );
    } else {
      _super.apply(this, arguments);
    }
  } is not a function
at Context.<anonymous> (file://localhost/Users/chris/dev/chai-jquery/test/chai-jquery-spec.js:604:37)
at Test.Runnable.run (file://localhost/Users/chris/dev/chai-jquery/node_modules/mocha/mocha.js:4039:32)
at Runner.runTest (file://localhost/Users/chris/dev/chai-jquery/node_modules/mocha/mocha.js:4404:10)
at file://localhost/Users/chris/dev/chai-jquery/node_modules/mocha/mocha.js:4450:12
at next (file://localhost/Users/chris/dev/chai-jquery/node_modules/mocha/mocha.js:4330:14)
at file://localhost/Users/chris/dev/chai-jquery/node_modules/mocha/mocha.js:4339:7
at next (file://localhost/Users/chris/dev/chai-jquery/node_modules/mocha/mocha.js:4287:23)
at file://localhost/Users/chris/dev/chai-jquery/node_modules/mocha/mocha.js:4307:5
at timeslice (file://localhost/Users/chris/dev/chai-jquery/node_modules/mocha/mocha.js:5279:29)

I get a different error in Safari:

'1' is not a function (evaluating 'subject.should.be.of.length(1)')

I suppose it's possible there's something wrong with my setup, but I don't see what it could be...

@endash
Copy link
Author

@endash endash commented Apr 17, 2013

New workaround, for the terminal variation of length: should.have.lengthOf(n)

FWIW the function it's complaining about and spitting out in the error message is the be method overwritten by chai-jquery, not a new addition at all. It is perplexing.

@endash
Copy link
Author

@endash endash commented Apr 17, 2013

Rolling back chai to 1.4.0 has no effect... I'm at a loss to explain why the workaround would have ever worked, given that be is also overridden to be a function, just like have.

@jfirebaugh
Copy link
Member

@jfirebaugh jfirebaugh commented Apr 17, 2013

It may well be that it never did work. I should've added it to the test suite when it was first proposed, but neglected to.

@nackjicholson
Copy link

@nackjicholson nackjicholson commented Sep 9, 2013

Only real work around my team has found is to not use length at all. And instead do expect(selection.length).to.equal(number);

@alexstrat
Copy link

@alexstrat alexstrat commented Oct 25, 2013

chai-jquery's have should maybe be renamed in something that does not interfere with chai's original have ?

@fizker
Copy link

@fizker fizker commented Dec 6, 2013

@nackjicholson I prefer expect(selection).to.have.property('length', number); over expect(selection.length).to.equal(number);.

I find that version way more readable in case of failure (Failure will read expected ... to have a property 'length' of x, but got y instead of expected x to be y.

@jedmao
Copy link

@jedmao jedmao commented Mar 3, 2014

I'm getting the same error with:

expect($('.foo')).to.have('h1');

TypeError: '[object Object]' is not a function (evaluating 'expect($('.foo')).to.have('h1')')

@mattfysh
Copy link

@mattfysh mattfysh commented Mar 28, 2014

I ended up opting for:

expect($('body')).with.length(1);
@jedmao
Copy link

@jedmao jedmao commented Mar 28, 2014

My issue was actually an issue with installing Chai Jquery into Karma. Now, my original expression works just fine.

@marcandre
Copy link

@marcandre marcandre commented Mar 31, 2014

with works fine.
Please fix the doc!

@gmfx
Copy link

@gmfx gmfx commented Apr 17, 2014

@jedmao: what exactly was the issue having the same prob with a mocha setup

@jedmao
Copy link

@jedmao jedmao commented Apr 17, 2014

@gumaflux my issue was with both syntaxes. The first isn't officially supported, so that shouldn't be a surprise:

expect(selection).to.have.length(2);

The second, however, was explicitly stated in the documentation as a workaround:

expect(selection).to.be.of.length(2);

This also doesn't work. The way I'm doing it now is pretty much as @nackjicholson has stated:

expect(selection.length).to.eq(2);

This is the only way I've gotten it to work. I'm sure the other ways expressed above work also, but I'm not interested in those syntaxes.

BTW @jfirebaugh, I can confirm that this workaround does, in fact, NOT work! I would love it if it did :)

@glittershark
Copy link

@glittershark glittershark commented Apr 29, 2014

👍 more descriptive error messages than "expected 2 to equal 1" would be very nice.

@gmfx
Copy link

@gmfx gmfx commented Apr 30, 2014

@jedmao great thanks

@jedmao
Copy link

@jedmao jedmao commented Apr 30, 2014

@gumaflux my co-worker had success with the following syntax as well:

expect(selection.length).to.be(2);
@jfirebaugh
Copy link
Member

@jfirebaugh jfirebaugh commented Oct 11, 2014

Fixed by #47.

@jfirebaugh jfirebaugh closed this Oct 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
10 participants
You can’t perform that action at this time.