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

时间:2018-05-01 12:10:28

标签: javascript unit-testing mocha

我在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.

1 个答案:

答案 0 :(得分:2)

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