Unit Testing (Vitest) Tutorial #1 - What is Unit Testing?
Key Takeaways
This video tutorial series covers unit testing using Vitest, a testing framework, and provides a step-by-step guide on setting it up in a project and writing effective unit tests for code.
Full Transcript
All right then, gang. In this course, we're going to be diving into the mysterious world of testing, specifically unit testing in JavaScript. Now, testing in general, I think, is one of those topics that a lot of new web developers tend to put off learning for a long, long time because maybe they think it's boring or too complex or it just gets in the way or maybe it's something they'll never really need anyway. I was firmly in that camp when I started out and it took me years to learn how to test my code for all of those reasons. But actually, it's not boring. It doesn't have to be hard. And in the age of AI and aentic coding, I would argue that learning how to test your code properly is more important than ever, especially when you're not the one writing all the code yourself anymore. On top of that, learning how to write tests, in my opinion, will most likely make you a better programmer because it forces you to think about how your codebase and functions are structured, about edge cases, what could go wrong with your code, or how it might be better refactored, and so on. So, regardless of how often you think you might need to test your code in the future, learning how to test is nearly always going to benefit you as a developer. Now, when it comes to testing your applications, there's different types of tests that we can write. We can write end to end tests, which are broad tests that kind of simulate user behavior and interaction with the application, like filling out forms maybe or navigating pages. We can write integration tests, which allow us to test how different parts of the application work together, like maybe how different modules or functions integrate with one another. And then we've also got unit tests which are small and focused tests that allow us to test individual functions or components in isolation. And typically you might have a combination of all three types of test in a well tested application. In this course we're going to be focusing on unit testing because I think it's a good place to start when you're learning how to test and it's going to provide you with a solid foundation for understanding other types of tests in the future. Now, to do this, we'll be using a testing framework, and there's plenty to choose from, like Jest, Jasmine, Mocker, and others. We're going to be using one called Vest, which is a modern testing framework built specifically for testing JavaScript and TypeScript project. It's fast, it's really easy to set up, and it's got good support for modern JavaScript features right out of the box. That said, Vest uses a lot of the same methods and patterns as Jest. So pretty much everything you learn here with Vest will carry over almost one:one if you ever decide to use at a later point. So then we'll be installing Vest into a simple JavaScript project later on. But to begin with, I just want to laser in on exactly what unit tests are and when we should use them. So in simple terms, a unit test is just a small piece of code that tests a specific function or module in your application. And the goal of that unit test is to ensure the function or module that you're testing behaves the way it's expected to under different conditions. For example, you might have a function that applies a discount to products by using a promo code. So you could write a unit test for that function to check it applies the correct discount when a valid promo code is provided and also that it doesn't apply discounts for invalid codes. You might also test edge cases like if there's white space before the promo code or if it's lowerase and not uppercase and so forth. And the key thing to remember about a unit test is that it should be isolated from the rest of the application. So that means it should only test the specific function or module in question and not rely on other parts of the application or any external services. And that isolation then makes it much easier to pinpoint where bugs come from because when a test fails, you know exactly which piece of code is responsible. Also, unit tests aren't really concerned with how a function is implemented. They care about what it does. So in most cases we should focus on testing the inputs and the outputs not the internal logic that gets it there. In that respect you could think of the function being tested as like a black box where you provide inputs and you expect certain outputs and you don't worry too much about what the function does inside the box. Now there will be exceptions to that oversimplification and when you're correcting failing tests you're going to need to look inside that box to see why it's not working as expected. But it's a nice mental model to have when you first start writing unit tests. As for what you should test and what you shouldn't test, well, that is an entire topic in itself with a lot of strong opinions on both sides. Some developers aren't going to be happy unless they're unit testing absolutely everything. And other developers don't like to write unit tests at all. Personally, I like to sit somewhere in the middle and I only make unit tests where I see the value in doing so. for example, for any functions that have complex logic or have a lot of edge cases or functions that are critical to the behavior of the application or even to double up as documentation for how a function is intended to be used. In all these scenarios, I would see good value in creating unit tests. On the other hand, I would usually skip writing tests for trivial functions that are unlikely to change much or for code that's already well covered by other types of tests. Ultimately though, what you choose to test will depend on the specific needs of your application as well as your own development process, workflow, and preferences. But in general, I think it's a good idea to get a balance of just enough tests to ensure code reliability, I guess, without them becoming a productivity roadblock. With all that in mind, then let's set up a brand new project and install Vest so we can start making some tests. So, I've got a folder open inside VS Code called Vest unit testing. And the only thing inside this is a source folder where all the source code we'll actually be testing is going to live. We'll also create a test folder later when we start making test files. The first thing we need to do though is create a new package.json file to manage the project dependencies. And we'll also be registering the test scripts in here later as well. Now to make a package file, we'll be using npm, the node package manager. And for that reason, you're going to have to have Node.js installed on your computer. If you don't have Node installed, you can download it and install it easily from node.js.org. If you don't know whether you have it installed, you can just open a terminal, which you can do by toggling this icon up here in VS Code, and then run the command node, then a space, and then hyphen v, and then we can press enter. If it is installed, then you should get a version number printed back. Now, to use vitest, you're going to need node version 18, I think it is, or above. So if you see a version less than this, you will need to download and install a more recent version from that same node.js.org website. Anyway, once you have Node installed, you can make a new package file in your project by running the command npm init followed by the Y flag. And you'll need to make sure you do this in the root of your project's directory, which you should be in by default when you're using VS Code's integrated terminal. Anyway, this should generate the file for us with all of the default properties and values. So, if we open that file up, we should see a script property, which is where we're going to set up the scripts later on for testing. And when we install V test in a moment, we'll see that dependency listed in this file as well. Now, in a real project, we could add the type property to this file and set it equal to module to tell Node that we're using modern ES module syntax, import, and export statements. But for this course, we don't need to do that because we'll be running our code through VEST's runtime, which already understands modern module syntax right out of the box. So the next thing we need to do then is install VEST. I'm on the Vest getting started guide then, which shows us how to install it into a brand new project. And if we scroll down a little bit, we can see that all we need to do is run this install command right here, npm install with the D flag to install this only as a dev dependency and then vest. And when we say something is a dev dependency, it means we only need that package during development, not for the production code. Anyway, let's copy this command and head back to the project. And actually, I've just noticed down here, you require node version 20 or greater, not 18 as I said earlier. So, I got that wrong. It's 20 or greater. So, if you have a version less than that, definitely go and update it. All right, then. So, in the project, let's open up the terminal again. And I'm just going to clear this to give us some room. Then I will paste in that command npm install hyphen capital D and then v test and press enter. All right then. So now we have a new project with a package.json file and we've installed V test as a dev dependency into this project which is tucked away inside the node modules folder. There's one more thing I want to do in this lesson and that is to set up a test script inside the package file which we can then run whenever we want to test our functions. So let's open that file up and head to the script property down here which already has a test script created. And at the moment the value of that script is just to echo this message saying we don't have any tests specified. So we just need to replace that with the command v test. And this v test command when we run the script will automatically look for any test files in this project directory and run any tests inside them. Now at the moment we don't have any test files or tests. So if we run this script then it won't do much. And I'm going to quickly show you that by running it down in the terminal which we can do by writing npm run followed by the name of the script which in this case is test and then we hit enter. Now when we do that you can see that vest tells us well there's no tests. Now we can exit this process by pressing Q for quit and that's going to put us back into the terminal. So, that is the script we're going to be running later once we have some tests and we're going to make our first one in the next lesson. By the way, if you want to get instant early access to this entire course now without waiting for it to be uploaded to YouTube, you can grab it on the net.dev website for just $3. So, I will leave a link to this page down below the video. You can also get it as part of this testing bundle I've released, which includes a brand new React testing course as well, not available anywhere else, and that is just $7 for both courses. So again, I'll leave this link below the video. Otherwise, my friends, please don't forget to like the video, share, and subscribe to the channel.
Original Description
In this Unit Testing tutorial series, you'll learn how to setup Vitest in a project, and use it to write effective unit tests for your code.
🍿👇 Get the Testing Bundle (inc Testing Next/React) for just $7:
https://netninja.dev/p/testing-bundle
🍿👇 Get early access to the course on NetNinja.dev:
https://netninja.dev/p/unit-testing-with-vitest-crash-course
🔥👇 Get access to premium courses with Net Ninja Pro:
https://netninja.dev/p/net-ninja-pro/#prosignup
🔗👇 Course files on GitHub:
https://github.com/iamshaunjp/vitest-unit-testing-tutorial
🔗👇 Vitest Docs:
https://vitest.dev/guide/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI