Skip to content

Instantly share code, notes, and snippets.

@jniemin
Created November 12, 2015 12:43
Show Gist options
  • Save jniemin/50db3d22f422f20966f8 to your computer and use it in GitHub Desktop.
Save jniemin/50db3d22f422f20966f8 to your computer and use it in GitHub Desktop.
Small example how to stub pure functions
"use strict";
import {module2_pureFunction} from './module2';
export function module1_pureFunction(){
return module2_pureFunction();
}
"use strict";
export function module2_pureFunction(){
return "I'm return value from module 2 pure function";
}
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");
})
});
@jniemin
Copy link
Author

jniemin commented Nov 12, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment