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

This talk has been presented at TestJS Summit 2023, check out the latest edition of this JavaScript Conference.

FAQ

Maya Chavin is a Senior Software Engineer at Microsoft, working for a group called Microsoft Industry AI. She leverages different AI technologies to build industry-specific solutions with JetGPT, GPT, and other modules.

Maya Chavin mentions several challenges in software testing, including code complexity, choosing the right testing framework, the learning curve associated with new frameworks, and ensuring that tests meet minimum coverage requirements.

VTest is a JavaScript and TypeScript unit testing framework developed by the VIT team. It is designed for fast testing with support for TypeScript and JSX out of the box and is compatible with other testing frameworks like Jest and Mocha.

Maya Chavin discusses three types of tests: unit tests, which test individual pieces of code; integration tests, which test the interaction between different pieces of code; and end-to-end tests, which test the actual user interaction using real data.

The recommended approach is to split the component logic into hooks and test these hooks separately. This allows for easier testing and maintenance, as changes in one part of the logic won't affect the entire component.

Maya Chavin recommends using additional tools like HappyDOM for a faster and more reliable DOM API, and Xscore React for accessibility testing of React components.

Planning tests before coding is important because it helps ensure that the tests will cover the necessary functionalities and scenarios. It also helps in choosing the right testing framework and tools, and in identifying the types of tests needed for different parts of the project.

Using data-test-id in testing helps ensure that test selectors remain stable and are not affected by changes in the component's class or ID attributes. This makes the tests more reliable and easier to maintain.

The focus of Maya Chavin's talk is on testing in software development, specifically using the VTest framework for unit and integration testing of React components.

Asynchronous code in testing can be handled by mocking asynchronous functions, using tools like VTest to spy on functions, and using the 'waitFor' function to ensure that asynchronous operations are completed before assertions are made.

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
This Talk introduces Test Effective Development, a new approach to testing that aims to make companies more cost-effective. The speaker shares their personal journey of improving code quality and reducing bugs through smarter testing strategies. They discuss the importance of finding a balance between testing confidence and efficiency and introduce the concepts of isolated and integrated testing. The speaker also suggests different testing strategies based on the size of the application and emphasizes the need to choose cost-effective testing approaches based on the specific project requirements.
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
This Talk discusses the challenges of testing in React and React Native applications, particularly with regards to barcode scanning. It explores the violation of separation of concerns in React and proposes the use of the HumbleObject model to simplify testing and improve code cleanliness. The MVP model is also introduced as a way to separate UI state and logic from the component. The importance of following patterns, standardization, and teaching principles is emphasized, along with the benefits of using custom hooks to share business logic. The potential of AI tools in code refactoring is mentioned as well.
We May Not Need Component Testing
Vue.js Live 2024Vue.js Live 2024
26 min
We May Not Need Component Testing
Component testing is a gray area between integration and unit testing. The demo app focuses on the cart component and writing test cases for Playwright component test and VTest. The first cart test encounters a bug with the invisible method in View Test.
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
This Talk explores the pain points and best practices in software testing, emphasizing the importance of simplicity and comprehensibility in test design. It discusses techniques such as the three-part rule for test titles, the triple-A pattern for test structure, and the use of clear and descriptive names in tests. The Talk also highlights the traps of testing implementation details and using fixed waiting times. The speaker encourages teamwork and learning from experience to improve testing practices.
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
This Talk provides ways to catch accessibility defects during testing, including adding accessibility testing to a website for Studio Ghibli using React, NX, Jazz, JazzX, Cypress, and CypressX. The importance of unitizing components and conducting end-to-end testing with Cypress and CypressX is emphasized to ensure accessibility. The process of setting up CypressX testing is explained, highlighting the use of typings and the CypressX support file. These tools make it easier for developers to avoid accessibility bugs during development.
Mock Service Worker 2.0
TestJS Summit 2023TestJS Summit 2023
27 min
Mock Service Worker 2.0
MSW is an API mocking library that simplifies the process of intercepting requests and mocking responses. It leverages standard JavaScript APIs like the ServiceWorker API and the Fetch API. MSW has seen significant adoption, with over 90,000 projects on GitHub and 2.5 million weekly downloads on npm. The recent release of Node.js 18 has allowed for refactoring and simplification in MSW. MSW supports TypeScript and can be used for contract testing with tools like PACT I-O.

Workshops on related topic

Learn To Use Composables: The Swiss Army Knife Of Vue Developers
Vue.js Live 2024Vue.js Live 2024
163 min
Learn To Use Composables: The Swiss Army Knife Of Vue Developers
Workshop
Juan Andrés Núñez Charro
Juan Andrés Núñez Charro
Composables (composition functions) are stateful/stateless functions that can leverage Vue's reactivity API, decoupling it from components.This shift in perspective opens the possibility for tackling common scenarios in a new and creative way. In this workshop, you will learn how to solve typical problems every Vue developer faces, using composables:
- Data store;- Component cross-communication;- Utility functions (DOM, API, etc);And more.
Introduction to React Native Testing Library
React Advanced 2022React Advanced 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/