惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Spread Privacy
Spread Privacy
K
Kaspersky official blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Security Latest
Security Latest
TaoSecurity Blog
TaoSecurity Blog
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
H
Hacker News: Front Page
C
Cybersecurity and Infrastructure Security Agency CISA
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
AI
AI
G
GRAHAM CLULEY
IT之家
IT之家
P
Privacy & Cybersecurity Law Blog
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
D
Docker
Recent Announcements
Recent Announcements
O
OpenAI News
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
Troy Hunt's Blog
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode Profile a React App for Performance
How to add testing to an existing project
2019-10-28 · via Kent C. Dodds Blog

We've all been there. You're working on an app that was "just a prototype" and was "definitely not going to get shipped this way." Or maybe you didn't have time to prototype it, and you certainly didn't have time to write tests for it. Or perhaps you join a team that's just never tested their code before (or maybe you're the one who's just getting into it). However you got here, you've got an app that's breaking in production. Your palms get sweaty every time your mouse hovers over the "deploy" button. You still need to build features and fix bugs, but you've finally decided it's worth the investment to test your app. You want confidence.

But where do you start?

You have tens of thousands, hundreds of thousands, or even millions (😱) of lines of code running in this app. The task feels so huge that you avoid thinking about it. But you have to start somewhere. Let's zoom out and see how the principles from the Testing Trophy can be applied to your app.

Here's our testing trophy, for reference. Read more about it in the blogpost:

The four types of tests

When you're just starting, you should focus on the things that provide the best return on your investment. So that's what we're going to do here.

Static testing tools include:

Setting up ESLint is by far the lowest "cost" thing you can apply to your codebase and can help you avoid an entire category of bugs. It's also reasonable to incrementally adopt over time because of its configurability. Disable rules that are broken all over your app today, enable others as warnings, and incrementally work on fixing and eliminating problematic code from your app.

Prettier is not difficult to setup, but it often requires a little more convincing for folks who aren't used to using a code formatter. You can read "Why Prettier?" for more on why it's so useful. I consider it a static testing tool because if you write your syntax incorrectly, then prettier will re-format it in a way that looks funny which highlights the problem.

TypeScript or Flow is a much harder sell. These tools are incrementally adoptable as well, but they can take a fair amount of work to get configured and working in your build pipeline. Even though these tools are basically "JavaScript with types" that "with types" bit has a very significant learning curve. I can tell you that a static type checker is definitely worth the effort. One piece of advice I would give you is to not worry about getting your types perfect from the start. Don't be afraid of any (or unknown)! They don't make your types as useful, but I've seen many people give up on TypeScript because they spent so much time banging their head against the wall trying to make their types perfect. Go ahead and leave it as imperfect as it is, then you can come back later when you know more and improve things later.

For more on the value of static testing tools, read Eliminate an entire category of bugs with a few simple tools

Step 2 - Make a single E2E test

We're jumping from the bottom of the testing trophy to the top here. This may take a fair bit of effort, but it will pay off in a HUGE way. There are various ways to go about doing this, but probably the quickest is to write a single E2E test that runs on production (or a production-like environment) and goes through a common/important user flow. This may be a pretty long test. That's fine (making E2E tests too short is a common testing mistake anyway). But just imagine how helpful it would be if you could hire a manual QA tester that could run through the most important user flow of your app every time you deploy your app to make sure it's still working. How valuable would that be? HUGE.

This one won't be easy. Depending on how complex your app, it could take a day or several days to get the test written and running on CI. If you have to set it on a cron job to run every hour or so that's fine. If you can't get it running on CI right away that's fine too. You can iterate to a better situation. Don't let perfection prevent you from improvement.

Step 3 - Write a single unit test

We're jumping back to the next type of test. Unit tests are the simplest type of test to write. Pick the simplest single pure function in your app and install/configure all the tools you need to get that simple pure function tested. Once you have that one thing tested, then it will be WAY easier to write additional tests. Many people don't write tests because all the effort it takes to get tools in place. So your job is to get the tools in place to make it easier to get tests going.

Step 4 - Write more tests

From here, you should have all the tools configured so you can write more tests. You can now focus on more integration tests. With each one you'll face new challenges and you may need to write some module mocks, but the more you do, the easier future tests become.

You may find it interesting that it isn't until this step that I suggest you start focusing on integration tests. While they do typically give you the most bang for your buck, they're often harder to get going until all the tooling is in place first. Also, as valuable as integration tests are, that first comprehensive E2E test is WAY more valuable.

Step 5 - Teach your whole team how to test

Now that your tools are all in place, your team needs to start writing tests for the code they create and maintain. The best way to do this is to convince your manager to buy your whole team (or your CTO to buy the whole company) licenses to TestingJavaScript.com. It is unequivocally the best place to learn how to test JavaScript applications on the internet (very soon to include testing Node.js backends as well).

In fact, TestingJavaScript.com teaches you how to configure all the tools I mention in this blog post, so I'd recommend that you get your boss to buy you licenses for it before you start working so you can save yourself the headache of learning how to get them configured and just get work done.

Having everyone learning from TestingJavaScript.com will unify your team's testing strategy and save yourself a TON of time trying to decide the best way to approach the problem. TestingJavaScript.com gives that to you. All you need to do is execute what you learn on your app.

Conclusion

I hope this is helpful to you in your efforts to improve testing on the project you so dearly want to ship with confidence. You'll sleep better at night with a confidence infusing testing strategy. 😴 Sweet dreams.