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

推荐订阅源

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 know what to test
Kent C. Dodds 🏹 @kentcdodds · 2019-04-13 · via Kent C. Dodds Blog

Knowing how to test is great and important. I've created a LOT of content that teaches people the fundamentals of testing, how to configure tools, how to write tests for specific scenarios, and so on. But knowing how to write tests is only half the battle to achieve confidence in your application. Knowing what to test is the other–very important–half of the battle.

In my workshop material and on TestingJavaScript.com, I do talk about how to know what to test, but I get asked about this enough that I thought it would be good to write a blog post about it. So here you go!

Remembering why we test

We write tests to be confident that our application will work when the user uses them. Some people write tests to enhance their workflow as well and that's great, but I'm ultimately interested in confidence. That being the case, what we test should map directly to enhancing our confidence. Here's the key point I want you to consider when writing tests:

Think less about the code you are testing and more about the use cases that code supports.

When you think about the code itself, it's too easy and natural to start testing implementation details (which is road to disaster).

Thinking about use cases though gets us closer to writing tests the way the user uses the application:

The more your tests resemble the way your software is used, the more confidence they can give you.

16 1,126

Code Coverage < Use Case Coverage

Code coverage is a metric that shows us what lines of our code is being run during the tests. Let's use this code as an example:

function arrayify(maybeArray) {
	if (Array.isArray(maybeArray)) {
		return maybeArray
	} else if (!maybeArray) {
		return []
	} else {
		return [maybeArray]
	}
}

Right now, we have no tests for this function, so our code coverage report will indicate that we have 0% coverage of this function. The code coverage report in this case helps give us an idea that tests are needed, but it does NOT tell us what's important about this function, nor does it tell us the use cases this function supports which is the most important consideration we keep in mind as we write tests.

In fact, when considering an entire application and wondering what to test, the coverage report does a very poor job of giving us insight into where we should be spending most of our time.

So the coverage report helps us identify what code in our codebase is missing tests. So when you look at a code coverage report and note the lines that are missing tests, don't think about the ifs/elses, loops, or lifecycles. Instead ask yourself:

What use cases are these lines of code supporting, and what tests can I add to support those use cases?

"Use Case Coverage" tells us how many of the use cases our tests support. Unfortunately, there's no such thing as an automated "Use Case Coverage Report." We have to make that up ourselves. But the code coverage report can sometimes help us identify use cases that we're not covering. Let's try it out.

So if we read the code and consider it for a minute, we can identify our first use case to support: "it returns an array if given an array." This use case statement is actually a great title for our test.

test('returns an array if given an array', () => {
	expect(arrayify(['Elephant', 'Giraffe'])).toEqual(['Elephant', 'Giraffe'])
})

And with that test in place, our coverage report looks something like this (highlighted lines are covered):

function arrayify(maybeArray) {
	if (Array.isArray(maybeArray)) {
		return maybeArray
	} else if (!maybeArray) {
		return []
	} else {
		return [maybeArray]
	}
}

Now, we can look at the remaining lines and determine that there are two more use cases that our tests don't support yet:

  • it returns an empty array if given a falsy value
  • it returns an array with the given argument if it's not an array and not falsy

Let's add tests for those use cases and see how it effects the code coverage.

test('returns an empty array if given a falsy value', () => {
	expect(arrayify()).toEqual([])
})
function arrayify(maybeArray) {
	if (Array.isArray(maybeArray)) {
		return maybeArray
	} else if (!maybeArray) {
		return []
	} else {
		return [maybeArray]
	}
}

Nice, almost there!

test(`returns an array with the given argument if it's not an array and not falsy`, () => {
	expect(arrayify('Leopard')).toEqual(['Leopard'])
})
function arrayify(maybeArray) {
	if (Array.isArray(maybeArray)) {
		return maybeArray
	} else if (!maybeArray) {
		return []
	} else {
		return [maybeArray]
	}
}

Cool! Now we can be confident that so long as we don't need to change the use cases of this function, our tests will continue to pass.

Code coverage is not a perfect metric, but it can be a useful tool in identifying what parts of our codebase are missing "use case coverage".

