- Mastering React Test:Driven Development
- Daniel Irvine
- 667字
- 2021-06-24 14:45:02
Writing your first expectation
Change your describe call to this:
describe('Appointment', () => {
it('renders the customer first name', () => {
});
});
The it function defines a single test. The first argument is the description of the test and always starts with a present-tense verb, so that it reads in plain English. The it in the function name refers to the noun you used to name your test suite (in this case, Appointment). In fact, if you run tests now, with npm test, remember, it should make sense:
PASS test/Appointment.test.js
Appointment
? renders the customer first name (1ms)
You can read the describe and it descriptions together as one sentence: Appointment renders the customer first name. You should aim for all of your tests to be readable in this way.
As we add more tests, Jest will show us a little checklist of passing tests.
Empty tests, such as the one we just wrote, always pass. Let's change that now. Let's add an expectation to our test. Change test to read as follows:
it('renders the customer first name', () => {
expect(document.body.textContent).toMatch('Ashley');
});
This expect call is an example of a fluent API. Like the test description, it reads like plain English. You can read it like this: I expect document.body.textContent toMatch the string Ashley.
Although it might look complicated, it's quite a simple idea: each expectation has an expected value that is compared against a received value. In this example, the expected value is Ashley and the received value is whatever is stored in document.body.textContent.
The toMatch function is called a matcher and there are a whole lot of different matchers that work in different ways. In this case, the expectation passes if document.body.textContent has the word Ashley anywhere within it.
Each individual test can have as many expectations in it as you like, and we'll see examples of multiple expectations in a test later in this chapter.
Before we run this test, spend a minute thinking about the code. You might have guessed that the test will fail. The question is, how will it fail?
Let's run test now, with npm test, and find out:
FAIL test/Appointment.test.js
Appointment
? renders the customer first name (10ms)
● Appointment ? renders the customer first name
expect(received).toMatch(expected)
Expected value to match:
"Ashley"
Received:
""
1 | describe('Appointment', () => {
2 | it('renders the customer first name', () => {
> 3 | expect(document.body.textContent).toMatch('Ashley');
| ^
4 | });
5 | });
6 |
at Object.toMatch (test/Appointment.test.js:3:39)
There are four parts to the test output that are relevant to us:
- The name of the failing test
- The expected answer
- The actual answer
- The location in the source where the error occurred
All of these help us to pinpoint where our tests failed: document.body.textContent is empty. This isn't surprising really, since we've not done anything to set the body text.
But, hold on a second. Where did document.body come from? No one defined that yet. Shouldn’t we expect the test to fail with an error saying that the document is undefined?
Jest magically includes a DOM implementation for us, which is why we have access to document and document.body. It uses jsdom, a headless implementation of the DOM. We can do test browser interactions on the command line, which is much simpler than involving a browser in our work.
In Jest lingo, this is called the Jest environment and it defaults to jsdom. If you want to verify that this is happening, add the following config to your package.json file:
"jest": {
"testEnvironment": "node"
}
Re-run tests and observe the different output to convince yourself that JSDOM is no longer present.
Be sure to remove this extra configuration before you continue, as we’ll be relying on the JSDOM environment from now on.
- Mastering NetBeans
- ADI DSP應(yīng)用技術(shù)集錦
- TradeStation交易應(yīng)用實(shí)踐:量化方法構(gòu)建贏家策略(原書第2版)
- IPython Interactive Computing and Visualization Cookbook
- 數(shù)據(jù)結(jié)構(gòu)與算法詳解
- 流暢的Python
- Learning Scrapy
- Learning Java by Building Android Games
- Java項(xiàng)目驅(qū)動(dòng)開發(fā)教程
- Unity Certified Programmer:Exam Guide
- Python語言及其應(yīng)用(第2版)
- RabbitMQ Essentials
- Python深度學(xué)習(xí)實(shí)戰(zhàn):基于TensorFlow和Keras的聊天機(jī)器人以及人臉、物體和語音識(shí)別
- Python Machine Learning
- 微信小程序開發(fā)零基礎(chǔ)入門