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.

This workshop has been presented at React Advanced Conference 2021, check out the latest edition of this React Conference.

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.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.

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
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize 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 web framework built on React Router that focuses on web fundamentals, accessibility, performance, and flexibility. It delivers real HTML and SEO benefits, and allows for automatic updating of meta tags and styles. It provides features like login functionality, session management, and error handling. Remix is a server-rendered framework that can enhance sites with JavaScript but doesn't require it for basic functionality. It aims to create quality HTML-driven documents and is flexible for use with different web technologies and stacks.
Don't Solve Problems, Eliminate Them
React Advanced Conference 2021React Advanced Conference 2021
39 min
Don't Solve Problems, Eliminate Them
Top Content
Kent C. Dodds discusses the concept of problem elimination rather than just problem-solving. He introduces the idea of a problem tree and the importance of avoiding creating solutions prematurely. Kent uses examples like Tesla's electric engine and Remix framework to illustrate the benefits of problem elimination. He emphasizes the value of trade-offs and taking the easier path, as well as the need to constantly re-evaluate and change approaches to eliminate problems.
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
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
Using useEffect Effectively
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Today's Talk explores the use of the useEffect hook in React development, covering topics such as fetching data, handling race conditions and cleanup, and optimizing performance. It also discusses the correct use of useEffect in React 18, the distinction between Activity Effects and Action Effects, and the potential misuse of useEffect. The Talk highlights the benefits of using useQuery or SWR for data fetching, the problems with using useEffect for initializing global singletons, and the use of state machines for handling effects. The speaker also recommends exploring the beta React docs and using tools like the stately.ai editor for visualizing state machines.
Modern Web Debugging
JSNation 2023JSNation 2023
29 min
Modern Web Debugging
Top Content
This Talk discusses modern web debugging and the latest updates in Chrome DevTools. It highlights new features that help pinpoint issues quicker, improved file visibility and source mapping, and ignoring and configuring files. The Breakpoints panel in DevTools has been redesigned for easier access and management. The Talk also covers the challenges of debugging with source maps and the efforts to standardize the source map format. Lastly, it provides tips for improving productivity with DevTools and emphasizes the importance of reporting bugs and using source maps for debugging production code.