Component Testing With Vitest

Rate this content
Bookmark

Testing is important. Proper unit tests can eliminate the chance for bugs to appear. But which testing framework will be suitable? Let’s explore how we can develop a reliable and efficient strategy for component development and testing with Vitest

Maya Shavin
Maya Shavin
29 min
07 Dec, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk explores the challenges of choosing and learning testing frameworks, emphasizing the importance of planning, automation, and prioritizing unit testing. The VTEST framework is introduced as a fast and stable option for unit testing JavaScript and TypeScript code, with a focus on logic and mocking external dependencies. The Talk also covers testing React hooks, integration testing with TestingLibraryReact, component testing, and achieving code coverage. Best practices include performing accessibility tests, planning tests before coding, and using data test IDs for stability.

Available in Español: Pruebas de Componentes con Vitest

1. Introduction to Testing Frameworks

Short description:

Thank you! I'm Maya Chavin, a Senior Software Engineer at Microsoft working with Microsoft Industry AI. I hate tests, but we know we need them. Codepay complexity and choosing a testing framework are challenges. This talk is about another framework and its learning curve.

Thank you! Okay, so it's pretty early in the morning though and one minute, yep, it's buffering. How are you feeling today? All good? You have coffee? I actually got a sip of coffee and I'm still very cool. The weather is crazy to me.

Anyway, so before we deep dive into our talk, I just want to repeat what I was talking about myself. So I'm Maya Chavin, I'm a Senior Software Engineer at Microsoft. I'm working for a group called Microsoft Industry AI. So we do kind of like leverage different AI technologies to build industry-specific solutions with JetGPT, GPT, and all this module. I have my book published just two days ago, yes. So if you are and you haven't known Vue before, you want to learn Vue, yeah, check it out. And I was an open source maintainer. You can follow me at Maya Chavin or follow my blog post. I post article once in every blue moon, hopefully.

Anyway, testing. So yeah, we have to test, everyone knows that, right? I have a disclaimer before I move on. I hate tests. Yeah. I do hate tests. Every time when I come to tests, I hate tests. Before I come to test, I say, yeah, everyone have to write test, everyone, literally including me. And I always say to everyone in my team, you got to write tests. But when I go to write test myself, I like, come on, why do anyone want test? Well, you know, we know that we need test, but we always face the dilemma, and that's why we hate test, kind of. Codepay complexity. Anyone who works with a very large code page will know the pain when you need to write test. And you need to write test in order to make sure that your test will only check what happened in your code and not because someone else do something and then it affects your qualities of your code. Testing framework. You're not going to choose framework. What framework are you going to choose? And yes, this talk is about another framework. Yep. I know that. And learning curve.

2. Challenges of Testing Frameworks

Short description:

Every time we talk about a new testing framework, we switch from one to another because it's cooler and faster. But there's always a learning curve. Not everyone agrees, and APIs differ. The dilemma is ensuring 90% code coverage. If not, PRs won't be merged. It all comes down to time. How do we allocate time and effort to testing?

