Stubbing with sinon in jest
I have all my unit tests in jests.
Keen to use sinon stubs for some tests. Can sinon be used with jest as test runner? Couldn't find much on the internet about combining these two
The problem I am trying solve is similar to the below code
const Meme = require('./Meme');
module.exports.allMemes = function(req, res) {
Meme.find({ repost: req.params.reposts }, function(err, memes) {
res.send(memes);
});
};
where Meme
is a mongoose Schema
and I would like to stub the Meme.find()
function so that I don't have to deal with the problematic part of mongoDB return
Any guidance?
javascript mongoose jestjs sinon
add a comment |
I have all my unit tests in jests.
Keen to use sinon stubs for some tests. Can sinon be used with jest as test runner? Couldn't find much on the internet about combining these two
The problem I am trying solve is similar to the below code
const Meme = require('./Meme');
module.exports.allMemes = function(req, res) {
Meme.find({ repost: req.params.reposts }, function(err, memes) {
res.send(memes);
});
};
where Meme
is a mongoose Schema
and I would like to stub the Meme.find()
function so that I don't have to deal with the problematic part of mongoDB return
Any guidance?
javascript mongoose jestjs sinon
Yes you can use the two together. Personally I prefer to just use Jest mocks and stubs. One less dependency to manage and I have never seen anything that Sinon does that jest doesn't.
– ruby_newbie
Nov 14 '18 at 3:07
yes, sinon works great with jest and tends to be a bit more refined in areas such as testing calls to spies and fake timers
– brian-lives-outdoors
Nov 14 '18 at 3:50
Yes. I am specially keen on sinon capacity to deal with spies. I am relatively new to unit testing. Do you have any pointers to online resources or any snippets where both sinon and jest are used at the same time. I am hoping the expect() method will be from jest and everything else will be related to sinon
– Theepan Thevathasasn
Nov 14 '18 at 6:19
Please, can you provide an example you like to use spices in?
– Alexander Elgin
Nov 14 '18 at 7:40
@AlexanderElgin I have edited the question :-)
– Theepan Thevathasasn
Nov 14 '18 at 10:32
add a comment |
I have all my unit tests in jests.
Keen to use sinon stubs for some tests. Can sinon be used with jest as test runner? Couldn't find much on the internet about combining these two
The problem I am trying solve is similar to the below code
const Meme = require('./Meme');
module.exports.allMemes = function(req, res) {
Meme.find({ repost: req.params.reposts }, function(err, memes) {
res.send(memes);
});
};
where Meme
is a mongoose Schema
and I would like to stub the Meme.find()
function so that I don't have to deal with the problematic part of mongoDB return
Any guidance?
javascript mongoose jestjs sinon
I have all my unit tests in jests.
Keen to use sinon stubs for some tests. Can sinon be used with jest as test runner? Couldn't find much on the internet about combining these two
The problem I am trying solve is similar to the below code
const Meme = require('./Meme');
module.exports.allMemes = function(req, res) {
Meme.find({ repost: req.params.reposts }, function(err, memes) {
res.send(memes);
});
};
where Meme
is a mongoose Schema
and I would like to stub the Meme.find()
function so that I don't have to deal with the problematic part of mongoDB return
Any guidance?
javascript mongoose jestjs sinon
javascript mongoose jestjs sinon
edited Nov 14 '18 at 10:32
Theepan Thevathasasn
asked Nov 14 '18 at 3:05
Theepan ThevathasasnTheepan Thevathasasn
5819
5819
Yes you can use the two together. Personally I prefer to just use Jest mocks and stubs. One less dependency to manage and I have never seen anything that Sinon does that jest doesn't.
– ruby_newbie
Nov 14 '18 at 3:07
yes, sinon works great with jest and tends to be a bit more refined in areas such as testing calls to spies and fake timers
– brian-lives-outdoors
Nov 14 '18 at 3:50
Yes. I am specially keen on sinon capacity to deal with spies. I am relatively new to unit testing. Do you have any pointers to online resources or any snippets where both sinon and jest are used at the same time. I am hoping the expect() method will be from jest and everything else will be related to sinon
– Theepan Thevathasasn
Nov 14 '18 at 6:19
Please, can you provide an example you like to use spices in?
– Alexander Elgin
Nov 14 '18 at 7:40
@AlexanderElgin I have edited the question :-)
– Theepan Thevathasasn
Nov 14 '18 at 10:32
add a comment |
Yes you can use the two together. Personally I prefer to just use Jest mocks and stubs. One less dependency to manage and I have never seen anything that Sinon does that jest doesn't.
– ruby_newbie
Nov 14 '18 at 3:07
yes, sinon works great with jest and tends to be a bit more refined in areas such as testing calls to spies and fake timers
– brian-lives-outdoors
Nov 14 '18 at 3:50
Yes. I am specially keen on sinon capacity to deal with spies. I am relatively new to unit testing. Do you have any pointers to online resources or any snippets where both sinon and jest are used at the same time. I am hoping the expect() method will be from jest and everything else will be related to sinon
– Theepan Thevathasasn
Nov 14 '18 at 6:19
Please, can you provide an example you like to use spices in?
– Alexander Elgin
Nov 14 '18 at 7:40
@AlexanderElgin I have edited the question :-)
– Theepan Thevathasasn
Nov 14 '18 at 10:32
Yes you can use the two together. Personally I prefer to just use Jest mocks and stubs. One less dependency to manage and I have never seen anything that Sinon does that jest doesn't.
– ruby_newbie
Nov 14 '18 at 3:07
Yes you can use the two together. Personally I prefer to just use Jest mocks and stubs. One less dependency to manage and I have never seen anything that Sinon does that jest doesn't.
– ruby_newbie
Nov 14 '18 at 3:07
yes, sinon works great with jest and tends to be a bit more refined in areas such as testing calls to spies and fake timers
– brian-lives-outdoors
Nov 14 '18 at 3:50
yes, sinon works great with jest and tends to be a bit more refined in areas such as testing calls to spies and fake timers
– brian-lives-outdoors
Nov 14 '18 at 3:50
Yes. I am specially keen on sinon capacity to deal with spies. I am relatively new to unit testing. Do you have any pointers to online resources or any snippets where both sinon and jest are used at the same time. I am hoping the expect() method will be from jest and everything else will be related to sinon
– Theepan Thevathasasn
Nov 14 '18 at 6:19
Yes. I am specially keen on sinon capacity to deal with spies. I am relatively new to unit testing. Do you have any pointers to online resources or any snippets where both sinon and jest are used at the same time. I am hoping the expect() method will be from jest and everything else will be related to sinon
– Theepan Thevathasasn
Nov 14 '18 at 6:19
Please, can you provide an example you like to use spices in?
– Alexander Elgin
Nov 14 '18 at 7:40
Please, can you provide an example you like to use spices in?
– Alexander Elgin
Nov 14 '18 at 7:40
@AlexanderElgin I have edited the question :-)
– Theepan Thevathasasn
Nov 14 '18 at 10:32
@AlexanderElgin I have edited the question :-)
– Theepan Thevathasasn
Nov 14 '18 at 10:32
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292605%2fstubbing-with-sinon-in-jest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292605%2fstubbing-with-sinon-in-jest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Yes you can use the two together. Personally I prefer to just use Jest mocks and stubs. One less dependency to manage and I have never seen anything that Sinon does that jest doesn't.
– ruby_newbie
Nov 14 '18 at 3:07
yes, sinon works great with jest and tends to be a bit more refined in areas such as testing calls to spies and fake timers
– brian-lives-outdoors
Nov 14 '18 at 3:50
Yes. I am specially keen on sinon capacity to deal with spies. I am relatively new to unit testing. Do you have any pointers to online resources or any snippets where both sinon and jest are used at the same time. I am hoping the expect() method will be from jest and everything else will be related to sinon
– Theepan Thevathasasn
Nov 14 '18 at 6:19
Please, can you provide an example you like to use spices in?
– Alexander Elgin
Nov 14 '18 at 7:40
@AlexanderElgin I have edited the question :-)
– Theepan Thevathasasn
Nov 14 '18 at 10:32