为什么 mocha 不能识别我的错误?

Why won't mocha recognise my error?

我在 Mocha 中有以下测试用例..

.toThrowError("invalid")

但是尽管有下面的代码(if 块中的代码正确执行)

   try {
    let arr = [];
    existing.forEach((exists) => {
        arr.push(...this.dependencies[exists]);
    });
    for (let x in arr){
        if(existing.indexOf(arr[x]) == -1){
            throw new Error('invalid');
        }
    }
  }

  catch(e){
     console.log(e); 
  }

我收到以下消息。我在这里做错了什么?

Expected the function to throw an error matching:
  "Invalid Base Permissions"
But it didn't throw anything.

你自己发现了这个错误,所以就mocha而言,它没有抛出错误(错误在进入测试之前已经处理)。如果您不依赖代码中捕获的错误,请删除 try-catch,它应该(假设错误确实被抛出)工作。