Javascript 当我在Mocha框架中调用helper函数时,它在test.js中调用时返回Undefined
我有我的helper.js,我在其中定义了读取目录中文件名的函数:Javascript 当我在Mocha框架中调用helper函数时,它在test.js中调用时返回Undefined,javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,我有我的helper.js,我在其中定义了读取目录中文件名的函数: module.exports.getfilenames= function(dirPath) { console.log(dirPath); let files= fs.readdir(dirPath, function (err, files) { if (err) console.log(err); else { console.log("\nCu
module.exports.getfilenames= function(dirPath)
{
console.log(dirPath);
let files= fs.readdir(dirPath, function (err, files) {
if (err)
console.log(err);
else {
console.log("\nCurrent directory filenames:");
files.forEach(file => {
console.log(file);
})
return files;
}
})
};
In test.js
I am calling helper function as:
describe('FILES', function()
{
files=helper.getfilenames(dirPath);//dirPath is a value of path of directory
it('GET FILES', function(done) {
console.log("reading files:"+ files);
done();
})
})
输出:
reading files: Undefined
请建议如何解析test.js中的文件对象?
fs.readdir
是一个异步函数,您需要使用同步版本:
否则,您可以使用异步版本,但需要进行一些代码更改。对readdir方法的同步调用解决了此问题。谢谢
let files = fs.readdirSync(dirPath);
files.forEach(file => {
console.log(file);
})
return files;