#test driven development react

Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application 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 DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Effective Testing Strategies for React Components
Effective Testing Strategies for React Components
Article
Testing the contract of a component by focusing on inputs and outputs.Utilizing Jest mock functions for testing function props and module mocks for external modules.Implementing the React Testing Library for rendering components and simulating user interactions.Addressing asynchronous behavior in tests using the findBy query and awaiting promises.Balancing test coverage between lower-level and higher-level components.Testing React components effectively involves focusing on the contract of the component, which means understanding the inputs and outputs. This mindset helps ensure that tests are meaningful and maintainable. Instead of delving into implementation details, the aim is to test observable behavior that a user would interact with.Components have various inputs and outputs. Inputs include props, user interactions, and responses from external functions, while outputs are the rendered UI and calls to external functions. Testing begins by rendering the component and simulating user interactions to observe the outputs.Jest mock functions are essential for testing function props. They allow you to simulate and make assertions on functions passed as props. For example, if a component receives a function to handle form submission, a Jest mock function can verify that this function is called with the correct arguments.Jest module mocks are another powerful tool for handling external modules. They enable you to mock out any JavaScript module that your component depends on, ensuring that tests remain isolated from external dependencies. This is particularly useful when dealing with API calls, as it allows you to simulate responses without hitting a real server, thereby avoiding flakiness and improving test reliability.When testing asynchronous behavior, such as loading data from an API, it's crucial to handle promises correctly. The findBy query from the React Testing Library is an excellent way to wait for elements to appear on the page. It returns a promise that resolves when the element is found, allowing you to await its presence before making assertions.Organizing tests effectively involves making judgment calls about what to test at each level. Lower-level component tests focus on the detailed behavior of individual components, while higher-level tests ensure that components integrate correctly. This balance helps maintain a comprehensive test suite without excessive duplication.In addition to testing visible outputs, it's important to confirm that components make the correct external calls. This involves asserting not only on the presence of elements but also on the calls made to external functions, such as ensuring that the correct API endpoints are hit with the appropriate parameters.By focusing on the contract of a component and using tools like Jest and the React Testing Library, developers can create robust and reliable tests. This approach emphasizes testing what matters most to users and ensures that components behave as expected, both in isolation and when integrated with others.
Designing Effective Tests with React Testing Library
React Day Berlin 2022React Day Berlin 2022
131 min
Designing Effective Tests with React Testing Library
Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Prerequisites:- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
BDD & TDD in React
React Summit Remote Edition 2021React Summit Remote Edition 2021
23 min
BDD & TDD in React
Top Content
Today's Talk covers TDD and BDD in React testing, including the importance of testing practice in React. The Talk discusses the evolution of testing, the daily flow of developing a feature with TDD and BDD, and best practices for testing. It also emphasizes the significance of using the right tools and writing tests that resemble user actions. Applying tests in practice leads to better architecture decisions.