Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:35:24+00:00 2026-05-30T14:35:24+00:00

I don’t know how to run this Jasmine test for my JS and surely

  • 0

I don’t know how to run this Jasmine test for my JS and surely also other ppl have this problem. Maybe I’m doing st wrong or maybe it’s impossible – I didn’t find any hint on this. The problem has to do with the fact that – in jQuery – $(this) is not the same as the element chosen by e.g. $(“#this-id”):

Javascript:

[..]
$("#button-id").on("click", function(e) { callSomeFunctionWith( $(this) ); } );

Jasmine-Test (CoffeeScript):

[..]
spyOn some.object, "callSomeFunctionWith"
spyOnEvent( $("#button-id"), 'click' )

$("#button-id").trigger( "click" )
expect( some.object.callSomeFunctionWith ).toHaveBeenCalledWith( $("#button-id") )

Unfortunately this test fails (with any variation like storing the ref to in a variable first in my Jasmine test), because the function is NOT called with $(“#button-id”), but instead is called with $(this), and $(this) != $(“#button-id”).

Can anybody tell me how to accomplish this test? I’m quite lost. Even Remy Sharp’s great article on jQuery and $(this) didn’t get me any further.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:35:26+00:00Added an answer on May 30, 2026 at 2:35 pm

    Ok, now I’ve got the solution to my problem. The solution is easy, the explanation not. I’ll explain the solution from scratch.

    This is my Javascript code with jQuery that I want to test using jasmine-jquery:

    $( "input.toggler" ).on( "click", function( e ) {
      [...]
      doSomethingWith( $(this) );
    } );
    

    And now using Jasmine-jQuery I want to ensure that the JS function “doSomethingWith” gets called with the correct “$(this)”.

    First one might think that $(this) === $( “input.toggler” ), but that is not true.
    Inside the callback function of the click handler, the $(this) jQuery uses is neither the jQuery object $( “input.toggler” ) nor the DOM element referenced by that object.
    As Remy Sharp explains in his really nice article “jQuery’s this: demystified“, the “this” inside the callback function is the DOM element, but $(this) creates a jQuery object from that DOM element. And that is not identical to the jQuery object $( “input.toggler” ).

    So if you want to test this with Jasmine using the function “toHaveBeenCalledWith”, you have to first extract the DOM element using either document.getElementById(…) or else document.getElementsByTagName(…)[INDEX] (where INDEX is the index of the element you want, since the latter function gives you an array of DOM elements), which is plain old Javascript.
    Then, when you have extracted the DOM element wanted, you have to create a jQuery-object from it by enclosing it in $( and ).

    My passing Jasmine-jQuery-test finally looks something like this (using Coffeescript):

    it "does something with my input element", ->
      DOM_input_element = document.getElementsByTagName( "input" )[0] # Choose the correct DOM element here
    
      spyOn myobject.functions, "doSomethingWith"
      spyOnEvent( $( 'input.toggler' ), 'click' )
    
      [...]
    
      $( 'input.toggler' ).trigger( 'click' )
    
      # Check for changes after click:
      expect( myobject.functions.doSomethingWith ).toHaveBeenCalledWith( $( DOM_input_element ) )
    

    So the “$(this)” from my Javascript code translates to “$(DOM_input_element)” in my Jasmine-jQuery test.

    Hopefully this helps you with your projects! It took me quite a while to figure this out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if this has been answered before. Have custom routes to users. If
Don't know why but I can't find a solution to this. I have 3
I don't know if the correct title for this is hot deploy. I am
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know if anyone can help me with this or if it's even possible.
I don't know: if this works. if it's a good idea. what it is
I don't know if this question is trivial or not. But after a couple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.