Global `before` and `beforeEach` for mocha?

后端 未结 5 1044
长情又很酷
长情又很酷 2021-02-01 00:38

I\'m using mocha for javascript unit-testing now.

I have several test files, each file has a before and beforeEach, but they are exactly the s

5条回答
  •  你的背包
    2021-02-01 00:44

    I've had similar issue when I needed to "mock" global variables used by one of dependencies.

    I used .mocharc.js for that, since code in that JS file is being executed once when "mocha" environment is being setup.

    Example .mocharc.js:

    global.usedVariable = "someDefinedValue";
    
    /** other code to be executed when mocha env setup **/
    
    module.exports = {};
    

    This worked for me, nevertheless this looks quite "dirty" way to do that. Please, comment if you know a better place for that code :)

提交回复
热议问题