0
votes

I'm currently developing an Nodejs application and carrying out some unit tests (I'm using Mocha, Chai and Sinon).

I ran into a little ESlint error when I exported and tested an internal function.

function _buildPayload(){
    //....
}

module.exports = { _buildPayload };

Then in my test script

const {_buildPayload} = requires('./someModule')

describe('Test',function(){
          it('Should work',function(){
              let expected = _buildPayload();
             })
        })

When I write the let expected = _buildPayload(); ESlint returns the following error:

error  Shouldn't be accessing private attribute '_buildPayLoad'

My question is should I change the name of my function to not represent and internal even though it is?

1
If you're exporting it for the sole purpose of testing it, you might consider not testing it directly and simply testing the functions that use it (ones that are exported). If it's not being exercised through those other exported methods, then that might hint at it being redundant. There's plenty of discussion about this here: stackoverflow.com/questions/34571/… - philipisapain

1 Answers

1
votes

@philipisapain makes a good point that testing internal methods may not be necessary. If you do need to do it, you have a couple options:

  1. Disable the rule by placing /* eslint-disable rule-name */ at the top of any test scripts that call private methods.
  2. Disable the rule in all test scripts using a glob config in your .eslintrc, provided you're using at least ESLint v4.1.0:

    "overrides": [{
        "files": ["*.test.js"],
        "rules": [{
            "rule-name": "off"
        }]
    }]