【发布时间】:2018-02-25 12:16:22
【问题描述】:
我在 node.js 中有一个函数:
fs.readdir(filesPath, function(err, items) {
items.forEach(function(filename){
fs.readFile(path.join(filesPath,filename), {encoding: 'utf-8'}, function (err,data){
//do some calculations with data from each file, the result is an object 'finalOutput'
fs.stat(path.join(__dirname,'output.csv'), function (err, stat) {
//check if file exists
fs.appendFile(path.join(__dirname,'output.csv'), csv, function (err) {
// append output to csv
});
}
else {
//create file
fs.writeFile(path.join(__dirname,'output.csv'), fields, function (err, stat) {
if (err) console.log(err);
console.log('File saved');
});
}
});
})
})
});
我想对 'finalOutput' 变量、文件内容(例如,如果文件为空,则输出应为 X 等)以及输出文件的存在编写测试。但是当我运行 npm test 时(我正在使用 mocha 和 chai,我得到' TypeError: Cannot read property 'to' of undefined at Context')
这是我要运行的测试示例:
var chai = require('chai'),
expect = chai.expect,
mypreviouscode = require('./mypreviouscode');
describe('test1', function() {
it('There output should always exist', function() {
expect(finalOutput.to.exist);
});
});
【问题讨论】:
标签: javascript node.js mocha.js chai