DEV Community

Steven Liao
Steven Liao

Posted on

6 1

Jest for Sinon Stubs

At work, we recently converted from Mocha, Karma, Chai, and Sinon to Jest. It was not immediately obvious what the direct replacement was for

sinon.stub(object, 'methodName').callsFake(() => {})
Enter fullscreen mode Exit fullscreen mode

Found out Jest provides similar functionality with

jest.spyOn(object, 'methodName').mockImplementation(() => {})
Enter fullscreen mode Exit fullscreen mode

Top comments (7)

Collapse
 
afsarzan profile image
Afsar

How to handle it if there is callback ?

Collapse
 
2ezpz2plzme profile image
Steven Liao

What callback are you talking about? Got an example?

Collapse
 
afsarzan profile image
Afsar
sinon.stub(object, 'methodName').callsFake((param,cb) => {
cb(param);
})
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
2ezpz2plzme profile image
Steven Liao
jest.spyOn(object, 'methodName').mockImplementation((param, cb) => {
  cb(param)
})
Enter fullscreen mode Exit fullscreen mode

Does that work?

Thread Thread
 
afsarzan profile image
Afsar

No. It says this expression is not callable. cb: unknow param

Collapse
 
hellixum profile image
SANKALP PANDEY

ThankYou very much... I have been looking for this exact thing for hours on internet
.... you saved my day

Collapse
 
danieldfc profile image
Daniel Felizardo

Thank you very much, I spent a lot of time trying to figure out how to solve this kind of problem.
Here's the solution.

PulumiUP 2025 image

PulumiUP 2025: Cloud Innovation Starts Here

Get inspired by experts at PulumiUP. Discover the latest in platform engineering, IaC, and DevOps. Keynote, demos, panel, and Q&A with Pulumi engineers.

Register Now

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay