chaijs / chai

BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
https://chaijs.github.io
MIT License
8.12k stars 697 forks source link

should.not.exist throwing TypeError #84

Closed anacronw closed 12 years ago

anacronw commented 12 years ago

I am using Chai with Mocha in the browser and with the "should" BDD style. When I attempt to use

My test is the following:

    it('should not pollute the global namespace', function(){
        should.not.exist($);
        should.not.exist(jQuery);
    });

I get the following error:

TypeError: Expecting a function in instanceof check, but got undefined
    at Assertion.<anonymous> (http://localhost/node_modules/chai-jquery/chai-jquery.js:125:26)
    at Assertion.<anonymous> (http://localhost/node_modules/chai/chai.js:3383:41)
    at Object.exist (http://localhost/node_modules/chai/chai.js:2380:36)
    at Context.<anonymous> (http://localhost/specs.js:79:28)
    at Test.run (http://localhost/node_modules/mocha/mocha.js:3320:32)
    at Runner.runTest (http://localhost/node_modules/mocha/mocha.js:3632:10)
    at http://localhost/node_modules/mocha/mocha.js:3678:12
    at next (http://localhost/node_modules/mocha/mocha.js:3560:14)
    at http://localhost/node_modules/mocha/mocha.js:3569:7
    at next (http://localhost/node_modules/mocha/mocha.js:3517:23)
anacronw commented 12 years ago

I'm closing - cannot reproduce in a difference instance so I'm guessing it was an environment error

logicalparadox commented 12 years ago

It looks like you were using the chai-jquery plugin. Basically, the paradox you have here is that in order for chai-jquery to work, jquery must be in the global namespace. See this.

anacronw commented 12 years ago

I had some setup code that tore it down temporarily with $.noConflict(true). Its true..that this is one difference between the two environments I tested on where one worked and the other threw an error.

I didn't realize chai-jquery was overriding the exist method which I thought belonged to chai. Oops! Perhaps that was exactly the issue there as you say. Is there a workaround to specifically test the non-existence of jQuery while using jquery-chai? Perhaps if I do something like:

chai.assert(undefined === jQuery)

Thanks in advance

logicalparadox commented 12 years ago

I think what you are looking for is ...

window.should.not.have.property('jQuery');

But I'm not entirely sure what your trying to do. Note that if you are taking jQuery out of the global namespace you should not load the chai-jquery plugin. If you are using chai-jquery for something, you will need to use a separate file for your test checking for non-existence in the global namespace.

Hope that helps.

anacronw commented 12 years ago

I have some library that injects jQuery as a dependency if its not there. I wanted to test this functionality in the specs, so I have to temporarily remove the jQuery namespace for that test.

Thanks! I will give that a try although I swear I tried that exactly line previously..didn't work the first time. Is there a subject to test in that line since should and window.should are equivalent?

logicalparadox commented 12 years ago

when you load the should interface you should be doing...

var should = chai.should();

If your not doing that, then that could cause problems as well. If you find that your unsure, switch to the expect interface for a given test. You can mix and match if you wish.

expect(jQuery).to.not.exist;
anacronw commented 12 years ago

Just for reference.. As I had thought window.should.not.have.property('jQuery') returns with an error saying that you can't use property on undefined (since there is no subject in that statement).

chai.assert(undefined===jQuery) worked for me, I'm sure the expect interface would actually have been an easier solution.

Thanks!