Mocking Techniques in Vitest

1h 30m 36s
English
Paid

Course description

Let's admit it, mocking can be really confusing. First, you need to understand what exactly to mock. Then, find the right way so it doesn't degrade the quality of your testing. If there are too many mocks, tests lose their meaning. If there are too few, they become fragile. In any case, you spend a lot of effort, and the feeling of usefulness doesn't always come. And this is where Artyom comes to the rescue. For almost a decade, he has been dispelling developers' misconceptions about mocking and is ready to make you a professional in this field.
Read more about the course

What you will learn:

In this practical workshop, you will learn:

  • The purpose of mocking;
  • What and when to mock;
  • How to write mocks that are beneficial rather than a maintenance burden;
  • How to separate mocks from implementation details;
  • How to ensure type safety in mocks;
  • And all the mocking techniques you need to test your code!

By solving real-world tasks using the Vitest framework, you will gain a complete understanding of mocking and finally make it a useful tool in your testing arsenal.

Workshop Structure: The workshop consists of 6 blocks of exercises dedicated to different mocking techniques for various data types.

Boundaries

Understand what mocking really is and how it helps create test boundaries. Learn how each mock affects the code being tested with clear examples.

Mocking Functions

Functions are at the core of JavaScript logic. Learn to mock functions for effective testing of input and output data. Create mocks for functions, track their calls, and configure behavior in the code being tested.

Mocking Date and Time

Eliminate side effects of changing date and time in tests. Learn how to “freeze” time and mock time-dependent code to ensure stable test results.

Mocking Global Variables

Explore how to mock implicit dependencies without disruption. Learn to mock global values, methods, and environment variables, creating the perfect testing environment.

Mocking Network Requests

Gain control over network requests and reliably test client-side code. Intercept requests and mock responses using Mock Service Worker, simulating various network scenarios.

Mocking Modules

Understand when to use module mocking and explore alternative techniques. Learn to safely mock modules using Vitest's built-in APIs.

Who this workshop is for: This workshop is intended for all developers, both newcomers to automated testing and those who have been writing tests for years. If you are unsure when to introduce mocks into your test suite or how to do it without having to redo it the next day, this workshop is designed especially for you.

Watch Online

This is a demo lesson (10:00 remaining)

You can watch up to 10 minutes for free. Subscribe to unlock all 48 lessons in this course and access 10,000+ hours of premium content across all courses.

View Pricing

Watch Online Mocking Techniques in Vitest

0:00
/
#1: Intro to Mocking Techniques in Vitest

All Course Lessons (48)

#Lesson TitleDurationAccess
1
Intro to Mocking Techniques in Vitest Demo
02:48
2
Intro to Boundaries
00:43
3
Test boundaries _Problem
01:40
4
Test boundaries _solution
02:27
5
Recap of Boundaries
00:18
6
Intro to Functions
00:42
7
Mock functions _Problem
01:57
8
Mock functions _solution
02:52
9
Spies _Problem
00:54
10
Spies _solution
02:22
11
Mock implementation _Problem
01:32
12
Mock implementation _solution
03:25
13
Recap of Functions
00:22
14
Intro to Date and time _solution
00:59
15
Date and time _Problem
01:04
16
Date and time _solution
02:15
17
Timers _Problem
02:01
18
Timers _solution
02:47
19
Ticks and tasks _Problem
02:16
20
Ticks and tasks _solution
02:15
21
Recap of Date and time
00:19
22
Intro to Globals
01:16
23
Global methods _Problem
00:38
24
Global methods _solution
02:20
25
Global values _Problem
01:25
26
Global values _solution
02:28
27
Environment variables _Problem
01:22
28
Environment variables _solution
03:49
29
Recap of Globals
00:25
30
Intro to Network
02:17
31
Set up MSW _Problem
05:02
32
Set up MSW _solution
01:28
33
Mock responses _Problem
01:05
34
Mock responses _solution
02:25
35
Error responses _Problem
01:40
36
Error responses _solution
02:59
37
Network errors _Problem
00:59
38
Network errors _solution
01:49
39
Response delay _Problem
00:59
40
Response delay _solution
02:30
41
Recap of Network
00:41
42
Intro to Modules
00:57
43
Dependency injection _Problem
03:28
44
Dependency injection _solution
03:45
45
Private side effects _Problem
02:11
46
Private side effects _solution
05:28
47
Recap of Modules
00:31
48
Mocking Techniques in Vitest Outro
00:41

Unlock unlimited learning

Get instant access to all 47 lessons in this course, plus thousands of other premium courses. One subscription, unlimited knowledge.

Learn more about subscription

Comments

0 comments

Want to join the conversation?

Sign in to comment

Similar courses

Web Animations Full Course | Build a GTA VI Website & Master GSAP

Web Animations Full Course | Build a GTA VI Website & Master GSAP

Sources: jsmastery.pro, Adrian Hajdin
More than 70% of leading design-oriented companies use GSAP in production. Now you will not only be able to understand their approach, but you will also...
3 hours 31 minutes 47 seconds
Clean Code: Writing Code for Humans

Clean Code: Writing Code for Humans

Sources: pluralsight
Anyone can write code a computer can understand, but professional developers write code *humans* can understand. Clean code is a reader-focused development styl
3 hours 10 minutes 36 seconds
Building Full-Stack Apps with AI

Building Full-Stack Apps with AI

Sources: Mckay Wrigley (takeoff)
The course "Building Full-Stack Apps with AI" teaches how to create comprehensive applications using artificial intelligence, suitable for both beginners and...
8 hours 3 minutes 16 seconds
Generating Fake Data with Faker.js

Generating Fake Data with Faker.js

Sources: vueschool.io
Every user application depends on data, whether it's user profiles, product listings, or blog posts. However, creating realistic datasets...
51 minutes 56 seconds