Last active
February 8, 2018 22:40
-
-
Save todgru/e439373af2488eefc30ae8bd1fe3864a to your computer and use it in GitHub Desktop.
stubbing require's using sinon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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