1. 23
    Run a Simple Test with Jest in a React App
    2m 26s

Run a Simple Test with Jest in a React App

Andy Van Slaars
InstructorAndy Van Slaars
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 5 years ago

Testing should be part of any substantial project, so we'll take the effort out of it by incorporating testing configuration into our boilerplate. In this lesson, we'll add Jest as a project dependency. We’ll add a test script to our package.json, create a sample test and verify that our test script works.

Instructor: [00:01] Any time I start a new project from this boilerplate, I'd like to be set up to run test. We're going to do that with Jest. I'm going to do an npm i -D, to save as a dev dependency, jest.

[00:24] With that installed, let's open up the package.json file. In my scripts, I'm going to update this test script. I'm going to remove this default script. I'm going to replace it with Jest. We can save that.

[00:39] Back in the terminal, I'm going to do an npm test to run the script. When I do this, I'm going to get an error that no tests were found. Let's create our first test file. We can either put tests in an tests directory, or we can colocate them. We can use the .spec or .test.js extension.

[01:01] I'm going to colocate my tests. In source, I'm just going to add a new file. I'm going to call it app.spec.js. In this file, I'm going to start with a describe block. Describe is going to take a string for what we're testing. In this case, it'll be our app component and a function. I'll just use an error function here.

[01:26] Then we can use either test or it to define an individual test. For now, we're just going to put in a test that passes. We're just going to say, "It runs and passes." This isn't going to be a valuable test, but it'll allow us to test our Jest script.

[01:44] In here, I'm just going to say that I expect true to be true. I can save that file. Back in the terminal, I'm going to npm test again. That's going to run our app.spec.js. We'll see that it passes.

[02:03] If I want to make sure that it can fail, I can just come in here and make an expectation that can't possibly pass. We'll npm test one more time. Our test fails. Let's fix that, npm test. Now we have a passing test and a working test script.

egghead
egghead
~ 15 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today