how can you mock a value for a variable? I tried mockReturnValue and mock but I think I am doing something wrong with how I am using it.
main.js
const isAvailable = cond1 || cond2 || cond3; // return boolean;
test/main.js
isAvailable.mock('true'); // tried this
isAvailable = jest.mockReturnValue('true'); // trie this as well
jest.fn().mockReturnValue(true)
Related
Saw this style of import on Github and was wondering why it is used? What's the deal with the () in the end?
const logger = require('pino')()
vs
const logger = require('pino');
const logger = require('pino')();
is equivalent to
const loggerFn = require('pino');
const logger = loggerFn();
Does this help?
Let me give a more concrete example. Consider
foo.js
module.exports = function() {
return 1;
};
and now in some other module
const fooFn = require('foo');
// fooFn is our defined function
const foo = fooFn();
// foo is 1
versus
const foo = require('foo')();
// foo is 1
which is equivalent.
So basically with pino package when you require('pino') you get a function which you then have to call. That's how the package was designed.
To answer your question, you just need to understand what require does. When require is called, NodeJS returns the value of module.exports in the module you are requesting. In the case of pino, module.exports is equal to a function, so when you add the parenthesis to the end of the require statement, you are simply calling the function returned by require.
I'm trying to test my Node module using Mocha.
The module is very small here is an example...
import { sharedFunctionA, sharedFunctionB, commonFunction } from <file>
const functionA = token => _data => sharedFunctionA(token);
const functionB = () => data => sharedFunctionB(data);
exports.doThingA = token => {
commonFunction(functionA(token));
};
exports.doThingB = () => {
commonFunction(functionB());
};
This is only a brief example but it shows what I'm trying to do.
I need to test that doThingA and doThingB pass in the correct function to the commonFunction.
I have stubbed the commonFunction and I can see that it is being called but I can't assert that the function passed in is correct.
TBH... I'm beginning to think of restructuring this entirely to pass in some sort of enum to the commonFunction and running the respective function from there.
In this case you can stub on the sharedFunctionA and sharedFunctionB and then retrieve the argument of your stub on the commonFunction and call it. Then check your other stubs are being called with the desired arguments.
I know it's tedious but it is the only way I can think of with your code.
Quick example:
const assert = require('assert')
const sinon = require('sinon')
const sharedFunctions = require('<fileWithSharedFunctions>')
const commonStub = sinon.stub(sharedFunctions, 'commonFunction')
const sharedBStub = sinon.stub(sharedFunctions, 'sharedFunctionB')
const fileToTest = require('<fileToTest>')
fileToTest.doThingB()
commonStub.getCall(0).args[0]()
assert(sharedBStub.calledOnce)
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.
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.
I want to stub process.env.FOO with bar.
var sinon = require('sinon');
var stub = sinon.stub(process.env, 'FOO', 'bar');
I'm confused.
I read document, but still I don't understand yet.sinonjs docs
sinonjs is one example, not sinonjs is okay.
From my understanding of process.env, you can simply treat it like any other variable when setting its properties. Keep in mind, though, that every value in process.env must be a string. So, if you need a particular value in your test:
it('does something interesting', () => {
process.env.NODE_ENV = 'test';
// ...
});
To avoid leaking state into other tests, be sure to reset the variable to its original value or delete it altogether:
afterEach(() => {
delete process.env.NODE_ENV;
});
I was able to get process.env to be stubed properly in my unit tests by cloning it and in a teardown method restoring it.
Example using Mocha
const env = Object.assign({}, process.env);
after(() => {
process.env = env;
});
...
it('my test', ()=> {
process.env.NODE_ENV = 'blah'
})
Keep in mind this will only work if the process.env is only being read in the function you are testing. For example if the code that you are testing reads the variable and uses it in a closure it will not work. You probably invalidate the cached require to test that properly.
For example the following won't have the env stubbed:
const nodeEnv = process.env.NODE_ENV;
const fnToTest = () => {
nodeEnv ...
}
With sinon you can stub any variable like this.
const myObj = {
example: 'oldValue',
};
sinon.stub(myObj, 'example').value('newValue');
myObj.example; // 'newValue'
This example is form sinon documentation. https://sinonjs.org/releases/v6.1.5/stubs/
With that knowledge, you can stub any environment variable.
In your case it would look like this:
let stub = sinon.stub(process.env, 'FOO').value('bar');
You can use this if you want to stub a key which not present in process.env
const sinon = require('sinon')
let sandbox = sinon.createSandbox();
sandbox.stub(process, 'env').value({ 'SOME_KEY': 'SOME_VALUE' });
How to quickly mock process.env during unit testing.
https://glebbahmutov.com/blog/mocking-process-env/
const sinon = require('sinon')
let sandbox = sinon.createSandbox()
beforeEach(() => {
sandbox.stub(process.env, 'USER').value('test-user')
})
it('has expected user', () => {
assert(process.env.USER === 'test-user', 'wrong user')
})
afterEach(() => {
sandbox.restore()
})
But what about properties that might not exist in process.env before the test? You can use the following package and then you will be able to test the not exist env variables.
https://github.com/bahmutov/mocked-env
In a spec-helper.coffee or something similar where you set up your sinon sandbox, keep track of the original process.env and restore it after each test, so you don't leak between tests and don't have to remember to reset every time.
_ = require 'lodash'
sinon = require 'sinon'
beforeEach ->
#originalProcessEnv = _.cloneDeep process.env
afterEach ->
process.env = _.cloneDeep #originalProcessEnv
In your test, use process.env as normal.
it 'does something based on an env var', ->
process.env.FOO = 'bar'
Like many others have pointed out the sinon way works unless you have the environment variables set before the test begins, e.g. in the file you are testing like so:
const foo = process.env.YOUR_VAR;
Gleb Bahmutov wrote an npm package that gives a nice way of setting environment variables in your tests no matter how you are referencing the environment variables.
Link to his page: https://glebbahmutov.com/blog/mocking-process-env/
Link to npm package: https://github.com/bahmutov/mocked-env
It worked great and was super easy to implement.