我有以下功能:
var fs = require('fs');
function Writer(path, content) {
this.path = path;
this.content = content;
this.process = function () {
fs.writeFile(
this.path,
this.content,
function (error) {
if (error) {
throw new Error('Something went wrong during writing');
}
}, 'utf-8')
};
}
module.exports = Writer;使用this测试:
it('test if writing fails ', function () {
var writer = new Writer(
'/data/configurations/ab/ba/abba/test.json',
'content'
);
expect(function() {
writer.process()
}).toThrowError("Something went wrong during writing");
})因此,这预计会失败,因为没有要写入的文件。则库FS抛出一个未被捕获的错误。
以下是测试的输出:
Started
.....F
Failures:
1) writer test test if writing fails
Message:
Expected function to throw an Error.
Stack:
Error: Expected function to throw an Error.
at Object.<anonymous> (/vagrant/project/tests/utils/file/writerTest.js:42:6)
6 specs, 1 failure
Finished in 0.198 seconds
/vagrant/project/node_modules/mock-fs/lib/binding.js:1060
throw new FSError('ENOENT', filepath);
Error: ENOENT, no such file or directory '/vagrant/project/node_modules/jasmine/node_modules/exit'
at Binding.<anonymous> (/vagrant/project/node_modules/mock-fs/lib/binding.js:1060:13)所以这个库抛出了错误,而toThrowError没有得到它。有没有人知道如何解决这个问题,或者我的错误是什么?
发布于 2017-10-04 15:57:08
当抛出ENOENT时,意味着文件或目录丢失。你可能缺少'exit‘包,所以你应该安装它-
https://www.npmjs.com/package/exit
运行npm install exit,它应该可以解决这个问题。
https://stackoverflow.com/questions/46559635
复制相似问题