在节点js中的jasmine测试期间,库正在抛出未被捕获的错误

时间:2017-10-04 07:48:53

标签: javascript node.js testing

我有以下功能:

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;

通过这个测试:

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没有得到它。有人知道如何解决它或我的错误是什么?

1 个答案:

答案 0 :(得分:0)

抛出ENOENT时,表示缺少文件或目录。 您可能缺少“退出”包,因此您应该安装它 -

https://www.npmjs.com/package/exit

运行npm install exit 它应该解决这个问题。