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.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay