-
Notifications
You must be signed in to change notification settings - Fork 3
Writing Tests
What are they for?
Unit tests are tests for small subsections of an app. They test intended functionality of individual functions or classes, without focusing on interaction with other functions or classes.
What to test
In react apps, a few things must be tested in unit tests:
- Helper functions associated with the component
- Component lifecycle methods
- State mutation methods
- Rendered Markup
How we test
We use a testing framework called Jest. If you're new to it, there are some tutorials here to get you up to speed.
It is preferred to test all function logic in separation from the component itself. To do this, be sure to write as much as possible outside of the component, and just use what you need to inside it. This allows you to export that logic to be imported into a test file.
Use snapshot testing to test rendered markup when necessary. This will error out whenever the markup changes, ensuring no accidental changes to markup are made. Since snapshot tests are fragile and sometimes hard to read, the following things are normally encouraged:
- Mock imported react components that you aren't testing directly.
- Read over the generated snapshots carefully. They will show what props are passed to each component and the rendered markup.
- Keep the number of snapshot tests low and only for difficult to test things. A large number of snapshots are difficult to review, and can lead to bugs slipping by.
Running Tests Locally
Running yarn test from the project root will run unit tests locally.
What are they for?
Testing a completed project and how all of the various pieces work together. In this case, that essentially means loading up the built app and simulating interaction with it (clicking around, filling out forms, etc).
What to test
In two words: critical components of an app.
Integration tests are fragile and difficult to write, so they should be kept as short and modular as possible while testing all critical functionality.
These are the equivalent to loading up a page in the browser and interacting with it directly. We do this through a platform called Selenium and webdriver. This allows a server to be started up with a controlled browser. Issuing commands to the server causes interaction with the web page as if a user were doing it. Currently, this selenium server is run on Sauce Labs. This allows us to specify which browser and which operating system we want to work with.
Running tests locally
We haven't set up a standard way for running these tests locally, but you can still do it if you wish by running selenium locally. Selenium-Standalone is a package that you can download and run to start up a selenium server on your machine. Then, you can change the WebDriver utility (/src/util/WebDriver.js) to point to your local selenium server.
-
yarn test-allwill run all unit and integration tests locally -
yarn test-intwill run all integration tests locally
In progesss 💯