Skip to main content
Version: 8.0.2

Assertion

What is Assertion?

An assertion(full api here) is defined as a claim or assumption. In programming, there are cases when we, as developers, know that a certain variable takes a specific value(s) at a particular line of code. We usually represent such a claim using comments in our code.

How to explain it more simply?

Say that our application accepts a phone number by the user. Our rule is that any phone number provided by the user must start with international code prefixed with '+' sign, e.g. +961...

What if the user does not adhere to that rule, what if he/she passes '-' sign instead of '+' sign?

This is where Assertion comes into play, we simply want to say if the phone number does not start with a '+' sign followed by 3 numbers of international code, then fail the Test.

Assertion diagram

Assertion

Basic Usage

Let us see an example of asserting that an input value should equal ‘hello world’

const inputValue = `hello world`;

await I.expect(inputValue).toEqual(`hello world`);

The example above is really a simple form of assertion, and it should pass, because the actual value equals the expected value in the toEqual Assertion.