jest-spy-object
Spies on all Object or Class methods using jest.spyOn
.
Why?
If you're sick of writing jest.spyOn(Object, 'method')
all the time.
And you want the following:
- You need to spy on some or all methods of an Object or Class
- You need to define mocks for some methods inside tests (
mockImplementation
ormockReturnValue
) - You need each test to have fresh spies without calling
mockRretore
ormockReset
on each spy - You don't want to manually add a spy for newly added methods
Installation
Install as a dev dependency of your project:
yarn add -D jest-spy-object
Or with npm
npm i -D jest-spy-object
Require
const spyObject = ;
Usage
Given a Class
{ thisvalue = 0; } { thisvalue = thisvalue + val; } { this; }
Before
;
After
const spyObject = ; ;
Tests
;
Other supported inputs
Function constructor
{ thisvalue = 0;} Counterprototype { thisvalue = thisvalue + val;}; Counterprototype { this;}; ;// or...;
Plain Object
const counter = value: 0 { thisvalue = thisvalue + val; } { this; }; ;
Contributing
See the CONTRIBUTING document.