bessdsv / karma-jasmine-jquery

Jasmine-jquery plugin for Jasmine in Karma
Other
18 stars 55 forks source link

jquery version problem with spyOnEvent #5

Open stuff opened 9 years ago

stuff commented 9 years ago

Hi,

I'm not very aware of the internals of jquery events, but it seems there's a problem with spyOnEvent. I'm using a version of jQuery in my tested code loaded via karma.set, in the "files" list. I'm using Browserify too, but jquery is used via the global $ var in my modules.

I can't manage to get spyOnEvent working... Even a simple test like this is failling:

setFixtures(sandbox().html('<a id="clickme">Click Me</a>'));
var $element = $('#clickme');
var spy = spyOnEvent($element, 'click');
$element.click();
expect(spy).toHaveBeenTriggered();

After digging a bit, I've found that I'm using a different version of jquery (more recent) and spyOnEvent is using it's own version (as specified here https://github.com/bessdsv/karma-jasmine-jquery#information) so everything looks "normal"

The fact is, if I switch from $('#clickme') to $j('#clickme'), to use the "internal" karma-jasmine-jquery jquery, the test pass just fine. Of course, my code can't use that version.

To be clear, it's not specifically the jquery version causing that problem; I tried to use the same exact version (2.1.1) in my own code, and it's not working. Seems that some internal data are specific to each jquery loaded or something like that.

Did some else have this problem ?

bessdsv commented 9 years ago

@STuFF You don't need to use $j in the application code. Your application should use the version you are using ($). In the tests, you can use as much as $ version(for emulation code as from application) and $j version(for test code). The fact that Jasmine-jquery for their needs requires 2.1.1 or later version.

stuff commented 9 years ago

Yes I know that, but something prevents me to test events with spyOnEvent, I can't find what :(

rincedd commented 9 years ago

The problem is indeed that karma-jasmine-jquery spies on events managed by its own jQuery version $j, so it does not know about events triggered by the application's jQuery. Using jasmine-jquery directly works for me, but if I use karma-jasmine-jquery all event-related tests break.

stuff commented 9 years ago

thank you, that's what I thought maybe I should try direct jasmine-jquery.

Evan-M commented 8 years ago

@rincedd: I'm also having this problem, but I'm not sure I understand what you mean by using jasmine-jquery directly? Are you saying to not use karma-jasmine-jquery at all, and to manually set up jasmine-jquery instead?

rincedd commented 8 years ago

@Evan-M exactly, that is what I did for the moment, I included jasmine-jquery in the list of loaded files in karma.conf.js.

boldfacedesign commented 8 years ago

@rincedd can you post the part of your karma.conf.js where you're loading in jasmine-jquery please? I was giving this a go but couldn't get it to work.

rincedd commented 8 years ago

@boldfacedesign for me it was as simple as adding 'bower_components/jasmine-jquery/lib/jasmine-jquery.js' to the files section in karma.conf.js, after all the jquery files but before the specs.

boldfacedesign commented 8 years ago

Thanks, that's what I tried but it didn't work for me, but I've actually stopped using karma-jasmine-jquery after reading the jasmine-jquery source code. I can mimic the checks I want right in my tests pretty easily so I'm just doing that.

andrewhao commented 8 years ago

I got this error, and traced it down to mixing up the load order in the frameworks section of the karma config:

frameworks: ['jasmine', 'jasmine-jquery'],

error:

PhantomJS 1.9.8 (Mac OS X 0.0.0) ERROR
  TypeError: 'undefined' is not an object (evaluating 'jasmine.spiedEventsKey = function (selector, eventName) {
      return [$(selector).selector, eventName].toString()
    }')
  at /Users/andrewhao/workspace/shmile-ui/node_modules/karma-jasmine-jquery/lib/jasmine-jquery.js:34
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0.077 secs / 0 secs)

Swapping the order worked:

frameworks: ['jasmine-jquery', 'jasmine'],