Every time we talk about a new testing framework, we switch from one testing framework to another because the other one is way cooler and faster. However, we always have a learning curve. Not everyone likes to agree, and not every API looks, feels, or does the same thing. This leads to a specific point of dilemma. Even if you pass all of this, you still need to make sure that your tests cover a minimum of 90% of your code base. If someone decides that 90% coverage is required, then 89% or 99% won't cut it. This can prevent your pull request from being merged, and it can be frustrating. Ultimately, this dilemma comes down to one thing: time. How are we going to allocate time and effort to testing? We need to do it, but how?

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Test Effective Development
TestJS Summit 2021TestJS Summit 2021
31 min
Test Effective Development
Top Content
Developers want to sleep tight knowing they didn't break production. Companies want to be efficient in order to meet their customer needs faster and to gain competitive advantage sooner. We ALL want to be cost effective... or shall I say... TEST EFFECTIVE!But how do we do that?Are the "unit" and "integration" terminology serves us right?Or is it time for a change? When should we use either strategy to maximize our "test effectiveness"?In this talk I'll show you a brand new way to think about cost effective testing with new strategies and new testing terms!It’s time to go DEEPER!
The Art of ‘Humble Views’: Testing React Native Apps the Smart Way
TestJS Summit 2023TestJS Summit 2023
32 min
The Art of ‘Humble Views’: Testing React Native Apps the Smart Way
In this talk, we explore the divisive world of testing, where developers often find themselves torn between writing no tests and striving for 100% test coverage. Learn how to navigate these polarizing positions and adopt a more nuanced strategy that makes testing efficient and effective.We'll dive into the concept of 'Humble Views,' where we minimize untestable objects by extracting logic from UI elements into test-friendly parts of the codebase. This approach simplifies testing, focusing on business logic instead of UI complexities. Discover how the Model-View-Presenter (MVP) architecture helps achieve this, with presenters serving as a logical layer for testing and hooks aiding in separating logic from UI components.Throughout the talk, we'll discuss the trade-offs of this approach, the role of End-to-End (E2E) tests, and how to strike the perfect balance between too much and too little testing. Join us as we delve into the art of creating 'Humble Views,' ensuring that our React Native apps are scalable, maintainable, and effectively tested!
We May Not Need Component Testing
Vue.js Live 2024Vue.js Live 2024
26 min
We May Not Need Component Testing
Testings are mandatory and unit tests are the foundation for building a good testing system for our project. But for front end projects which involve components, how many unit tests are considered efficient and not overkill? Should we use additional libraries like Testing Library or Vue Test Utils with Vitest to test a component, when we can perform the same with just Playwright? Whether a component test using an E2E framework like Playwright is really a kill for? Let's find out in my talk.
It's a (Testing) Trap! - Common Testing Pitfalls and How to Solve Them
TestJS Summit 2021TestJS Summit 2021
20 min
It's a (Testing) Trap! - Common Testing Pitfalls and How to Solve Them
It’s a trap” - a call or feeling we all might be familiar with, not only when it comes to Star Wars. It’s signalizing a sudden moment of noticing imminent danger. This situation is an excellent allegory for an unpleasant realization in testing. Imagine having the best intentions when it comes to testing but still ending up with tests failing to deliver you any value at all? Tests who are feeling like a pain to deal with?
When writing frontend tests, there are lots of pitfalls on the way. In sum, they can lead to lousy maintainability, slow execution time, and - in the worst-case - tests you cannot trust. But it doesn’t have to be that way. In this session, I will talk about developers’ common mistakes (including mine), at least from my experience. And, of course, on how to avoid them. Testing doesn’t need to be painful, after all.
How to Catch a11y Defects During Unit and E2E Testing
TestJS Summit 2021TestJS Summit 2021
7 min
How to Catch a11y Defects During Unit and E2E Testing
For developers, it is better to catch any a11y defects during unit and e2e testings. This talk is going to show how to automate a11y testing using jest and cypress.
Unit Testing Angular Applications
TestJS Summit 2022TestJS Summit 2022
24 min
Unit Testing Angular Applications
Angular offers many things out of the box, including various testing-related functionalities. This presentation will demonstrate how we can build on Angular's solid unit testing fundamentals and apply certain patterns that make testing easier. Topics covered include: test doubles, testing module pattern, harnesses, "recipes" on how to test some common cases, and more!

Workshops on related topic

Introduction to React Native Testing Library
React Advanced Conference 2022React Advanced Conference 2022
131 min
Introduction to React Native Testing Library
Workshop
Josh Justice
Josh Justice
Are you satisfied with your test suites? If you said no, you’re not alone—most developers aren’t. And testing in React Native is harder than on most platforms. How can you write JavaScript tests when the JS and native code are so intertwined? And what in the world are you supposed to do about that persistent act() warning? Faced with these challenges, some teams are never able to make any progress testing their React Native app, and others end up with tests that don’t seem to help and only take extra time to maintain.
But it doesn’t have to be this way. React Native Testing Library (RNTL) is a great library for component testing, and with the right mental model you can use it to implement tests that are low-cost and high-value. In this three-hour workshop you’ll learn the tools, techniques, and principles you need to implement tests that will help you ship your React Native app with confidence. You’ll walk away with a clear vision for the goal of your component tests and with techniques that will help you address any obstacle that gets in the way of that goal.you will know:- The different kinds React Native tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting text, image, and native code elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RNTL tests and how to handle them- Options for handling native functions and components in your JavaScript tests
Prerequisites:- Familiarity with building applications with React Native- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Native Testing Library- Machine setup: Node 16.x or 18.x, Yarn, be able to successfully create and run a new Expo app following the instructions on https://docs.expo.dev/get-started/create-a-new-app/