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

jquery version problem with spyOnEvent #5

Open
stuff opened this issue Apr 21, 2015 · 10 comments
Open

jquery version problem with spyOnEvent #5

stuff opened this issue Apr 21, 2015 · 10 comments

Comments

@stuff
Copy link

stuff commented Apr 21, 2015

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
Copy link
Owner

bessdsv commented Apr 23, 2015

@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
Copy link
Author

stuff commented Apr 23, 2015

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

@rincedd
Copy link

rincedd commented Aug 6, 2015

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
Copy link
Author

stuff commented Aug 6, 2015

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

@Evan-M
Copy link

Evan-M commented Nov 17, 2015

@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
Copy link

rincedd commented Nov 18, 2015

@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
Copy link

@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
Copy link

rincedd commented Dec 1, 2015

@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
Copy link

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
Copy link

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'],

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

No branches or pull requests

6 participants