Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stub entire Object in Javascript using Sinon

I am looking for an example for the most trivial sinon use case: mock an entire Object / Array without methods, static properties only, with another one.

I cannot seem to find how to achieve this using sinon. Pseudo code explanation:

sinon.mock(originalObj, myObj); // do tests using originalObj = myObj... sinon.restore(originalObj);

Motivation: while this can be achieved easily with a swap variable, things become somewhat ugly if you have to mock several objects and the mock and restore are in different scopes - then you start using a swap array residing in a parent scope.

like image 203
B M Avatar asked Aug 04 '26 03:08

B M


1 Answers

just use sinon.stub(myObj) and you will get an object where each property is a stub

like image 67
SystematicFrank Avatar answered Aug 06 '26 17:08

SystematicFrank