Skip to content

Instantly share code, notes, and snippets.

@fvovan
Created March 30, 2018 13:52
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 fvovan/95a0f4206554333d483455098e1c97d6 to your computer and use it in GitHub Desktop.
Save fvovan/95a0f4206554333d483455098e1c97d6 to your computer and use it in GitHub Desktop.
rewiremock mocking
const getDoubledFoo = require("./nest1")
const { getFoo, getBar } = require("./nest2")
function getData() {
return {
b: getBar(),
d: getDoubledFoo(),
f: getFoo(),
}
}
module.exports = getData
const {getFoo} = require("./nest2")
function getDoubledFoo() {
return getFoo().repeat(2)
}
module.exports = getDoubledFoo
function getFoo() {
return "Foo"
}
function getBar() {
return "Bar"
}
module.exports = {
getFoo: getFoo,
getBar: getBar,
}
const assert = require('chai').assert;
const rewiremock = require('rewiremock/node');
describe('Data', function () {
it('original modules', function () {
const getData = require("../../src/core/direct");
assert.deepEqual(getData(), {
b: "Bar",
d: "FooFoo",
f: "Foo",
})
});
context("mocked with proxy", function () {
it('mocked modules 1 ', function () {
const getData = rewiremock.proxy('../../src/core/direct', (r) => ({
'../../src/core/nest1': () => "Baaar",
}));
assert.deepEqual(getData(), {
b: "Bar",
d: "Baaar",
f: "Foo",
})
});
// by default it affects all nested calls ie nest2 deps from nest1
it('mocked modules 2', function () {
const getData = rewiremock.proxy('../../src/core/direct', (r) => ({
'../../src/core/nest2': {
getFoo: () => "a1",
getBar: () => "b2",
},
}));
assert.deepEqual(getData(), {
b: "b2",
d: "a1a1",
f: "a1",
})
});
// no original methods
it('mocked modules all', function () {
const getData = rewiremock.proxy('../../src/core/direct', (r) => ({
'../../src/core/nest1': () => "Baaar",
'../../src/core/nest2': {
getFoo: () => "a1",
getBar: () => "b2",
},
}));
assert.deepEqual(getData(), {
b: "b2",
d: "Baaar",
f: "a1",
})
});
it('mocked modules first child', function () {
const getData = rewiremock.proxy('../../src/core/direct', (r) => ({
'../../src/core/nest2': r.with({
getFoo: () => "a1",
getBar: () => "b2",
}).directChildOnly()
}));
assert.deepEqual(getData(), {
b: "b2",
d: "FooFoo",
f: "a1",
})
});
it('mocked parts of modules first child', function () {
const getData = rewiremock.proxy('../../src/core/direct', (r) => ({
'../../src/core/nest2': r.with({
getFoo: () => "a1",
}).callThrough().directChildOnly()
}));
assert.deepEqual(getData(), {
b: "Bar",
d: "FooFoo",
f: "a1",
})
});
})
context("mocked directly", function () {
it('mocked module 1 ', function () {
rewiremock.default('../../src/core/nest1').with(() => "Baaar")
rewiremock.enable()
const getData = require('../../src/core/direct')
assert.deepEqual(getData(), {
b: "Bar",
d: "Baaar",
f: "Foo",
})
rewiremock.disable()
})
it('mocked module 2', function () {
rewiremock.default('../../src/core/nest2').with({
getFoo: () => "a1",
getBar: () => "b2",
})
rewiremock.enable()
const getData = require('../../src/core/direct')
assert.deepEqual(getData(), {
b: "b2",
d: "Baaar", // the mock doesn't affect nested deps
f: "a1",
})
rewiremock.disable()
})
it('mock all', function () {
rewiremock.default('../../src/core/nest2').with({
getFoo: () => "a1",
getBar: () => "b2",
})
rewiremock.default('../../src/core/nest1').with(() => "Baaar")
rewiremock.enable()
const getData = require('../../src/core/direct')
assert.deepEqual(getData(), {
b: "b2",
d: "Baaar",
f: "a1",
})
rewiremock.disable()
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment