Skip to content

Instantly share code, notes, and snippets.

@todgru
Last active February 8, 2018 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todgru/e439373af2488eefc30ae8bd1fe3864a to your computer and use it in GitHub Desktop.
Save todgru/e439373af2488eefc30ae8bd1fe3864a to your computer and use it in GitHub Desktop.
stubbing require's using sinon
// foo-module.js
module.exports.bar = () => {
return "hello bar";
};
// my-file.test
const foo = require("./foo-module");
module.exports.myBar = () => {
return foo.bar();
};
// test.js
const sinon = require("sinon");
const test = require("ava");
const fooModule = require("./foo-module");
const myFile = require("./my-file");
// stubbing module method to return a new value
sinon.stub(fooModule, "bar").returns("goodbye bar");
test("foo-module.bar() should return goodbye bar", t => {
t.is(fooModule.bar(), "goodbye bar");
});
test("my-file.myBar() should return goodbye bar", t => {
t.is(myFile.myBar(), "goodbye bar");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment