Skip to content

Commit e02bddc

Browse files
svelte: update docs (#1298)
* svelte: update docs * install @vitest/ui * address feedback
1 parent 104b1e0 commit e02bddc

File tree

4 files changed

+85
-73
lines changed

4 files changed

+85
-73
lines changed

docs/svelte-testing-library/example.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ sidebar_label: Example
2525
## Test
2626

2727
```js
28-
// NOTE: jest-dom adds handy assertions to Jest and it is recommended, but not required.
28+
// NOTE: jest-dom adds handy assertions to Jest (and Vitest) and it is recommended, but not required.
2929
import '@testing-library/jest-dom'
3030

3131
import {render, fireEvent, screen} from '@testing-library/svelte'
3232

3333
import Comp from '../Comp'
3434

3535
test('shows proper heading when rendered', () => {
36-
render(Comp, { name: 'World' })
37-
const heading = screen.getByText("Hello World!");
38-
expect(heading).toBeInTheDocument();
36+
render(Comp, {name: 'World'})
37+
const heading = screen.getByText('Hello World!')
38+
expect(heading).toBeInTheDocument()
3939
})
4040

4141
// Note: This is as an async test as we are using `fireEvent`
4242
test('changes button text on click', async () => {
43-
render(Comp, { name: 'World' })
44-
const button = screen.getByRole("button")
43+
render(Comp, {name: 'World'})
44+
const button = screen.getByRole('button')
4545

4646
// Using await when firing events is unique to the svelte testing library because
4747
// we have to wait for the next `tick` so that Svelte flushes all pending state changes.

docs/svelte-testing-library/faq.mdx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ title: FAQ
44
sidebar_label: FAQ
55
---
66

7+
- [Does this library also work with SvelteKit?](#does-this-library-also-work-with-sveltekit)
78
- [Testing file upload component](#testing-file-upload-component)
89

910
---
1011

11-
## Testing file upload component
12+
## Does this library also work with SvelteKit?
1213

13-
File upload handler not triggering? Use `happy-dom`, not `jsdom`, and make
14-
sure to use `fireEvent.change(...)` and not `fireEvent.input(...)`. It seems
15-
that jsdom (which is at v22.1.0 at the time of this writing) doesn't fake all
16-
the File object as it should.
14+
Yes, it does. It requires the same [setup](setup.mdx) as a "normal" Svelte
15+
project.
1716

17+
## Testing file upload component
1818

19-
See [svelte-testing-library's issue
20-
219](https://github.com/testing-library/svelte-testing-library/issues/219) for
21-
more details.
19+
File upload handler not triggering? Use `happy-dom`, not `jsdom`, and make sure
20+
to use `fireEvent.change(...)` and not `fireEvent.input(...)`. It seems that
21+
jsdom (which is at v22.1.0 at the time of this writing) doesn't fake all the
22+
File object as it should.
2223

24+
See
25+
[svelte-testing-library's issue 219](https://github.com/testing-library/svelte-testing-library/issues/219)
26+
for more details.

docs/svelte-testing-library/intro.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,3 @@ Introduction][dom-solution-explainer] for a more in-depth explanation.
4040

4141
1. A test runner or framework.
4242
2. Specific to a testing framework.
43-
44-
We recommend Vitest as our preference.

docs/svelte-testing-library/setup.mdx

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,95 @@ title: Setup
44
sidebar_label: Setup
55
---
66

7-
We recommend using [Vitest](https://vitest.dev/) but you're free to use the library
8-
with any testing framework and runner you're comfortable with.
7+
We recommend using [Vitest](https://vitest.dev/) but you're free to use the
8+
library with any testing framework and runner you're comfortable with.
99

1010
## Vitest
11-
1. Install Vitest and jsdom
1211

13-
```
14-
npm install --save-dev vitest jsdom
15-
```
12+
1. Install Vitest and jsdom
1613

17-
2. Add the following to your `package.json`
14+
We're using `jdom` here as the test environment, but you can use any other
15+
options e.g `happy-dom`.
1816

19-
```json
20-
{
21-
"scripts": {
22-
"test": "vitest run src",
23-
"test:watch": "vitest src"
24-
}
25-
}
26-
```
17+
```bash npm2yarn
18+
npm install --save-dev vitest jsdom
19+
```
2720

28-
3. You'll need to compile the Svelte components before using them in Vitest, so
29-
you need to install
30-
[@sveltejs/vite-plugin-svelte](https://github.com/sveltejs/vite-plugin-svelte) and Vite
31-
21+
Optionally install `@vitest/ui`, which opens a UI within a browser window to
22+
follow the progress and interact with your tests.
3223

33-
```
34-
npm install --save-dev @sveltejs/vite-plugin-svelte vite
35-
```
24+
```bash npm2yarn
25+
npm install --save-dev @vitest/ui
26+
```
3627

37-
4. Add a `vitest.config.ts` configuration file to the root of your project
28+
1. Add the test scipts to your `package.json` to run the tests with Vitest
3829

39-
```js
40-
import { defineConfig } from 'vitest/config';
41-
import { svelte } from '@sveltejs/vite-plugin-svelte';
30+
```json
31+
{
32+
"scripts": {
33+
"test": "vitest run",
34+
"test:ui": "vitest --ui",
35+
"test:watch": "vitest"
36+
}
37+
}
38+
```
4239

43-
export default defineConfig({
44-
plugins: [svelte({ hot: !process.env.VITEST })],
45-
test: {
46-
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
47-
globals: true,
48-
environment: 'jsdom'
49-
}
50-
});
40+
2. To compile the Svelte components before using them in Vitest, you need to
41+
install
42+
[@sveltejs/vite-plugin-svelte](https://github.com/sveltejs/vite-plugin-svelte)
43+
and Vite
5144

52-
```
45+
```bash npm2yarn
46+
npm install --save-dev @sveltejs/vite-plugin-svelte vite
47+
```
5348

54-
5. This is optional but it is recommended, you can install
55-
[jest-dom](https://github.com/testing-library/jest-dom) to add handy
56-
assertions to Jest
49+
3. Add a `vitest.config.ts` configuration file to the root of your project
5750

58-
5.1 Install `jest-dom`
51+
```js
52+
import {defineConfig} from 'vite'
53+
import {svelte} from '@sveltejs/vite-plugin-svelte'
5954

60-
```
61-
npm install --save-dev @testing-library/jest-dom
62-
```
55+
export default defineConfig({
56+
plugins: [svelte({hot: !process.env.VITEST})],
57+
test: {
58+
globals: true,
59+
environment: 'jsdom',
60+
},
61+
})
62+
```
6363

64-
5.2 import `@testing-library/jest-dom` at the start of your test files
64+
4. Optionally install [vitest-dom](https://github.com/chaance/vitest-dom) to add
65+
handy assertions to Vitest
6566

66-
```js
67-
import '@testing-library/jest-dom';
68-
```
67+
5.1 Install `vitest-dom`
6968

70-
6. Create your component and a test file (checkout the rest of the docs to see how)
71-
and run the following command to run the tests.
69+
```bash npm2yarn
70+
npm install --save-dev vitest-dom
71+
```
7272

73-
```
74-
npm run test
75-
```
76-
### SvelteKit
73+
5.2 import `vitest-dom` at within the vitest setup file (usually `vitest-setup.(js|ts)`)
74+
75+
```js
76+
import * as matchers from "vitest-dom/matchers";
77+
import { expect } from "vitest";
78+
expect.extend(matchers);
79+
80+
// or:
81+
import "vitest-dom/extend-expect";
82+
```
7783

78-
To use Vitest with SvelteKit install `vitest-svelte-kit`, which includes a preconfigured Vitest configuration for SvelteKit projects.
79-
You can take a look at the [`vitest-svelte-kit` configuration docs](https://github.com/nickbreaton/vitest-svelte-kit/tree/master/packages/vitest-svelte-kit#configuring) for further instructions.
84+
5. Create your component and a test file (checkout the rest of the docs to see
85+
how) and run the following command to run the tests.
86+
87+
```bash npm2yarn
88+
npm run test
89+
```
8090

8191
## Jest
8292

8393
1. Install Jest & jest-environment-jsdom
8494

85-
```
95+
```bash npm2yarn
8696
npm install --save-dev jest jest-environment-jsdom
8797
```
8898

@@ -173,8 +183,8 @@ You can take a look at the [`vitest-svelte-kit` configuration docs](https://gith
173183

174184
### TypeScript
175185

176-
To use TypeScript with Jest, you'll need to install and configure `svelte-preprocess` and
177-
`ts-jest`. For full instructions, see the
186+
To use TypeScript with Jest, you'll need to install and configure
187+
`svelte-preprocess` and `ts-jest`. For full instructions, see the
178188
[`svelte-jester`](https://github.com/mihar-22/svelte-jester#typescript) docs.
179189

180190
### Preprocessors

0 commit comments

Comments
 (0)