Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Jest, how can I make a test fail?

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?

like image 818
kYuZz Avatar asked Feb 11 '17 13:02

kYuZz


People also ask

How do I make jest fail on console error?

Add jest-fail-on-console npm package, then on your jest.config.js This will fail a test once there is a console error or warning done by jest because of an error or warning thrown in the test item. Thanks for contributing an answer to Stack Overflow!

Can you fail a jest test arbitrarily?

Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library. When testing code with Jest, it can sometimes be useful to fail a test arbitrarily. This post goes through a few scenarios where that might be useful and how to fail a Jest test explicitly/in a forced manner.

How do I get Started with jest?

Now hands on Jest! As with every JavaScript project you'll need an NPM environment (make sure to have Node installed on your system). Create a new folder and initialize the project with: Next up install Jest with: Let's also configure an NPM script for running our tests from the command line.

What is a jest test?

Jest Tutorial: what is Jest? Jest is a JavaScript test runner, that is, a JavaScript library for creating, running, and structuring tests. Jest ships as an NPM package, you can install it in any JavaScript project. Jest is one of the most popular test runner these days, and the default choice for React projects.


2 Answers

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

Sample call:

fail('it should not reach here'); 

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

declare function fail(error?: any): never; 
like image 154
What Would Be Cool Avatar answered Sep 23 '22 17:09

What Would Be Cool


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

test('Obi-Wan Kenobi', () => {   throw new Error('I have failed you, Anakin') }) 
like image 36
Cristóvão Trevisan Avatar answered Sep 22 '22 17:09

Cristóvão Trevisan