- To have completed the food-app Lab 1 to Lab 8
- Knowledge on how to write react components
- Knowledge of how to write tests with react-testing-library
Previously in the labs we have approached development in this order
- Write component code
- Create test and run that single test case in watch mode
- Write test to cover the behaviour of the component
- Ensure test goes from red to green
- Commit
This workflow is beneficial for developers who are still new to the syntax for React and react-testing-library, as it allows us to focus on coding and testing individually so that there are not many "moving pieces" that we need to worry about at the start.
However that is Not the Recomended way to develop, as we might:
- Write redundant code that is not actually required
- Not fully test all the behaviours of the app
Now that we have mastered the syntax for React and React testing, we would like to modify our workflow to use Test Driven Development (TDD), which is a development best practice and discipline we should follow.
In TDD, as its name implies, we first start by writing tests and only when it fails, then we are allowed to switch to the component code to develop. Once the test passes, we must switch back to writing more tests before we can write more component code.
In this way, TDD ensures that only essential code is written, and it would also be impossible to have a feature that does not have a corresponding test.
In the TDD process we would :
- Run all tests in watch mode, and ensure that all are passing (Green) before we begin
- Create test case for the component we wish to build
- Render the component in the test, and watch the test fail (Red)
- Go ahead to create the component file and watch the test pass (Green)
- Switch back to the test and create an expect/assertion and watch the test fail
- Develop the simplest code you can do to make the test pass
- Refactor the code if it is not clean (Refactor)
- Add a new test with another expect/assertion and watch the test fail
- Develop the simplest code you can do to make the test pass again
- Keep repeating the cycle till the component is fully tested and developed
1. In the Labs we run only the test which we are developing, while in TDD we run all tests. Why is that so?
Ans: In the first workflow we run all the tests when we are developing, but once the development phase is over, we no longer touch the App code. Hence we can choose to run only the test we are developing, so that there is less noise on the console
In TDD we are developing all the time. We need to run all tests as a safety net to help us detect when we have broken something.
Ans: Red - Green - Refactor
is describing the process of writing a test and letting it fail (Red), writing the code to pass the test (Green), and looking at the code from the previous cycle and the current cycle, and cleaning up if that is required (Refactor)
Ans: Since TDD consists of many cycles of code change, rather than a upfront design of what you wish to code. As the code from the cycles accumulate, reduandant code could build up. eg: Code repetition, bad naming of variables or functions etc. If this is not cleaned up, it could become a tech debt
, and make the code hard to maintain in the future.
Hence the Refactor
steps means to clean the code so that it has the same behaviour as in the Green
stage but is shorter and more readable, and has no repetition.
This repo contains the starter project for building a comments list by Test Driven Development (TDD).
We will follow the steps in the blog post Creating Readable Tests Using React Testing to build a comment app with TDD
- Fork this repository to your github account and clone it to your computer
- https://github.com/thoughtworks-jumpstart/comment-app
- The
master
branch contains the final solution - You should checkout to the
lab
branch do annpm install
before starting this lab
The sample repo is created using create-react-app
The initial libraries installed besides react, are
For testing:
-
react-testing-library
-
jest-dom
Note: We do not need to install
jest
as it is already installed bycreate-react-app
For development:
- props-types (this an optional library for ensuring the correct type of props is passed to a component)
For styling:
- bootstrap (and imported in index.js)
- How to arrange the code in your test case: Unit Testing and the Arrange, Act and Assert (AAA) Pattern
- An interesting article explaining how to extract the truthy/falsey value in JS JavaScript “Bang, Bang. I shot you down” - Use of double bangs (!!) in JavaScript.
- This is another excellent tutorial you should checkout.
- If you would like to see how Kent (the author of react-testing-library) does testing, you can checkout his online course