Mocha tests with extra options or parameters

后端 未结 10 1953
一向
一向 2020-12-04 16:17

I am writing test cases for my Node.js application using Mocha. The test cases need an API key as an extra input option or parameter. The API key is private, so I don\'t wan

10条回答
  •  爱一瞬间的悲伤
    2020-12-04 17:17

    One of the easiest ways to pass parameters similar to the process.argv[index] method mentioned in this thread is using the npm config variables. This allows you to see the variable name a little more clearly:

    test command:

    npm --somevariable=myvalue run mytest
    

    package.json:

    "scripts": {
    "mytest": "mocha ./test.js" }
    

    test.js

    console.log(process.env.npm_config_somevariable) // should evaluate to "myvalue"
    

提交回复
热议问题