Autoplay

    React Testing: Setting up dependencies

    Trevor MillerTrevor Miller

    To write tests for our React code, we need to first install some libraries for running tests and writing assertions. In this lesson we walk through setting up Mocha as our test runner and expect as our assertion library. We will also set up some React and JSX specific test tools (React Test Utils) to make writing our tests easier.

    NOTE: There are many alternatives to Mocha, expect, and the other test tools we use in this course. Although we use these specific tools and libraries, the testing principles apply to all other tools.

    reactReact
    >0.14.2 <15.3.2
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 Before we write tests for our React components, we're going to need a test runner. I'm going to NPM install Mocha. We also need to be able to write assertions for our tests, so I'm going to install the Expect library. Finally, we're going to need the React addons TestUtils package, which has been developed by the React team to make it easier to test React components.

    00:22 I'm going to add the -save-dev flag onto this command, so that it saves these packages into our package.json devDependencies. We'll run this, and we can see in our package.json that the dependencies have been installed, as expected.

    00:36 We're now set up to write up some tests. I'd like to point out that, although we're using Mocha and Expect, there are many test tools that you can use, but the principles in this course apply to all these different test tools.

    Discuss

    Discuss