开玩笑检查错误实例

时间:2018-07-09 21:31:59

标签: jestjs

我刚从Jest开始,有一个愚蠢的检查,我无法处理,我想检查是否有错误,我尝试了下一个句子,但它不起作用,你知道吗我做错了吗?

//This is the function I want to check
const ReadDir = (path) =>
    new Promise((resolve, reject) => {
        fs.readdir(path, (err, files) => err ? reject(err) : resolve(files));
    });

//This is the test
it('should return an error', (done) => {
    ReadRid('this is not a dir').catch((err) => {
        expect(err).toBeInstanceOf('Error');
    });
});

非常感谢

1 个答案:

答案 0 :(得分:0)

这是该测试的一个工作案例:


//This is the test
describe('testing ReadDir', function () {

  //This is the function I want to check
  const readDir = (path) =>
    new Promise((resolve, reject) => {
      fs.readdir(path, (err, files) => err ? reject(err) : resolve(files))
    })

  it('should return an error test 2', async () => {
    await expect(readDir('this is not a dir'))
      .rejects
      .toThrowError(new Error('ENOENT: no such file or directory, scandir \'C:\\dev\\mph-backend\\this is not a dir\''))
  })
})

使用了最新的笑话24,请参阅async example for more details.