你怎么能正确地要求在mocha.opts模块?

我正在用mocha- mongoose在testing之间自动清除mongo。 在文档中说要在您的spec文件中需要模块或者在您的spec帮助程序中全局使用。

根据规范做它很好,但我想从mocha.opts做到这一点,以保持我的代码干爽。

要求与mocha.opts不起作用。 Mongo在规格之间没有被清除

mocha.opts:

--require ./test/common.js --reporter spec --ui bdd --recursive --colors --timeout 60000 --slow 300 

common.js:

 require('mocha-mongoose')('mongodb://your-mongodb-url-here'); 

要求在每个spec文件中工作

test.js

 var should = require('chai').should() , require('mocha-mongoose')('mongodb://your-mongodb-url-here'); describe("Example test", function() { it(' Mongo will be automatically clear all collections',); }); 

我怎样才能在mocha.opts中正确地要求mocha-mongoose mongoose,所以我不必在每次testing中重复它呢?

它的工作方式是检查beforeEach并注册为beforeEach钩子。 这里是相关的资料来源 :

 if (!options.noClear && !beforeEachRegistered) { if ('function' == typeof beforeEach && beforeEach.length > 0) { // we're in a test suite that hopefully supports async operations beforeEach(clearDB); beforeEachRegistered = true; } } 

问题是, beforeEach不可用于由--require加载的模块。 所以你不能这样做 – --require

我可以想象mocha-mongoose作者的“你的spec助手”的意思是一个模块,其中包含一系列实用函数,每个规范都要求:而不是添加require('mocha-mongoose')(...); 对于每个规范,您只需将其添加到每个规范所需的模块中。