I'm trying to stub a constructor in node but I really can't.
I found this , that is quite similiar to what I need to do but I have an error that I could not solve.
//file.js
var foo = require('foo-client')
function toTest () {
var bar = foo()
returns config = bar.foo2(a,b) // returns a Promise
}
what I am trying to do in the test file is
//file-sepc.js
var stub = sinon.stub()
stub.returns(Promise.resolve('config'))// config it's just an example
var file = proxyquire('./file.js', {
'foo-client':{foo2: stub}
})
file.toTest()
.then((result) => {
console.log(result)
done()
})
supposing the node syntax is correct, I am getting this output:
TypeError: foo is not a function
Can anyone help me telling me where is my error or another way to mock/stub this things?
Thanks a lot!
Haven't tried running your code but it looks like foo-client should be a function rather than an object in order for the var bar = foo() not to throw an error you are seeing. Try the following:
var file = proxyquire('./file.js', {'foo-client': sinon.stub.returns({ foo2: stub }) })
Related
I was looking up some database connection Google searches when I saw something that exported an instance of a module as such
const foo = () => {
// Do stuff
};
...
module.exports = foo();
I don't know what this is called but how does nodejs treat exporting a function invocation vs an object or the function itself (without calling it)?
Thank you
The foo function only gets called once no matter how many times you require the module.
This is very simplified explanation of what is happening behind the scenes in Node.js
// cache for modules
var modules = {};
// very simplified require function
function require(name) {
// check cache
if (modules[name])
// so if it has already been required it returns the cached result
return modules[name].module.exports;
// it will resolve path to the required module
// and loads the file content
// not showing here
var obj = { module: { exports: {}}};
// node will wrap the code in a function similar to bellow
function module(module, exports){
const foo = () => {
// Do stuff
};
...
module.exports = foo();
};
module(obj.module, obj.module.exports);
// and now cache it
modules[name] = obj;
return obj.module.exports;
}
I'm facing a problem I'm not able to resolve on my own, maybe some of you faced the same problem.
Let me show you what I'm trying to do, here is the mock:
let mockConfig = {name: 'dude'};
jest.mock('../../../configManager', () => mockConfig);
configManager is a dependency of the function I'm trying to test.
It works well but I want to change the returning object of configManager in another test so the tested function behaves differently.
Let me show you, here is the function I'm testing:
const config = require('../../../configManager');
module.exports = () => {
if (config.name === 'dude') {
do stuff;
}
if (config.name === 'dudette') {
do something else;
}
So, typically, I want to change the config.name to 'dudette' to be able to test the second part of my function.
Naturally, when I want to do this with an imported function, I just do:
let mockJsonQueryResult = { value: 'stuff' };
jest.mock('json-query', () => jest.fn(() => mockJsonQueryResult));
and then in the test, I directly set another value to mockJsonQueryResult:
mockJsonQueryResult = { value: 'hotterStuff' };
But I don't find any way of doing this with a dependency that returns an object, with a dependency returning a function, no problem.
Is there even any way of doing this?
Thanks in advance!
Edit: this is not the same as how to change jest mock function return value in each test? as #Dor Shinar suggested because his problem is to mock a function, even if it is inside a returning object it is still a function, I just want to change a value inside the returned object.
So, I found a solution I'm not completely satisfied with but it works:
I simply set the original full object and then for my tests, change the value of specific properties by setting them directly before calling the function I want to test.
example:
let mockConfig = { person: { name: 'dude', origin: {country: 'France'} } };
jest.mock('../../../configManager', () => mockConfig);
mockConfig.person = {};
mockConfig.person.name = 'dudette';
You don't need to mock the module at all.
If your module export is just an object with property values then just change the properties as needed.
Here is a simple working example to demonstrate:
configManager.js
module.exports = {
name: 'original'
}
code.js
const config = require('./configManager');
module.exports = () => `name: ${config.name}`;
code.test.js
const config = require('./configManager');
const func = require('./code');
test('func', () => {
expect(func()).toBe('name: original'); // Success!
config.name = 'dude';
expect(func()).toBe('name: dude'); // Success!
config.name = 'dudette';
expect(func()).toBe('name: dudette'); // Success!
})
Details
A module binding can't be directly changed to something else:
const config = require('./configManager');
config = { name: 'mock' }; // <= this doesn't work
...but you can change the properties of an object representing a module binding:
const config = require('./configManager');
config.name = 'mock'; // <= this works!
...and any code using the module will automatically see the changes.
I'm using GeoFire in a Cloud Functions for Firebase project I want to unit test.
In my original code GeoFire is used like this:
GeoFire = request('geofire');
...
var catGeoFire = new GeoFire(catGeofireRef);
return catGeoFire.set(storeId, [lat, lon]).then( () => {
console.log("Added store " + storeId + " to GeoFire" );
return Promise.resolve();
});
I need to stub both the call to the GeoFire constructor and the GeoFire().set() method.
I tried:
const geofireStub = sinon.stub(GeoFire, 'set').resolves();
But I received the error:
TypeError: Cannot stub non-existent own property set
I also tried:
const setStub = sinon.stub().resolves();
const geofireStub = sinon.stub(GeoFire).returns({set: setStub});
And I receive the error:
TypeError: Cannot stub non-existent own property set
Both errors happen at the geofireStub line.
Reading the sinon documentation I understood that I can stub an object's methods. However in this case GeoFire isn't an object; it is a constructor function. So I don't really know how can I stub it without having an associated object.
Thanks!
You need something like this, using rewire:
// target.js
var GeoFire = require('geofire');
var catGeoFire = new GeoFire(catGeofireRef);
return catGeoFire.set(storeId, [lat, lon]).then(() => {
console.log("Added store " + storeId + " to GeoFire" );
return Promise.resolve();
});
// test.js
var GeoFire = require('geofire');
var rewire = require('rewire')
var target = rewire('./target')
describe('target', () => {
it('test case', () => {
// arrange
// configure instance
var geoFireStub = sinon.createStubInstance(GeoFire)
geoFireStub.set.resolves()
// configure constuctor
var GeoFireMock = sinon.stub().returns(geoFireStub)
// 'GeoFire' is a mocked variable here
var revert = rewire('GeoFire', GeoFireMock)
// act (call tested module)
target()
// assert (should is just for example)
should(GeoFireMock).calledWithNew(/* params*/)
should(geoFireStub.set).calledWith(/* params*/)
//cleanup (rewire and stubs, prefer to use sandbox)
revert();
...
})
})
GeoFire is the constructor, but set is an instance method.
You should stub GeoFire.prototype I believe.
sinon.stub(GeoFire.prototype, 'set').resolves();
I have a module:
let xmlParser = require('./myTools').xmlParser;
function extractDataAndWrite(xmldata) {
let doc = xmlParser(xmldata);
...
}
module.exports = {
extractDataAndWrite,
};
now I want test the call of xmlParser in extractDataAndWrite:
var extractDataAndWrite = require('../services/importData.js').extractDataAndWrite;
var mytools = require('./myTools');
var sinon = require('sinon');
describe('Test extractDataAndWrite', function() {
it('call xmlParser', function(done) {
var xmlParserSpy = sinon.spy(mytools, 'xmlParser');
extractDataAndWrite("someXML");
console.log('xmlParserSpy: ' + xmlParserSpy.callCount);
done();
});
});
I expect get xmlParserSpy.callCount == 1 but it is 0!
My spy is not working, what must I change?
The problem is that when you create a spy on a function, you are replacing that function reference with a new function. That means that people that reference the old function will not use the new function. In your case, things are not working, as you wrap the exported function reference in mytools' after your own module has already referenced the old function reference.
The general techniques you need to look into are called dependency injection and link seams. The Sinon docs has a good tutorial on the latter, using proxyquire.
Essentially you would have this:
const proxyquire = require('proxyquire');
const toolsStub = createToolsStub();
const importData = proxyquire('../services/importData.js', {
'./myTools': toolsStub
});
function createToolsStub(){
return { xmlParser : sinon.stub().returns({mydoc:{foo:'bar'}};
}
Then later on in your test you could check the xmlParser for calls
assert(toolsStub.xmlParser.calledWith('arg1', 'arg2');
so, I have this problem - and when I have a problem with JavaScript or node inevitably it is my coding that is the problem ;)
So at the risk of ridicule, this is the problem:
I have a module that has an optional parameter for config
Using the standard pattern, this is what I have:
module.exports = function(opts){
return {
// module instance
};
}
and in the calling code there is this
var foo = require('bar')({option: value})
if there are no options to pass, the code looks like this
var foo = require('bar')({})
which kinda looks ugly
so, I wanted to do this
var foo = require('bar')
which doesn't work, as the exports is a function call
so, to the meat of the issue
a) is there any way of achieving this lofty goal ?
b) is there a better pattern of passing parameters to a module ?
many thanks - and I hope that once the laughter has passed you will be able to send some help my way :)
Instead of removing the function call completely, you could make the options argument options to remove the need for an empty object:
module.exports = function(opts) {
opts = opts || {};
return {
// module instance
};
}
It doesn't completely remove the need for () but is better than ({}).
tldr: stick with require('foo')('bar');
There's no way to pass additional parameters to require. Here's the source code, notice how it only takes a single argument:
Module.prototype.require = function(path) {
assert(util.isString(path), 'path must be a string');
assert(path, 'missing path');
return Module._load(path, this);
};
If you really really really want to avoid ()(), you could try something like this:
b.js
'use strict';
module.exports = {
x: 'default',
configure: function (x) {
this.x = x;
},
doStuff: function () {
return 'x is ' + this.x;
}
};
a.js
'use strict';
var b = require('./b');
// Default config:
console.log(b.doStuff()); // 'x is default'
// Reconfigure:
b.configure(42);
console.log(b.doStuff()); // 'x is 42'
But I think it's uglier... stick with the original idea.