Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Common Matchers | Using a Test Automation Framework
Introduction to QA Automation Testing
course content

Зміст курсу

Introduction to QA Automation Testing

Introduction to QA Automation Testing

1. Introduction to Automation Testing
2. Using a Test Automation Framework
3. Browser Automation with Selenium
4. Intro to Intermediate Automation Testing

bookCommon Matchers

In this chapter we will look at some common matchers that can be used for testing various types of values.

Matching Arrays and Objects

The toEqual matcher compares two items on a deep level. Therefore, it is useful for matching structures like Array or perhaps an Object:

Deep Check

The toContain method checks if the passed Array contains a specific value:

The toContainEqual method is used for complicated arrays to perform deep comparisons:

Matching String Patterns

The toMatch method is also used for matching strings:

This matcher doesn’t compare the two strings exactly, rather, it looks for a substring inside the original input. Therefore the following assertion passes as well:

In actuality, the toMatch method simply accepts a Regular Expression (RegEx) which looks for the given pattern in the original input string.

A substring, or simple text is a valid RegEx expression, hence simply, writing World looks for the term World in the input string, and it matches because "World" is a substring of "Hello World".

A Regular Expression is simply a sequence of characters which is used to represent a specific pattern. Regular Expressions are used for efficiently searching for pieces of text which match a desired pattern. Most programming languages like Python and JavaScript come with built-in support for Regular Expressions, and languages which do not have built-in support have external libraries which can be installed to add support for Regular Expressions.

Regex follows specific syntax, just like how programming languages do. It is not necessary to learn Regular Expressions to progress through this course, however, we can look at an example to see Regular Expressions can be useful in general.

The pattern /^[a-m]{5}$/ checks if the input string is 5 characters long, and only contains lowercase letters from ‘a’ to ‘m’.

Regular Expressions are fairly robust and can be used to look for any kind of pattern. For-example, the pattern /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/ checks if the input string is a valid email.

Checking For `null` and `undefined`

To check if a value is null, we cannot use any of the previous matches since null is not an integer, a structure, or a string. In this case we use the matcher toBeNull which doesn’t accept any argument:

JavaScript also has the keyword undefined. We can check if a value is undefined using the matcher toBeUndefined :

There’s a matcher for checking the opposite as well. The toBeDefined matcher verifies that the target value is defined.

Checking for Boolean Values & Truthiness

To check if a value is True or False, we use the methods toBeTruthy and toBeFasly :

These methods check the “truthiness” or “falseness” of a value. Any value which would be considered True inside an if-conditional is a Thuthy value, otherwise it is a Falsy value.

In this case "turthy" will be printed out, indicating that 10 is a truthy value. On the contrary, replacing 10 with a 0, or an empty string ('') prints "falsy".

Checking for Exceptions

We can also check if a certain exception has been thrown by a function, using the toThrow matcher.

Let’s say we have the classic factorial function which throws an exception when an invalid input is given to the function:

We can use the toThrow matcher to test if the function actually throws an Error / Exception in case of invalid input:

The toThrow method accepts one argument, which is supposed to be the text content of the expected exception. Since it compares the text of the thrown exception, therefore the exact text of the expected exception should be passed in the toThrow method.

Negation

We can also negate matchers by chaining the matcher after a not:

1. What is the purpose of the toEqual matcher in Jest?
2. Which matcher would you use to verify if an array contains a specific object with matching properties?
3. When would you use the toMatch matcher?
4. How do you verify that a function throws an exception with a specific message?
What is the purpose of the toEqual matcher in Jest?

What is the purpose of the toEqual matcher in Jest?

Виберіть правильну відповідь

Which matcher would you use to verify if an array contains a specific object with matching properties?

Which matcher would you use to verify if an array contains a specific object with matching properties?

Виберіть правильну відповідь

When would you use the toMatch matcher?

When would you use the toMatch matcher?

Виберіть правильну відповідь

How do you verify that a function throws an exception with a specific message?

How do you verify that a function throws an exception with a specific message?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 7
We're sorry to hear that something went wrong. What happened?
some-alt