Reputation: 13510
I have a function like the following:
const config = require('../config')
getLabel (objectList) {
if (config.labeling.currentVersion === 'v1') {
// Do something on objectList
return true
}
if (config.labeling.currentVersion === 'v2') {
// Do something on objectList
return false
}
}
and want to write test for it. I am new to node.js and not sure how to write a test such that it covers both if statements in the above example since the function requires some parameter from a config file which is fixed. Is there a way to pass in that config? I have absolutely no idea how to solve this so any help or leads on this is much appreciated. I have to mention that right now the currentVersion
is set to v1
in my config files and what I have as for the test looks like the following and codecov suggests that the second if statement is not covered with this test (daah):
const labeler = require('../../src/lib/labeler')
describe('Labeling Unit Testing', () => {
it('should get the label', () => {
let label1 = labeler.getLabel([1, 2, 3])
let label2 = labeler.getLabel([1, 2, 3])
return assert.equal(label1, label2)
})
Upvotes: 0
Views: 735
Reputation: 58
There are a few different ways to accomplish this. One of the easier ones is to remove that direct dependency on the config and allow you to swap out what the version is going to be. You can do this by slightly changing the signature to take another parameter with a default that uses the config:
getLabel(objectList, currentVersion = config.labeling.currentVersion) {
if (currentVersion === 'v1') {
// Do something on objectList
return true
}
if (currentVersion === 'v2') {
// Do something on objectList
return false
}
}
This provides the flexibility you are looking for, while not forcing others to be concerned with specifying a version.
Upvotes: 1