Jasmine calls.length and callCount are undefined
Asked Answered
P

2

35

I'm trying to test my chrome extension with Jasmine but I'm having trouble getting calls.length and callCount to behave as expected. Both cases return undefined.

I've included a sample of the code and the spec. Here's the rest of the code if it helps: https://github.com/DruRly/kamikaze/tree/closeIdleTab

How to reproduce:

  • git clone https://github.com/DruRly/kamikaze/tree/closeIdleTab
  • cd kamikaze
  • open SpecRunner.html

spec/kamikazeSpec.js

describe("kamikaze", function() {
  describe("closeIdleTabs", function(){
    it("calls closeIdleTab for each tab received", function(){
      spyOn(kamikaze, 'closeIdleTab');

      kamikaze.closeIdleTabs([1,2,3]);
      expect(kamikaze.closeIdleTab.calls.length).toBe(3);
    })
  })
})

src/kamikaze.js

kamikaze = {
  ...

  closeIdleTabs: function(tabs){
    tabs.forEach(function(tab){
      test.closeIdleTab(tab);
    })
  },

  closeIdleTab: function(tab){
    if(tabTimeStamps[tab.id]){
      var secondsSinceUpdated = getSecondsSinceUpdated(tab.id)
      if(secondsSinceUpdated > (minutesUntilIdle * 60)){
        chrome.tabs.remove(tab.id)
      }
    }
  },

  ...
}
Pigpen answered 7/1, 2014 at 13:12 Comment(3)
+10 for the quality and format of the question. Most people would have stopped after the first 2 lines (literally) ! (Relevant code, link to full code and steps to reproduce the issue - I hate the fact it was so easy to answer this one :))Decorative
Thanks @ExpertSystem! Felt like I was going crazy. I still may be but I was also looking at outdated docs :)Pigpen
It would be nice if the repo was still available for reference.Thus
D
59

The Jasmine APIs have changed a bit in the 2.x version "series".
According to the latest docs you should use the count() method:

expect(kamikaze.closeIdleTab.calls.count()).toBe(3);

I also tried that with your code and all tests pass successfully.

Decorative answered 7/1, 2014 at 13:41 Comment(0)
G
13

Alternative syntax (with a bit nicer output on failure) would be

expect(kamikaze.closeIdleTab).toHaveBeenCalledTimes(3);

Here is some documentation.

Glyconeogenesis answered 17/11, 2017 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.