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: https://facebook.github.io/jest/docs/migration-guide.html As with any relatively new tech, there's nothing that can easily be found in the docs about this.
And the test:
Docs found here: https://facebook.github.io/jest/docs/en/jest-object.html#jestspyonobject-methodname
There is also jest.fn()
https://facebook.github.io/jest/docs/en/jest-object.html#jestfnimplementation
I've written a very quick createSpyObj function for jest, to support the old project. Basically ported from Jasmine's implementation.
David
's answer helped to get me on the right track. I modified it to work with ionic-mocks (https://github.com/stonelasley/ionic-mocks) in my Ionic3/Angular4 project.In my test "helper" class, I have this:
Then I'm able to use it as such in my test/spec file. I inject the provider in question as:
And until ionic-mocks is compatible with Jest, I have to copy over the mocks I want (which use
createSpyObj
):