React, TypeScript, and TDD

Rate this content
Bookmark

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.

FAQ

The main idea of the workshop is to demonstrate test-first development with React components, focusing on writing and developing code with tests before implementing functionality, to ensure that components behave as expected.

Test-first development is recommended because it allows developers to define expected behaviors and interfaces upfront, reduce bugs, and maintain a clear understanding of component functionality, which overall improves code quality and reliability.

TypeScript helps in React development by providing type safety, which can catch errors at compile time, improve the readability and maintainability of the code, and enhance the development experience by providing features like autocompletion and refactoring tools.

The workshop discussed using Jest as the test runner and React Testing Library for testing React components. These tools help simulate user events and assert component states and outputs, providing a robust environment for test-first development.

The parent component's role is to manage state that can affect multiple child components. It handles state logic and passes down necessary data and callbacks to child components as props, allowing for cleaner and more modular code architecture.

Refactoring helps improve React application code by making it cleaner, more efficient, and easier to understand. It involves restructuring existing code without changing its external behavior to enhance performance, maintainability, and scalability.

Paul Everitt
Paul Everitt
174 min
12 Oct, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's workshop focuses on test-driven development in React, covering topics such as running, building, and testing React apps, using Prettier for code formatting, refactoring and creating subcomponents, implementing state and event handling in components, and project management and IDE integration. The workshop emphasizes the benefits of test-driven development, the use of IDEs, and the integration with tools like Jest and React Testing Library. TypeScript is also highlighted as a valuable tool for type checking and improving code quality. Overall, the workshop provides a comprehensive guide to developing React components test-first and following best practices in software development.

Available in Español: React, TypeScript y TDD

1. Introduction to Test-Driven Development in React

Short description:

Today's workshop is about developing React components test first. Writing a test before writing the code and continuing to develop the code and component. At the end, you can see the actual result in the browser. The tutorial provides a URL where you can follow along, with write-ups, code, and videos. Each step in the tutorial has a link to working code for that segment.

Today's workshop is going to be me trying to convince you about an idea. And that idea is about developing your React components test first. Writing a test before you write the code. Writing a test as the way to continue developing the code and the component. And only at the very end when you need to see what it actually looks like do you go to a browser.

This tutorial has associated with it something you can follow along on. You can follow along on this url www.jetbrains.com webstorm guide. If you click on tutorials you'll get to this tutorial. And you will find in this tutorial that I'm kind of cheating. Every single thing has the write up that I'm going to talk about it has the code that I'm going to show. It even has a video which is mostly correct.

Let me cover that point real quick about the yes I can. I put it in discord, but you're right. I should have put it over to someone else did all right. All right got me thanks for doing that. Every single thing I have is kind of cheating and putting in here but there's a reason for this I want you to be able to follow along and play around and not worry about every single thing. I say if there's something you didn't get there is something you want to follow up on. I can be easy you easily replaced by a URL to a website. And I should also know that each step in the tutorial has a link to working code for that segment of the tutorial.

2. Setting Up a React Project

Short description:

In this part, we will discuss the benefits of test-driven development and the use of IDEs in the development process. We will also cover the setup and cleanup process, testing, and debugging. The tutorial is up to date with the latest version of Create React App and focuses on writing React components. We will create a new project using WebStorm and NPX, ensuring we have the latest version of Create React App. TypeScript support will be included. The output of Create React App will install the necessary dependencies for our project. Let's dive in!

So what we're going to do is we're going to go through this. make the case for this way of doing development. As I go through building working code. We will try to do a stop maybe like an hour and a half from now or something like that. Take a five minute break. So I've got my timer set for that. We will also be talking in chat. I will try to get both the Discord channel and the, let me make the Zoom chat a little bit bigger, so I won't make a mistake on it.

All right, excuse me for a second. Apologies, I wonder if my microphone picked up that sneeze. We will be taking a little bit of a break, we'll be following along on all these things. And if you get stuck, that's okay, because the link to the working code for each step will teleport you into a magical future where everything works correctly.

One last caveat before beginning, I am a developer advocate for our IDEs. So at JetBrains, for this, it would mean WebStorm, or any of the other IDEs that include the WebStorm plugin. I'm not going to really over promote WebStorm on this. It will be – I use the word IDE, because then it will apply to other smart editors that can look at structure instead of just the strings of text. But at the same time, as I do this tutorial, I get really excited about how the tooling can help us, the tooling of testing and the tooling of typing can make us have a better development experience and stay in the flow.

So, with that in mind, let's begin. This is the sequence we're going to do setup and cleanup and then jump right into testing. We'll do a little bit of debugging to show the benefit of sitting in an IDE, and then straight into a number of things about writing React components. This is up to date as of create React app last week, so I updated all the code for this. I didn't update the videos. There are a couple of places where the videos are out of date on using a jest selector for roles, but we're all smart. We can work around it. I don't know how far we will get through all of this today, but, again, I'm easily replaceable, so if we don't get to the end, you can just watch the video on your own time and see about these last couple of things. They're pretty interesting topics, and they really do show off how far you can go with test first.

All right, let's begin, so I'm going to go to project set up, and I'm going to get us a sample application, and we're going to show three kinds of running things in the code. We're going to show running the development set up, the build your final site set up, and the test set up. And along the way, we're going to start introducing a little bit of things about the IDE. So, with that in mind, if you want to follow along on this page, that's fine. I also have it in the background so I can cheat. I'm going to go over to my IDE, I'm going to start on the welcome to WebStorm screen. I'm going to create a new project.

