Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Spying on Function Calls | Advanced Techniques and TDD Workflow
Testing JavaScript Code

bookSpying on Function Calls

test.js

test.js

copy

When you want to verify that a function was called, with which arguments, or how many times, you can use a spy. Spies are especially useful when you need to monitor the behavior of existing functions without replacing their implementation. In the example above, jest.spyOn is used to track calls to the add method of the math object. This allows you to assert whether the function was called and with what arguments, while still preserving the original logic.

Spies are different from mocks in a key way: a spy wraps around an existing function and observes its usage, but by default, it does not change the function's behavior. In contrast, a mock replaces the original implementation with a custom one, often returning fixed values or tracking calls without running real code. Use a spy when you want to keep the original logic intact but need to verify how it is used. Use a mock when you want to isolate the code under test and control its dependencies' behavior.

1. What is the main difference between a mock and a spy in Jest?

2. Which Jest method allows you to monitor calls to an existing function?

question mark

What is the main difference between a mock and a spy in Jest?

Select the correct answer

question mark

Which Jest method allows you to monitor calls to an existing function?

Select the correct answer

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

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

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

Секція 3. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you give an example of how to use a spy in Jest?

What are some common use cases for spies versus mocks?

How do I decide when to use a spy instead of a mock?

Awesome!

Completion rate improved to 7.14

bookSpying on Function Calls

Свайпніть щоб показати меню

test.js

test.js

copy

When you want to verify that a function was called, with which arguments, or how many times, you can use a spy. Spies are especially useful when you need to monitor the behavior of existing functions without replacing their implementation. In the example above, jest.spyOn is used to track calls to the add method of the math object. This allows you to assert whether the function was called and with what arguments, while still preserving the original logic.

Spies are different from mocks in a key way: a spy wraps around an existing function and observes its usage, but by default, it does not change the function's behavior. In contrast, a mock replaces the original implementation with a custom one, often returning fixed values or tracking calls without running real code. Use a spy when you want to keep the original logic intact but need to verify how it is used. Use a mock when you want to isolate the code under test and control its dependencies' behavior.

1. What is the main difference between a mock and a spy in Jest?

2. Which Jest method allows you to monitor calls to an existing function?

question mark

What is the main difference between a mock and a spy in Jest?

Select the correct answer

question mark

Which Jest method allows you to monitor calls to an existing function?

Select the correct answer

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

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

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

Секція 3. Розділ 3
some-alt