Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
minor tweaks
  • Loading branch information
Kent C. Dodds authored Mar 21, 2018
commit 52bc0eb72d20fce1927fbe2754484de284e8a89f
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ unmount()

#### `getByTestId`

A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` except that it will throw an Error if no matching element is found. Use this instead of `queryByTestId` if you don't want to handle whether the return value could be null. Read more about `data-testid`s below.
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` except
that it will throw an Error if no matching element is found. Read more about
`data-testid`s below.

```javascript
const usernameInputElement = getByTestId('username-input')
Expand All @@ -158,12 +160,15 @@ Simulate.change(usernameInputElement)

#### `queryByTestId`

A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) ``. This could return null if no matching element is found. Read
more about `data-testid`s below.
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) ``
(Note: just like `querySelector`, this could return null if no matching element
is found, which may lead to harder-to-understand error messages). Read more about
`data-testid`s below.

```javascript
const hiddenItemElement = queryByTestId('item-hidden')
expect(hiddenItemElement).toBeFalsy() // we just care it doesn't exist
// assert something doesn't exist
// (you couldn't do this with `getByTestId`)
expect(queryByTestId('username-input')).toBeNull()
```

## More on `data-testid`s
Expand Down Expand Up @@ -239,7 +244,7 @@ test('you can mock things with jest.mock', () => {
// in the real world, the CSSTransition component would take some time
// before finishing the animation which would actually hide the message.
// So we've mocked it out for our tests to make it happen instantly
expect(queryByTestId('hidden-message')).toBeFalsy() // we just care it doesn't exist
expect(queryByTestId('hidden-message')).toBeNull() // we just care it doesn't exist
})
```

Expand All @@ -264,6 +269,10 @@ Learn more about how Jest mocks work from my blog post:

You typically will get access to rendered elements using the `getByTestId` utility. However, that function will throw an error if the element isn't found. If you want to specifically test for the absence of an element, then you should use the `queryByTestId` utility which will return the element if found or `null` if not.

```javascript
expect(queryByTestId('thing-that-does-not-exist')).toBeNull()
```

**I don't want to use `data-testid` attributes for everything. Do I have to?**

Definitely not. That said, a common reason people don't like the `data-testid`
Expand Down