what is the equivalent of jasmine.createSpyObj() for jest?

Viewed 2799

What is the equivalent of the below code in Jest.

let mockHeroService = jasmine.createSpyObj(['getHeros', 'addHero', 'deleteHero']);

I would like to use it the testBed.

TestBed.configureTestingModule({
  providers: [
    {
       provide: HeroService,
       useValue: mockHeroService         
    }
  ]
});

My understanding is that, with jest, you can only spy on one method of a service like

const spy = jest.spyOn(HeroService, 'getHeros');

Thanks for helping

1 Answers

There's no equivalent because it doesn't have much uses. Jest is focused on modular JavaScript and generates auto-mocks (stubs) with jest.mock and jest.createMockFromModule.

The problem with auto-mocks is that they result in unspecified set of functions that behave differently from original ones and can make the code that uses them to work incorrectly or silently fail.

A mock with no implementation can be defined as:

let mockHeroService = { getHeros: jest.fn(), ... };

Most times some implementation is expected:

let mockHeroService = { getHeros: jest.fn().mockReturnValue(...), ... };
Related