Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Writing Your First Test | Testing Fundamentals and Jest Basics
Testing JavaScript Code

bookWriting Your First Test

test.js

test.js

copy

To begin testing with Jest, you write a test using the test function. This function takes two arguments: a description string and a callback function containing the test logic. In the example above, the description string is 'adds 2 + 2 to equal 4'. This description should clearly state what you expect the test to do, making it easy to understand the purpose of the test when reading results or debugging.

Inside the callback function, you use the expect function. This function is essential for making assertions about your code. In this case, expect(2 + 2) creates an expectation for the value of 2 + 2. To check if this value matches what you expect, you use a matcher. Here, the matcher is toBe(4), which checks if the result of 2 + 2 is strictly equal to 4.

The combination of expect and toBe forms the core of many Jest tests. The expect function evaluates an expression, and the matcher, such as toBe, checks if the result meets your expectations. If the expectation passes, the test succeeds; if not, the test fails and Jest reports the error, helping you quickly identify issues in your code.

1. In a Jest test, what does the expect function do?

2. What is the purpose of the toBe matcher in Jest?

question mark

In a Jest test, what does the expect function do?

Select the correct answer

question mark

What is the purpose of the toBe matcher in Jest?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 7.14

bookWriting Your First Test

Deslize para mostrar o menu

test.js

test.js

copy

To begin testing with Jest, you write a test using the test function. This function takes two arguments: a description string and a callback function containing the test logic. In the example above, the description string is 'adds 2 + 2 to equal 4'. This description should clearly state what you expect the test to do, making it easy to understand the purpose of the test when reading results or debugging.

Inside the callback function, you use the expect function. This function is essential for making assertions about your code. In this case, expect(2 + 2) creates an expectation for the value of 2 + 2. To check if this value matches what you expect, you use a matcher. Here, the matcher is toBe(4), which checks if the result of 2 + 2 is strictly equal to 4.

The combination of expect and toBe forms the core of many Jest tests. The expect function evaluates an expression, and the matcher, such as toBe, checks if the result meets your expectations. If the expectation passes, the test succeeds; if not, the test fails and Jest reports the error, helping you quickly identify issues in your code.

1. In a Jest test, what does the expect function do?

2. What is the purpose of the toBe matcher in Jest?

question mark

In a Jest test, what does the expect function do?

Select the correct answer

question mark

What is the purpose of the toBe matcher in Jest?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 4
some-alt