186

I'm using Jasmine and have a library js file with lots of functions which are not associated with any object (i.e. are global). How do I go about spying on these functions?

I tried using window/document as the object, but the spy did not work even though the function was called. I also tried wrapping it in a fake object as follows :

var fakeElement = {};
fakeElement.fakeMethod = myFunctionName;
spyOn(fakeElement, "fakeMethod");

and test with

expect(fakeElement.fakeMethod).toHaveBeenCalled();

This does not work either as the spy did not work.

10 Answers 10

170

If you are defining your function:

function test() {};

Then, this is equivalent to:

window.test = function() {}  /* (in the browser) */

So spyOn(window, 'test') should work.

If that is not, you should also be able to:

test = jasmine.createSpy();

If none of those are working, something else is going on with your setup.

I don't think your fakeElement technique works because of what is going on behind the scenes. The original globalMethod still points to the same code. What spying does is proxy it, but only in the context of an object. If you can get your test code to call through the fakeElement it would work, but then you'd be able to give up global fns.

7
  • 2
    It worked! I think the error I was making earlier was that I was calling the spyOn with method() instead of method. Thanks! Mar 1, 2012 at 8:09
  • 3
    I've had some problems using spyOn(window, 'test') using chutzpah for running the tests as part of our automation due to 'window' not being assigned. Using jasmine.createSpy() got around this.
    – Henners
    Sep 24, 2013 at 13:43
  • 7
    jasmine.createSpy() worked perfectly for me. Thanks!
    – dplass
    Jul 24, 2014 at 0:34
  • 1
    used test = jasmine.createSpy(); to spy on angularJs $anchroScroll worked perfectly
    – Subtubes
    Mar 17, 2015 at 21:20
  • 1
    For some reason I can't get either way to work, but it may be entirely possible that it's because I'm trying to mock up an existing window function; $window.open(url, '_blank'); with the intention of opening a new tab (or window depending on browser setup). How should I be going about making sure it's calling this function and verifying that it's navigating to the right url regardless of the browser?
    – CSS
    Aug 19, 2015 at 15:55
104

TypeScript users:

I know the OP asked about javascript, but for any TypeScript users who come across this who want to spy on an imported function, here's what you can do.

In the test file, convert the import of the function from this:

import {foo} from '../foo_functions';

x = foo(y);

To this:

import * as FooFunctions from '../foo_functions';

x = FooFunctions.foo(y);

Then you can spy on FooFunctions.foo :)

spyOn(FooFunctions, 'foo').and.callFake(...);
// ...
expect(FooFunctions.foo).toHaveBeenCalled();
10
  • 3
    Thanks for the TypeScript hint. Should be equally the same for ES6/Babel, but I haven't tried it.
    – hgoebl
    May 29, 2017 at 12:09
  • 3
    Seems it only works if calling the function explicitly with the alias FooFunctions. I have a function bar() that is a factory returning baz() and want to test that baz() calls foo(). This method doesn't appear to work in that scenario. Jul 11, 2017 at 22:46
  • 8
    This will work if the alias is taken inside foo_functions export const FooFunctions = { bar, foo }; and the import in the test becomes import { FooFunctions } from '../foo_functions'. However, the alias still needs to be explicitly used within foo_functions private implementation for the spy to work. const result = FooFunctions.foo(params) // spy reports call const result = foo(params) // spy reports no call Jul 11, 2017 at 23:28
  • 2
    Worked like a charm! Thanks, you saved me a lot of time!
    – SrAxi
    Jan 5, 2018 at 13:33
  • 38
    This is not working anymore got a Error: <spyOn> : parseCookie is not declared writable or has no setter
    – Ling Vu
    May 14, 2020 at 6:58
52

There is 2 alternative which I use (for jasmine 2)

This one is not quite explicit because it seems that the function is actually a fake.

test = createSpy().and.callFake(test); 

The second more verbose, more explicit, and "cleaner":

