Jest createSpyObj

jest mock module
jest spyon mockimplementation
jest tohavebeencalled
jest settimeout
jest mock imported function
jest mock axios
jest mock interface

With Chai, you can create a spy object as follows:

chai.spy.object([ 'push', 'pop' ]);

With jasmine, you can use:

jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);

What's the Jest equivalent?

Context: I am currently migrating a (typescript) Jasmine tests to (typescript) Jest. The migration guide is basically useless in this case: As with any relatively new tech, there's nothing that can easily be found in the docs about this.

I've written a very quick createSpyObj function for jest, to support the old project. Basically ported from Jasmine's implementation.

export const createSpyObj = (baseName, methodNames): { [key: string]: Mock<any> } => {
    let obj: any = {};

    for (let i = 0; i < methodNames.length; i++) {
        obj[methodNames[i]] = jest.fn();

    return obj;

Jest createSpyObj, I've written a very quick createSpyObj function for jest, to support the old project. Basically ported from Jasmine's implementation. export const  jest-createspyobj. Helper function to create spy object for jest, same as jasmine.createSpyObj. USAGE

const video = {
  play() {
    return true;

module.exports = video;

And the test:

const video = require('./video');

test('plays video', () => {
  const spy = jest.spyOn(video, 'play');
  const isPlaying =;



Docs found here:

There is also jest.fn()

const mockFn = jest.fn();

  // With a mock implementation:
  const returnsTrue = jest.fn(() => true);
  console.log(returnsTrue()); // true;

jest-createspyobj, Helper function to create spy object for `jest`, same as `jasmine.createSpyObj` The jest object is automatically in scope within every test file. The methods in the jest object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by via import {jest} from '@jest/globals'.

David's answer helped to get me on the right track. I modified it to work with ionic-mocks ( in my Ionic3/Angular4 project.

In my test "helper" class, I have this:

export function  createSpyObj (baseName: string, methodNames: string[]): { [key: string]: jasmine.Spy } {
  const obj: any = {}
  for (let i: number = 0; i < methodNames.length; i++) {
    obj[methodNames[i]] = jasmine.createSpy(baseName, () => {})
  return obj

Then I'm able to use it as such in my test/spec file. I inject the provider in question as:

{ provide: AlertController, useFactory: () => AlertControllerMock.instance() },

And until ionic-mocks is compatible with Jest, I have to copy over the mocks I want (which use createSpyObj):

class AlertMock {
  public static instance (): any {
    const instance: any = createSpyObj('Alert', ['present', 'dismiss'])

    return instance

class AlertControllerMock {
  public static instance (alertMock?: AlertMock): any {

    const instance: any = createSpyObj('AlertController', ['create'])
    instance.create.and.returnValue(alertMock || AlertMock.instance())

    return instance

unlight/jest-createspyobj: Helper function to create spy , Helper function to create spy object for `jest`, same as `jasmine.createSpyObj`. jest spy jasmine createspyobj. Star 6. Watch  First you’ll need to install the dependency. The package is called jest-createspyobj (on NPM jest-createspyobj) - which is done by executing the following command:

Testing Angular with jest, spyOn ; other mocks will require you to manually restore them. jest.resetModules​() #. Resets the module  jasmine.createSpyObj('name', ['key']) --> jest.fn({key: jest.fn()}) remove @types/jasmine module (duplicate TS declarations from @types/jest) After porting jasmine.createSpyObj() you can come back later to migrate rest of the functions, which are optional at the time of writing (this may change in the future): jasmine.createSpy('name') --> jest

Migrating from Jasmine to Jest – Nick Smith – Web Developer, Last year I learned about Jest, a testing framework. a mock object using Jasmine's createSpyObj-function and passing some parameters in it. Jest uses a custom resolver for imports in your tests, making it simple to mock any object outside of your test’s scope. You can use mocked imports with the rich Mock Functions API to spy on function calls with readable test syntax.

Jest .fn() and .spyOn() spy/stub/mock assertion reference · Code with , Since the current application I help build runs about >1200 (and growing) Jasmine unit-tests, I figured migrating to Jest would be a pretty  In February 2017, they merged a PR adding this feature, they released in April 2017. so to spy on getters/setters you use: const spy = spyOnProperty(myObj, 'myGetterName', 'get'); where myObj is your instance, 'myGetterName' is the name of that one defined in your class as get myGetterName() {} and the third param is the type get or set.

  • Haha, I know how to create a spy :P Your example shows how to attach one. So, in order to do what I have done, you need to add four lines. Not one - also the context is switching from an old project, so rewriting 500 tests would not really be a good idea.