TIL: How to Correctly Use a Jasmine Spy

At Gaslight we are all about continuously growing and learning, so we’re starting a “Today I Learned Series”! Stay tuned for more tidbits!

I often find myself stumped when trying to use spies in a JavaScript test… Until Today.

TIL: How to correctly use a Jasmine Spy:

Foo = require('foo')

  describe 'Foo'
    describe 'bar'
      it 'does a thing'
        spyOn(Foo, 'bar')
        foo = new Foo()

        expect(foo.bar).toHaveBeenCalled()

Run into no method error? Try spyOn(Foo.prototype, 'bar').

  • Jasmine is a popular JavaScript testing framework
  • Spies are a type of test double that allow you to stub functions and track the function call, and it’s arguments

For more information on Jasmine Spies, visit their documentation.

So you want to be a developer

We're always looking for enthusiastic, high aptitude beginners trying to enter the field of software development

Start your journey into development