Pierre
Pierre

Reputation: 1227

Trying to use Jasmine spies and getting "is not a function" errors

Here is my class:

class Clock {

  // Runs callback while it continues
  // to returns truthy values

  constructor(callback, delay) {
    this.callback = callback;
    this.delay = delay;
    this.timerId = null;
  }

  start() {
    this.timerId = setTimeout(() => {
      if (this.callback.call()) this.start();
    }, this.delay);
  }

  // Want to kill some time?

  kill() {
    clearTimeout(this.timerId);
  }

}

export default Clock;

And the relevant tests:

import Clock from '../../src/js/lib/clock.js';

describe("Clock", function() {

  it("Has a callback and a delay", function() {
    let clock = ClockFactory();
    expect(clock.callback).toBeDefined();
    expect(clock.delay).toBeDefined();
  });

  describe("Is a clock", function() {
    let observer,
    clock;

    beforeEach(function() {
      jasmine.clock().install();

      observer = { callback: function() {} };
      spyOn(observer, 'callback')
      console.log(observer)

      clock = ClockFactory({callback: observer});
    });

    afterEach(function() {
      jasmine.clock().uninstall();
      clock.kill()
    });

    it("It periodically executes a callback", function() {
      clock.start()
      expect(observer.callback).not.toHaveBeenCalled();

      jasmine.clock().tick(5001);
      expect(observer.callback.calls.count()).toEqual(1);

      jasmine.clock().tick(2500);
      expect(observer.callback.calls.count()).toEqual(1);

      jasmine.clock().tick(2500);
      expect(observer.callback.calls.count()).toEqual(2);
    });

    it("It can stop ticking", function() {
      clock.start()
      expect(observer.callback).not.toHaveBeenCalled();

      jasmine.clock().tick(5001);
      expect(observer.callback.calls.count()).toEqual(1);

      clock.kill()

      jasmine.clock().tick(5000);
      expect(observer.callback.calls.count()).toEqual(1);
    });

  });

  function ClockFactory(options = {}) {
    return new Clock(
      (options.callback || function() {}),
      options.delay || 5000
      );
  }

});

Now when I run the tests, I get the following failures:

Failures:
1) Clock Is a clock It periodically executes a callback
  Message:
    TypeError: _this.callback.call is not a function
  Stack:
    TypeError: _this.callback.call is not a function
        at clock.js:14:25
        at Object.<anonymous> (clock_spec.js:34:23)

2) Clock Is a clock It can stop ticking
  Message:
    TypeError: _this.callback.call is not a function
  Stack:
    TypeError: _this.callback.call is not a function
        at clock.js:14:25
        at Object.<anonymous> (clock_spec.js:48:23)

I think I'm using spies correctly based on examples, but evidently there is problem! Any suggestions?

Upvotes: 1

Views: 1504

Answers (1)

Igor Raush
Igor Raush

Reputation: 15240

Your Clock constructor expects a function (or at least an object with a call() method) as its first argument. In your tests, you are invoking ClockFactory like

observer = { callback: function () {} };
spyOn(observer, 'callback');
clock = ClockFactory({ callback: observer });

i.e. the first argument being passed to ClockFactory is

{ callback: { callback: function () {} } }

This is most likely not what you intended (unless your ClockFactory signature does not match your Clock constructor signature). Try doing something like

observer = jasmine.createSpy('observer');
clock = ClockFactory(observer);

Alternatively, you can use the "options object" pattern in your Clock constructor and write something like

constructor({callback, delay}) {
  this.callback = callback;
  this.delay = delay;
  ...
}

Then, you can pass a single object with callback and delay properties, like

observer = { callback: function () {} };
spyOn(observer, 'callback');
clock = ClockFactory(observer);

Upvotes: 1

Related Questions