简体   繁体   中英

Testing internal functions in Mocha ESlint error

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?

@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" }] }] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM