Javascript 在 Jest 中,如何使测试失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42176642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:58:09  来源:igfitidea点击:

In Jest, how can I make a test fail?

javascriptjestjs

提问by kYuZz

I know I could throw an error from inside the test, but I wonder if there is something like the global fail()method provided by Jasmine?

我知道我可以从测试内部抛出错误,但我想知道是否有类似fail()Jasmine 提供的全局方法的东西?

采纳答案by Cristóv?o Trevisan

You can do it by throwing an error. For example:

您可以通过抛出错误来实现。例如:

test('Obi-Wan Kenobi', () => {
  throw new Error('I have failed you, Anakin')
})

回答by What Would Be Cool

Jest actually uses Jasmine, so you can use failjust like before.

Jest 实际上使用 Jasmine,因此您可以fail像以前一样使用。

Sample call:

示例调用:

fail('it should not reach here');

Here's the definition from the TypeScript declaration file for Jest:

以下是 Jest 的 TypeScript 声明文件中的定义:

declare function fail(error?: any): never;

回答by Beau Smith

Copy/pasta failing test:

复制/面食失败测试:

it('This test will fail', done => {
  done.fail(new Error('This is the error'))
})

回答by Rikin

Dont think there is, discussed here: https://github.com/facebook/jest/issues/2129

不要认为有,这里讨论:https: //github.com/facebook/jest/issues/2129

回答by Rash

Here are certain scenarios where some of the answers won't work. In a world of async-await, it is quite common to have try-catch logic like so.

以下是某些答案不起作用的某些情况。在 的世界中async-await,像这样的 try-catch 逻辑是很常见的。

try {
  await someOperation();
} catch (error) {
  expect(error.message).toBe('something');
}

Now imagine if someOperation()somehow passed, but you were expecting it to fail, then this test will still pass because it never went to the catch block. So what we want is to make sure that the test fails if someOperation does not throw an error.

现在想象一下,如果someOperation()以某种方式通过了,但您预计它会失败,那么该测试仍然会通过,因为它从未进入 catch 块。所以我们想要的是确保如果 someOperation 没有抛出错误,测试就会失败。

So now let's see which solutions will work and which won't.

现在让我们看看哪些解决方案有效,哪些无效。

Accepted answer won't work here because the throw will be catched again.

接受的答案在这里不起作用,因为投掷会再次被抓住。

try {
  await someOperation();
  throw new Error('I have failed you, Anakin');
} catch (error) {
  console.log('It came here, and so will pass!');
}

The answer with true === false also won't work because, assertions too throw an error like above which will be catched.

true === false 的答案也不起作用,因为断言也会抛出类似上面的错误,该错误将被捕获。

try {
  await someOperation();
  expect(true).toBe(false); // This throws an error which will be catched.
} catch (error) {
  console.log('It came here, and so will pass!');
}

The one solution that DOES WORK (as shown in @WhatWouldBeCool's answer) for this case is below. Now it explicitly fails the test.

对于这种情况,一种有效的解决方案(如@WhatWouldBeCool 的回答所示)如下。现在它明确地未通过测试。

try {
  await someOperation();
  fail('It should not have come here!')
} catch (error) {
  console.log('It never came here!');
}

回答by Torvaldur Rúnarsson

You can always do something like this :)

你总是可以做这样的事情:)

expect(true).toBe(false);