Jestjs Jest检查错误实例

Jestjs Jest检查错误实例,jestjs,Jestjs,我刚开始开玩笑,有一个愚蠢的检查我无法处理,我想检查是否有错误,我试了下一句,但不起作用,你知道我做错了什么吗 //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 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');
    });
});

非常感谢

以下是此测试的工作案例:


//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,看两件事:1。如果你在测试承诺,你必须回报承诺;二,。从节点回调中得到的
错误与
错误不同。非常感谢您回答jonrsharpe,我将更改代码并再次尝试测试