Code coverage can hide use cases

Sometimes, our code coverage report indicates 100% code coverage, but not 100% use case coverage. This is why sometimes I try to think of all the use cases before I even start writing the tests.

For example, let's imagine that the arrayify function had been implemented like this instead:

function arrayify(maybeArray) {
	if (Array.isArray(maybeArray)) {
		return maybeArray
	} else {
		return [maybeArray].filter(Boolean)
	}
}

With that, we can get 100% coverage with the following two use cases:

  • it returns an array if given an array
  • it returns an array with the given argument if it's not an array

But if we could look at a use case coverage report, it would indicate that we're missing this use case:

  • it returns an empty array if given a falsy value

This could be bad because now our tests aren't giving us as much confidence that our code will work when users use it like this: arrayify(). Right now, it's fine because even though we don't have a test for it, our code supports that use case. But the reason we have tests in place is to ensure that code continues to support the use cases we intend it to support, even as things change.

So, as an example for how missing this test can go wrong, someone could come around, see that .filter(Boolean) thing and think: "Huh, that's weird... I wonder if we really need that." So they remove it, and our tests continue to pass, but any code that relied on the falsy behavior is now broken.

Key takeaway:

Test use cases, not code.

How does this apply to React?

When writing code, remember that you already have two users that you need to support: End users, and developer users. Again, if you think about the code rather than the use cases, it becomes dangerously natural to start testing implementation details. When you do that, your code now has a third user.

Here are a few aspects of React that people often think about testing which results in implementation details tests. For all of these, rather than thinking about the code, think about the observable effect that code has for the end user and developer user, that's your use case, test that.

  • Lifecycle methods
  • Element event handlers
  • Internal Component State

Conversely, here are things that you should be testing because they concern your two users. Each of these could change the DOM, make HTTP requests, call a callback prop, or perform any other number of observable side effects which would be useful to test:

  • User interactions (using userEvent from @testing-library/user-event): Is the end user able to interact with the elements that the component renders?
  • Changing props (using rerender from React Testing Library): What happens when the developer user re-renders your component with new props?
  • Context changes (using rerender from React Testing Library): What happens when the developer user changes context resulting in your component re-rendering?
  • Subscription changes: What happens when an event emitter the component subscribes to changes? (Like firebase, a redux store, a router, a media query, or browser-based subscriptions like online status)

How do I know where to start in an app?

So we know how to think about what to test for individual components and even pages of our app, but where do you start? It's a bit overwhelming. Especially if you're just getting started with testing in a large app.

So here's what you do, consider your app from the user's point of view and ask:

What part of this app would make me most upset if it were broken?

Alternatively, and more generally:

What would be the worst thing to break in this app?

I'd suggest making a list of features that your application supports and prioritize them based on this criteria. It's a great exercise to do with your team and manager. This meeting will have the side-effect of helping everyone in the room understand the importance of testing and hopefully convince them that it should receive some level of prioritization in all the other feature work you need to do.

Once you have that prioritized list, then I suggest writing a single end to end (E2E) test to cover the "happy path" that most of your users go through for the particular use case. Often you can cover parts of several of the top features on your list this way. This may take a little while to get set up, but it'll give you a HUGE bang for your buck.

The E2E tests aren't going to give you 100% use case coverage (and you should not even try), nor will they give you 100% code coverage (and you should not even record that for E2E tests anyway), but it will give you a great starting point and boost your confidence big time.

Once you have a few E2E tests in place, then you can start looking at writing some integration tests for some of the edge cases that you are missing in your E2E tests and unit tests for the more complex business logic that those features are using. From here it just becomes a matter of adding tests over time. Just don't bother with targeting a 100% code coverage report, it's not worth the time.

For more on establishing a culture of testing and reasonable code coverage targets, I suggest watching Aaron Abramov's talk at AssertJS 2018: Establishing testing patterns with software design principles

Read more about the distinction between the different types of tests here: Static vs Unit vs Integration vs E2E Testing for Frontend Apps

Conclusion

Given enough time and experience, you develop an intuition for knowing what to test. You'll probably make mistakes and struggle a bit. Don't give up! Keep going. Good luck.