test = createSpy('testSpy', test).and.callThrough();

-> jasmine source code to see the second argument

1
  • This makes a little more sense and breaks it out far enough to duplicate with success. +1 from me. Thanks, C§
    – CSS
    Aug 19, 2015 at 20:00
9

A very simple way:

import * as myFunctionContainer from 'whatever-lib';

const fooSpy = spyOn(myFunctionContainer, 'myFunc');
0
3

Solution with TypeScript

Replace your import:

import { yourGlobalFunction } from '../your-global-functions';

with:

import * as Functions from '../your-global-functions';

Then:

spyOnProperty(Functions, 'yourGlobalFunction').and.returnValue(() => 'mock return value');
1
import * as saveAsFunctions from 'file-saver';
..........
....... 
let saveAs;
            beforeEach(() => {
                saveAs = jasmine.createSpy('saveAs');
            })
            it('should generate the excel on sample request details page', () => {
                spyOn(saveAsFunctions, 'saveAs').and.callFake(saveAs);
                expect(saveAsFunctions.saveAs).toHaveBeenCalled();
            })

This worked for me.

1
  • 4
    Please add explanation to your answer, code by itself is not that helpful for the person asking the question if they cannot understand what is going on.
    – chevybow
    Jun 18, 2018 at 18:56
1

The approach we usually follow, is as follows:

utils.ts file for all global utilities:

function globalUtil() {
  // some code
}

abc.component.ts:

function foo {
  // some code
  globalUtil();  // calls global function from util.ts
}

While writing a Jasmine test for function foo (), you can spy on the globalUtil function as follows:

abc.component.spec.ts:

import * as SharedUtilities from 'util.ts';

it('foo', () =>
{
  const globalUtilSpy = jasmine.createSpy('globalUtilSpy');
  spyOnProperty(SharedUtilities, "globalUtilSpy").and.returnValue(globalUtilSpy);
  
  foo();
  expect(globalUtilSpy).toHaveBeenCalled();
});
0

My answer differs slightly to @FlavorScape in that I had a single (default export) function in the imported module, I did the following:

import * as functionToTest from 'whatever-lib';

const fooSpy = spyOn(functionToTest, 'default');
1
  • 1
    I did and it resulted in this error: Error: <spyOn> : default is not declared writable or has no setter. After some research it seem this solution only works on es5 Nov 2, 2022 at 0:48
0

I found a new way because the suggested solutions don't work for me :( So you can do it like this:

import * as FooFunctions from 'foo-functions';

spyOnProperty(FooFunctions, 'foo').and.returnValue(jasmine.createSpy());

If you want do callThrough:

import * as FooFunctions from 'foo-functions';

const originalFn = FooFunctions.foo;
spyOnProperty(FooFunctions, 'foo').and.returnValue(
    jasmine.createSpy().and.callFake(originalFn)
);

To make it more convenient, I made a helper. You can use it like this:

import * as FooFunctions from 'foo-functions';

spyOnFunction(FooFunctions, 'foo'); // to call through
spyOnFunction(FooFunctions, 'foo').and.callFake(...) // to call fake
spyOnFunction(FooFunctions, 'foo').and... // to do something else

Here is the helper code:

function spyOnFunction<T, K extends keyof T>(source: T, originalFnKey: K): jasmine.Spy {
    const originalFn: T[K] = source[originalFnKey];

    if (!isFunction(originalFn)) {
        throw new Error('[spyOnFunction] spy target must be a function');
    }

    const spy: jasmine.Spy = jasmine.createSpy().and.callFake(originalFn);

    spyOnProperty(source, originalFnKey).and.returnValue(spy);

    return spy;
}

function isFunction(item: unknown): item is (...args: unknown[]) => unknown {
    return typeof item === 'function';
}
-1

I guess it's the easiest way:

const funcSpy = spyOn(myFunc, 'call');
1
  • I can't get this to work
    – Lord Midi
    Mar 4, 2022 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.