Andrew Shenstone
Andrew Shenstone

Reputation: 281

Jasmine 2.0 compatibility with Jasmine-Jquery

I am having difficulty running my Jquery tests with the jasmine-jquery plugin. Here are the steps I have taken:

  1. gem install jasmine
  2. jasmine init
  3. add jquery and jasmine-jquery to the helpers folder
  4. Here is my repository https://github.com/shenst1/jasminesample

Here are my specs as recommended following these tutorials: https://www.youtube.com/watch?v=3Huh44nsZTw

describe("My Feature", function() {
  it("should add numbers", function() {
    expect(1+1).toBe(2);
   });
 });
describe("experimentation", function() {
  var elem;
  beforeEach(function() {
  elem = $('<div id="container"><p>Hello World</p></div>');
  });
  it("allows us to serach with Css selectors", function() {
   expect(elem).toBe('#container');
   expect(elem).toContainElement('p');
    expect(elem).toEqual('#container');
   expect(elem).toEqual('p')
  });
 });

The first spec passes as expected but the second ones with jquery fail with the following errors:

    Expected { 0 : HTMLNode, length : 1 } to be '#container'.
    Error: Expected { 0 : HTMLNode, length : 1 } to be '#container'.
    at stack (http://localhost:8888/__jasmine__/jasmine.js:1293:17)
    at buildExpectationResult (http://localhost:8888/__jasmine__/jasmine.js:1270:14)
    at Spec.Env.expectationResultFactory        (http://localhost:8888/__jasmine__/jasmine.js:484:18)
    at Spec.addExpectationResult (http://localhost:8888/__jasmine__/jasmine.js:260:46)
    at Expectation.addExpectationResult (http://localhost:8888/__jasmine__/jasmine.js:442:21)
    at Expectation.toBe (http://localhost:8888/__jasmine__/jasmine.js:1209:12)
    at Object.<anonymous> (http://localhost:8888/__spec__/testSpec.js:12:18)
    at attemptSync (http://localhost:8888/__jasmine__/jasmine.js:1510:12)
    at QueueRunner.run (http://localhost:8888/__jasmine__/jasmine.js:1498:9)
    at QueueRunner.execute (http://localhost:8888/__jasmine__/jasmine.js:1485:10)
TypeError: Object #<Expectation> has no method 'toContainElement'
TypeError: Object #<Expectation> has no method 'toContainElement'
    at Object.<anonymous> (http://localhost:8888/__spec__/testSpec.js:13:18)
    at attemptSync (http://localhost:8888/__jasmine__/jasmine.js:1510:12)

It looks like the jquery matchers are not loading in properly. It appears it is converting the object to a string rather than check the html elements. I tried to use the jasmine-jquery.js version from the Support Jasmine v2.0 (version 1.5.92), but that failed with the same errors. Do I need to roll back jasmine versions to support Jquery or am I just missing something about how to make it work with jquery? Thanks, Andrew

Upvotes: 2

Views: 2062

Answers (1)

travisjeffery
travisjeffery

Reputation: 146

i maintain jasmine jquery, I just added support for jasmine v2 a few mins ago. https://github.com/velesin/jasmine-jquery/

Upvotes: 3

Related Questions