如何在运行testing之前从摩卡文件读取数据?

我目前正在API上运行testing – 使用Mocha运行时没有问题。 testing数组存储在一个variables – “testing”在文件的顶部。 我想从文本文件中读取testing信息,并在运行testing(一次)之前将信息parsing成variables。

我试图使用before()同步和asynchronous(下面)

//Synchronously describe("API Tests", function (done) { before(function(){ tests = fs.readFileSync('./json.txt', 'utf8'); tests = JSON.parse(tests); }); for (var i = 0; i < tests.length; i++) { runTest(tests[i]); } done(); }); 

 //Asynchronously describe("API Tests", function () { var tests = ""; before(function(){ fs.readFile('./json.txt', 'utf8', function(err, fileContents) { if (err) throw err; tests = JSON.parse(fileContents); }); }); for (var i = 0; i < tests.length; i++) { runTest(tests[i]); }}); 

节点返回一个错误,说明该文件不存在(它是)。

此外,我试图运行文件读(同步和asynchronous),执行封装callback描述(如下所示)。 似乎无法查出案件,返回“没有发现任何testing”。

 var tests; fs.readFile('./json.txt', 'utf8', function(err, fileContents) { if (err) throw err; tests = JSON.parse(fileContents); describe("API Tests", function () { for (var i = 0; i < tests.length; i++) { runTest(tests[i]); } }); }); 

如何在运行Mocha之前读取包含testing用例的文件? 我在Webstorm中使用Mocha。

asynchronous版本是错误的,你需要传递一个donecallback,否则钩子将同步运行。 就像是

 before(function(done){ fs.readFile('./json.txt', 'utf8', function(err, fileContents) { if (err) throw err; tests = JSON.parse(fileContents); done(); }); });