0

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?

Phil Dimeski
  • 41
  • 1
  • 7
  • 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: https://stackoverflow.com/questions/34571/how-do-i-test-a-private-function-or-a-class-that-has-private-methods-fields-or – philipisapain Feb 13 '18 at 16:31

1 Answers1

1

@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"
        }]
    }]
    
btmills
  • 4,431
  • 24
  • 22