node.js require() cache - possible to invalidate?

后端 未结 17 1834
我寻月下人不归
我寻月下人不归 2020-11-22 06:52

From the node.js documentation:

Modules are cached after the first time they are loaded. This means (among other things) that every call to require(\'

17条回答
  •  Happy的楠姐
    2020-11-22 07:44

    rewire is great for this use case, you get a new instance with each call. Easy dependency injection for node.js unit testing.

    rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may

    inject mocks for other modules or globals like process leak private variables override variables within the module. rewire does not load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).

    Good news to all caffeine-addicts: rewire works also with Coffee-Script. Note that in this case CoffeeScript needs to be listed in your devDependencies.

提交回复
热议问题