Skip to content

Commit e888514

Browse files
committed
chore: apply review suggestions
1 parent 6d790ba commit e888514

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

docs/qwik-testing-library/api.mdx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ sidebar_label: API
1414

1515
## `render`
1616

17-
Render your component to the DOM with Qwik. By default, the component will be
18-
rendered into a `<host>` appended to `document.body`.
17+
Render your component to the DOM with Qwik. By default, when no options are
18+
provided, the component will be rendered into a `<host>` appended to
19+
`document.body`.
1920

2021
```tsx
2122
import {render} from '@noma.to/qwik-testing-library'
23+
import {MockProvider} from './MockProvider'
2224
import {MyComponent} from './MyComponent'
2325

24-
const result = await render(<MyComponent />, renderOptions)
26+
const result = await render(<MyComponent />, {
27+
baseElement: document.body,
28+
container: document.createElement('host'),
29+
wrapper: MockProvider,
30+
})
2531
```
2632

2733
### Render Options

docs/qwik-testing-library/example.mdx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ describe('<Counter />', () => {
3535
// retrieve the 'increment count' button
3636
const incrementBtn = screen.getByRole('button', {name: /increment count/})
3737
// click the button twice
38-
await userEvent.click(incrementBtn)
39-
await userEvent.click(incrementBtn)
38+
const user = userEvent.setup()
39+
await user.click(incrementBtn)
40+
await user.click(incrementBtn)
4041

4142
// assert that the counter is now 2
42-
await waitFor(() => expect(screen.getByText(/count:2/)).toBeInTheDocument())
43+
expect(await screen.findByText(/count:2/)).toBeInTheDocument()
4344
})
4445
})
4546
```
@@ -55,6 +56,8 @@ We're happy to discuss it on [Discord][discord], but we consider this failure to
5556
be a good thing: your components should be tested in isolation, so you will be
5657
forced to mock your server functions.
5758

59+
[discord]: https://qwik.dev/chat
60+
5861
Here is an example of how to test a component that uses `server$` calls:
5962

6063
```ts title="~/server/blog-post.ts"
@@ -85,9 +88,7 @@ describe('<LatestPostList />', () => {
8588
it('should render the latest posts', async () => {
8689
await render(<LatestPostList />)
8790

88-
await waitFor(() =>
89-
expect(screen.queryAllByRole('listitem')).toHaveLength(2),
90-
)
91+
expect(await screen.findAllByRole('listitem')).toHaveLength(2)
9192
})
9293
})
9394
```
@@ -97,5 +98,5 @@ being named as `getLatestPosts$`. This is caused by the Qwik optimizer renaming
9798
it to `Qrl`. So, we need to mock the `Qrl` function instead of the original `$`
9899
one.
99100

100-
If your `server$` function doesn't end with `$`, the Qwik optimizer might not
101-
rename it to `Qrl`.
101+
If your function doesn't end with `$`, the Qwik optimizer will not rename it to
102+
`Qrl`.

docs/qwik-testing-library/setup.mdx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ This module is distributed via [npm][npm] which is bundled with [node][node] and
2020
should be installed as one of your project's `devDependencies`:
2121

2222
```bash npm2yarn
23-
npm install --save-dev @noma.to/qwik-testing-library
23+
npm install --save-dev @noma.to/qwik-testing-library @testing-library/dom
2424
```
2525

26-
This library supports `qwik` versions `1.7.2` and above.
26+
This library supports `qwik` versions `1.7.2` and above and
27+
`@testing-library/dom` versions `10.1.0` and above.
2728

2829
You may also be interested in installing `@testing-library/jest-dom` and
2930
`@testing-library/user-event` so you can use [the custom jest
@@ -48,8 +49,8 @@ npm install --save-dev jsdom
4849

4950
## Vitest Configuration
5051

51-
We recommend using `@noma.to/qwik-testing-library` with [Vitest][] as your test
52-
runner.
52+
We recommend using `@noma.to/qwik-testing-library` with [Vitest][vitest] as your
53+
test runner.
5354

5455
If you haven't done so already, add vitest to your project using Qwik CLI:
5556

@@ -108,26 +109,31 @@ create it:
108109
Then, create the `vitest.setup.ts` file:
109110

110111
```ts title="vitest.setup.ts"
111-
import {afterEach} from 'vitest'
112+
// Configure DOM matchers to work in Vitest
112113
import '@testing-library/jest-dom/vitest'
113114

114115
// This has to run before qdev.ts loads. `beforeAll` is too late
115116
globalThis.qTest = false // Forces Qwik to run as if it was in a Browser
116117
globalThis.qRuntimeQrl = true
117118
globalThis.qDev = true
118119
globalThis.qInspector = false
119-
120-
afterEach(async () => {
121-
const {cleanup} = await import('@noma.to/qwik-testing-library')
122-
cleanup()
123-
})
124120
```
125121

126-
This setup will make sure that Qwik is properly configured and that everything
127-
gets cleaned after each test.
122+
This setup will make sure that Qwik is properly configured. It also loads
123+
`@testing-library/jest-dom/vitest` in your test runner so you can use matchers
124+
like `expect(...).toBeInTheDocument()`.
125+
126+
By default, Qwik Testing Library cleans everything up automatically for you. You
127+
can opt out of this by setting the environment variable `QTL_SKIP_AUTO_CLEANUP`
128+
to `true`. Then in your tests, you can call the `cleanup` function when needed.
129+
For example:
128130

129-
Additionally, it loads `@testing-library/jest-dom/vitest` in your test runner so
130-
you can use matchers like `expect(...).toBeInTheDocument()`.
131+
```ts
132+
import {cleanup} from '@noma.to/qwik-testing-library'
133+
import {afterEach} from 'vitest'
134+
135+
afterEach(cleanup)
136+
```
131137

132138
Now, edit your `tsconfig.json` to declare the following global types:
133139

0 commit comments

Comments
 (0)