- Mastering React Test:Driven Development
- Daniel Irvine
- 405字
- 2021-06-24 14:45:02
Writing a failing test
What exactly is a test? We'll discover that by writing one. In your project directory, type the following commands:
mkdir test
touch test/Appointment.test.js
Open the test/Appointment.test.js file in your favorite editor or IDE and enter the following:
describe('Appointment', () => {
});
The describe function defines a test suite, which is simply a set of tests with a given name. The first argument is the name (or description) of the unit you are testing. It could be a React component, a function, or a module. The second argument is a function inside of which you define your tests.
For React components, it's good practice to give your describe blocks the same name as the component itself.
You should run this code right now in the Jest test runner. It will give us valuable information about what to do next. You might think that running tests now is pointless, since we haven't even written a test yet, but with TDD, it's normal to run your test runner at every opportunity.
On the command line, run the npm test command:
> appointments@1.0.0 test /home/daniel/work/react-tdd/ch1
> jest
FAIL test/Appointment.test.js
● Test suite failed to run
Your test suite must contain at least one test.
at node_modules/jest/node_modules/jest-cli/build/TestScheduler.js:225:24
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.917s
Ran all test suites.
npm ERR! Test failed. See above for more details.
You can see Jest helpfully tells us Your test suite must contain at least one test. Test-driven developers rely heavily on listening to the test runner and what it tells us. It usually tells them exactly what to do next. In this case, it's telling us to create a test. So, let's do that.
If you do try out the create-react-app template, you’ll notice that it contains a single unit test file, App.test.js , which exists in the same directory as the source file, App.js .
I don't recommend mixing production code with test code. For a start, it isn’t the conventional unit-testing approach, which uses two separate directories for production code and test code. More importantly, however, it’s likely that you won’t have a one-to-one mapping between production and test files.
- Python程序設(shè)計(jì)教程(第2版)
- Drupal 8 Blueprints
- Oracle從新手到高手
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計(jì)
- Apache Spark Graph Processing
- Java面向?qū)ο蟪绦蜷_發(fā)及實(shí)戰(zhàn)
- Hands-On GPU:Accelerated Computer Vision with OpenCV and CUDA
- Building RESTful Python Web Services
- 案例式C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)
- 快速入門與進(jìn)階:Creo 4·0全實(shí)例精講
- C語言程序設(shè)計(jì)與應(yīng)用(第2版)
- PHP+MySQL動(dòng)態(tài)網(wǎng)站開發(fā)從入門到精通(視頻教學(xué)版)
- OpenCV Android Programming By Example
- Julia High Performance(Second Edition)
- Python 快速入門(第3版)