Check out a free preview of the full JavaScript Testing Practices and Principles course

The "Jest Q&A" Lesson is part of the full, JavaScript Testing Practices and Principles course featured in this preview video. Here's what you'd learn in this lesson:

While answering a student question about the appropriateness to mock an API when the actual API is still in development, Kent is lead to the topic of discussing whether to use dependency injection or mocking.

Preview
Close

Transcript from the "Jest Q&A" Lesson

[00:00:00]
>> Kent C. Dodds: You have a question?
>> Speaker 2: Yeah, going back to the mocks real quick. Is there a situation where let's say, you have an internal API that is not yet ready in the backend? Is that a situation where out of necessity you want a mockup at that end?
>> Kent C. Dodds: Yeah, that's a good question.

[00:00:19]
So yeah, if you wanna create a mock for an API that's not ready yet. Well, taking a step back, [COUGH], when that API is ready, I will want to have a mock for it, probably. It kind of depends. If you're doing an integration task that integrates several services together, maybe you want to actually make that network request to verify things, whatever.

[00:00:45]
You can rely on that service for your tests and stuff. And so in that situation, you might not wanna eventually have a mock for it. But if we're talking about unit tests, these lower level tests, then you probably will want to have a mock for it anyway. And so I just create a mock for it whether it's ready or not.

[00:01:02]
If it's not ready yet, then in the process of development, it's kind of nice to have a mock API. Not in the context of testing, but just in the context of being able to develop the software. That's just a general good practice. And so, if you're tests are gonna be actually hitting the API, ultimately, when it's available, then you'll just have your tests hit that mock API.

[00:01:28]
You'll have to spin up that mock server, or whatever, and hit those. But in general, the way I think about testing, with the exception of maybe in stage or something, like in the CI first stage, you'll want to actually hit live services, and stuff like that. In general, if I'm running the tests on my machine, I want to be totally offline, to be able to run all those tests.

[00:01:55]
And so I'm gonna have those services on my computer anyway, I'm gonna fire up those services. So if one of those is not ready yet, I'm gonna have a mock version of that service. And my test will hit that mock versio, just like my development process would. Does that make sense?

[00:02:09]
>> Speaker 2: Yeah.
>> Kent C. Dodds: Cool.
>> Kent C. Dodds: Cool, any other questions? All righty, cool, so let's go ahead and what I'm gonna do is I'm actually going to reset git, so my project's in a clean slate. I don't want any of my changes to mess up anything else. So you don't have to do this, but I'm going to just git reset hard, get everything back to a fresh spot.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now