Created
November 12, 2015 12:43
-
-
Save jniemin/50db3d22f422f20966f8 to your computer and use it in GitHub Desktop.
Small example how to stub pure functions
This file contains hidden or 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
"use strict"; | |
import {module2_pureFunction} from './module2'; | |
export function module1_pureFunction(){ | |
return module2_pureFunction(); | |
} |
This file contains hidden or 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
"use strict"; | |
export function module2_pureFunction(){ | |
return "I'm return value from module 2 pure function"; | |
} |
This file contains hidden or 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
let sinon = require("sinon"); | |
let chai = require("chai"); | |
let assert = chai.assert; | |
import {module1_pureFunction} from '../lib/module1'; | |
import * as module2 from '../lib/module2'; | |
"use strict"; | |
describe("Test pure function importing", function () { | |
let sinonSandbox; | |
beforeEach(function () { | |
sinonSandbox = sinon.sandbox.create(); | |
}); | |
afterEach(function () { | |
sinonSandbox.restore(); | |
}); | |
it("should use stubs", function(){ | |
let result = module1_pureFunction(); | |
assert.equal(result, "I'm return value from module 2 pure function"); | |
const stubText = "I'm return value from stubbed function"; | |
let stub = sinonSandbox.stub(module2, 'module2_pureFunction'); | |
stub.returns(stubText); | |
result = module1_pureFunction(); | |
assert.equal(result, stubText); | |
}); | |
it("should be original value", function(){ | |
let result = module1_pureFunction(); | |
assert.equal(result, "I'm return value from module 2 pure function"); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems to work at least with node 4.2.1 and babel 6.1.4 (babel-plugin-transform-es2015-modules-commonjs plugin 6.1.4) and mocha 2.2.5