Now, I could, if you're not using WebStorm, that's fine, you can type this. So, I'm going to create a new project, it's going to be a react project. I'm going to call it Create React App and it's going to prompt it's going to tell me some of the things that's going to be using by default. Let me make sure I have it. I gotta scroll up. We're good. There. We're good here. I'm using hug. I think node 16. Yeah. 16.10 Thank you, WebStorm for answering that question for me. It's going to be using NPX to get create React app. So let's see if you have intelligent idea ultimate, then you have access to the Node JS plugin. And if you've installed Node JS, you have access to all of this.

A little disclaimer about this, I'm going to explain as if some things for some of you might be new. But I also realize that some of you do know what NPX is. I won't dwell on it because it's all really well explained elsewhere. In the world of Node JS. But if you're creating a project, you can use the NPM command to install packages. But what if you want to install like a application, something that you're going to run, which will then create a package. There's this thing called NPX which says, Okay, go get the latest version of this put it in a temporary environment and run it. So you see a lot of these command line tools like learners and formatters and scaffolds like this. Create React app using NPM so that you're always getting the latest version. What is the latest version of Create React app? Which is the scaffolding tool that we want to use. It's four point oh point three. And finally, I want to make a Typescript project because typing is an important part of this.

Okay, with that in mind. a little bit off the end. I should have. I'm going to go back and make a new one. I did well I will go and delete that scratch pen. All right. Okay, and then I'll come back. My apologies I should have deleted that. So new project, react going to call it CRA, and I'm going to ask for TypeScript support, say create new project and WebStorm is putting on my external monitor. So let me resize this here. And then. So. So what WebStorm did was it created a new quote unquote project in that directory and ran some things that it knows how to run, such as create react out, which is creating all the software and then installing all the dependencies. So that's what you see right now is the output of create react app, as if I was doing it on the command line.

Ah, yes next JS, in fact, we're getting more interested in next JS, my colleague Ebenezer Dawn has a long blog post about MDX and next JS that we published recently. Alright, so this is going and getting I mean, let me talk about create react app. For those of you who don't know it, it is a scaffold to generate the best collection of supported software for a modern react app. What that means is add on packages, you might want to use and tooling that you might want to use like lenders and stuff like that. It's going to be over a thousand, maybe 1,500 packages that it installs. And this is just madness. It's so hard to keep all of this stuff in your head. And then as it changes like every single day, this will break that create react app is doing us all a really big favor by taking ownership of what software should you use. And what versions of what software should you use? And they will say we will support all of that. So this is a really important thing to be doing for create react app. And that's why I'm using it as the start point.

How luck we have co-workers here together. All right. And yes, June, you're correct. There are always 100 options to do the same thing for everything, especially in the front end world.

Watch more workshops on topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Concurrent Rendering Adventures in React 18
React Advanced Conference 2021React Advanced Conference 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
Maurice de Beijer
Maurice de Beijer
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
React Hooks Tips Only the Pros Know
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
Web3 Workshop - Building Your First Dapp
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
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
Remix Fundamentals
React Summit 2022React Summit 2022
136 min
Remix Fundamentals
Top Content
Featured WorkshopFree
Kent C. Dodds
Kent C. Dodds
Building modern web applications is riddled with complexity And that's only if you bother to deal with the problems
Tired of wiring up onSubmit to backend APIs and making sure your client-side cache stays up-to-date? Wouldn't it be cool to be able to use the global nature of CSS to your benefit, rather than find tools or conventions to avoid or work around it? And how would you like nested layouts with intelligent and performance optimized data management that just works™?
Remix solves some of these problems, and completely eliminates the rest. You don't even have to think about server cache management or global CSS namespace clashes. It's not that Remix has APIs to avoid these problems, they simply don't exist when you're using Remix. Oh, and you don't need that huge complex graphql client when you're using Remix. They've got you covered. Ready to build faster apps faster?
At the end of this workshop, you'll know how to:- Create Remix Routes- Style Remix applications- Load data in Remix loaders- Mutate data with forms and actions

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

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a new web framework from the creators of React Router that helps you build better, faster websites through a solid understanding of web fundamentals. Remix takes care of the heavy lifting like server rendering, code splitting, prefetching, and navigation and leaves you with the fun part: building something awesome!
Don't Solve Problems, Eliminate Them
React Advanced Conference 2021React Advanced Conference 2021
39 min
Don't Solve Problems, Eliminate Them
Top Content
Humans are natural problem solvers and we're good enough at it that we've survived over the centuries and become the dominant species of the planet. Because we're so good at it, we sometimes become problem seekers too–looking for problems we can solve. Those who most successfully accomplish their goals are the problem eliminators. Let's talk about the distinction between solving and eliminating problems with examples from inside and outside the coding world.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
React provides a contract to developers- uphold certain rules, and React can efficiently and correctly update the UI. In this talk we'll explore these rules in depth, understanding the reasoning behind them and how they unlock new directions such as automatic memoization. 
Using useEffect Effectively
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Can useEffect affect your codebase negatively? From fetching data to fighting with imperative APIs, side effects are one of the biggest sources of frustration in web app development. And let’s be honest, putting everything in useEffect hooks doesn’t help much. In this talk, we'll demystify the useEffect hook and get a better understanding of when (and when not) to use it, as well as discover how declarative effects can make effect management more maintainable in even the most complex React apps.
Modern Web Debugging
JSNation 2023JSNation 2023
29 min
Modern Web Debugging
Top Content
Few developers enjoy debugging, and debugging can be complex for modern web apps because of the multiple frameworks, languages, and libraries used. But, developer tools have come a long way in making the process easier. In this talk, Jecelyn will dig into the modern state of debugging, improvements in DevTools, and how you can use them to reliably debug your apps.