You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: ReduxToolKit/README.md
+249-1
Original file line number
Diff line number
Diff line change
@@ -165,4 +165,252 @@ For troubleshooting the code, please use docs [official Redux Toolkit documentat
165
165
166
166
- Contact me on discord: [Kkermit.](https://discord.com/users/526853643962679323) or create a discussion on main repository found [here](https://github.com/Kkkermit/ReduxToolKit/discussions)
167
167
168
-
### If you are a fan of the repo and want to support it, please feel free to leave a star :)
168
+
### If you are a fan of the repo and want to support it, please feel free to leave a star :)
`Vitest` is a next generation testing framework powered by Vite
175
+
176
+
You can learn more about the rationale behind the project in the [Why Vitest](https://vitest.dev/guide/why.html) section.
177
+
178
+
# Requirements -
179
+
180
+
`Vitest` 1.0 requires `Vite >=v5.0.0` and `Node >=v18.00`
181
+
182
+
# Setting up unit tests -
183
+
184
+
- Install `vitest` either as a normal dependency or as a dev dependency. We recommend using a dev dependency as this is what is encouraged by `vitest`.
185
+
186
+
In terminal use command
187
+
```bash
188
+
npm install -D vitest
189
+
or
190
+
yarn add -D vitest
191
+
```
192
+
- It is recommended that you install a copy of `vitest` in your `package.json`, using one of the methods listed above. However, if you would prefer to run `vitest` directly, you can use `npx vitest`(the `npx` command comes with npm and Node.js).
193
+
194
+
- The `npx` command will execute the command either from a local `node_modules/.bin` installing any packages needed in order for the command to run. By default, npx will check whether command exists in $PATH, or in the local project binaries, and execute that. If command is not found, it will be installed prior to execution.
195
+
196
+
## Writing tests -
197
+
198
+
**Example:**
199
+
200
+
```js
201
+
// sum.js
202
+
exportfunctionsum(a, b) {
203
+
return a + b
204
+
}
205
+
```
206
+
207
+
```js
208
+
// sum.test.js
209
+
import { expect, test } from'vitest'
210
+
import { sum } from'./sum'
211
+
212
+
test('adds 1 + 2 to equal 3', () => {
213
+
expect(sum(1, 2)).toBe(3)
214
+
})
215
+
```
216
+
217
+
- By default, tests must contain ".test." or ".spec." in their file name.
218
+
219
+
## Executing tests -
220
+
221
+
- You will need to go into `package.json` and update the following:
222
+
223
+
```json
224
+
{
225
+
"scripts": {
226
+
"test": "vitest"
227
+
}
228
+
}
229
+
```
230
+
231
+
- This allows the tests to be ran as a script has been initiated in the package.json
232
+
233
+
- Finally, run **`npm run test`**, **`yarn test`**, or **`pnpm test`**, depending on your package manager, and `Vitest` will print this message:
- API docs for more usage - [DOCS](https://vitest.dev/api/)
246
+
247
+
# configuring Vitest -
248
+
249
+
One of the main advantages of Vitest is its unified configuration with Vite. If present, `vitest` will read your root `vite.config.ts` to match with the plugins and setup as your Vite app. For example, your Vite [resolve.alias](https://vitejs.dev/config/shared-options.html#resolve-alias) and [plugins](https://vitejs.dev/guide/using-plugins.html) configuration will work out-of-the-box. If you want a different configuration during testing, you can:
250
+
251
+
- Create `vitest.config.ts`, which will have the higher priority
252
+
253
+
- Pass `--config option` to CLI, e.g. `vitest --config ./path/to/vitest.config.ts`
254
+
255
+
- Use `process.env.VITEST` or `mode` property on `defineConfig` (will be set to `test` if not overridden) to conditionally apply different configuration in `vite.config.ts`
256
+
257
+
Vitest supports the same extensions for your configuration file as Vite does: **`.js, .mjs, .cjs, .ts, .cts, .mts. Vitest does not support .json extension.`**
258
+
259
+
If you are not using Vite as your build tool, you can configure Vitest using the test property in your config file:
260
+
261
+
```ts
262
+
import { defineConfig } from'vitest/config'
263
+
264
+
exportdefaultdefineConfig({
265
+
test: {
266
+
// ...
267
+
},
268
+
})
269
+
```
270
+
271
+
If you are already using Vite, add `test` property in your Vite config. You'll also need to add a reference to Vitest types using a [triple slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) at the top of your config file.
272
+
273
+
```ts
274
+
/// <referencetypes="vitest" />
275
+
import { defineConfig } from'vite'
276
+
277
+
exportdefaultdefineConfig({
278
+
test: {
279
+
// ...
280
+
},
281
+
})
282
+
```
283
+
284
+
# Workspace support -
285
+
286
+
Run different project configurations inside the same project with [Vitest Workspaces](https://vitest.dev/guide/workspace.html). You can define a list of files and folders that define your workspace in **`vitest.workspace`** file. The file supports `js`/`ts`/`json` extensions. This feature works great with monorepo setups.
287
+
288
+
```ts
289
+
import { defineWorkspace } from'vitest/config'
290
+
291
+
exportdefaultdefineWorkspace([
292
+
// you can use a list of glob patterns to define your workspaces
293
+
// Vitest expects a list of config files
294
+
// or directories where there is a config file
295
+
'packages/*',
296
+
'tests/*/vitest.config.{e2e,unit}.ts',
297
+
// you can even run the same tests,
298
+
// but with different configs in the same "vitest" process
299
+
{
300
+
test: {
301
+
name: 'happy-dom',
302
+
root: './shared_tests',
303
+
environment: 'happy-dom',
304
+
setupFiles: ['./setup.happy-dom.ts'],
305
+
},
306
+
},
307
+
{
308
+
test: {
309
+
name: 'node',
310
+
root: './shared_tests',
311
+
environment: 'node',
312
+
setupFiles: ['./setup.node.ts'],
313
+
},
314
+
},
315
+
])
316
+
```
317
+
318
+
# Command line interface -
319
+
320
+
In a project where Vitest is installed, you can use the `vitest` binary in your npm scripts, or run it directly with `npx vitest`. Here are the default npm scripts in a scaffolded Vitest project:
321
+
322
+
```json
323
+
{
324
+
"scripts": {
325
+
"test": "vitest",
326
+
"coverage": "vitest run --coverage"
327
+
}
328
+
}
329
+
```
330
+
331
+
- To run tests once without watching for file changes, use `vitest run`. You can specify additional CLI options like `--port` or `--https`. For a full list of CLI options, run `npx vitest --help` in your project.
332
+
333
+
- More on [command line interface](https://vitest.dev/guide/cli.html)
334
+
335
+
336
+
# Vitest with React Testing Library
337
+
338
+
# Installation -
339
+
340
+
1. Run command in terminal:
341
+
342
+
```bash
343
+
npm install jsdom --save-dev
344
+
```
345
+
346
+
2. Include it to the Vite configuration file `vite.config.ts`:
347
+
348
+
```ts
349
+
import { defineConfig } from'vite';
350
+
importreactfrom'@vitejs/plugin-react';
351
+
352
+
// https://vitejs.dev/config/
353
+
exportdefaultdefineConfig({
354
+
plugins: [react()],
355
+
test: {
356
+
+ environment: 'jsdom',
357
+
},
358
+
});
359
+
```
360
+
361
+
3. Install React Testing Library on the command line:
5. include this new test setup file in Vite's configuration file `vite.config.ts`. In addition, make all imports from **Vitest global**, so that you don't need to perform these imports (e.g. expect) in each file manually anymore:
382
+
383
+
```ts
384
+
import { defineConfig } from'vite';
385
+
importreactfrom'@vitejs/plugin-react';
386
+
387
+
// https://vitejs.dev/config/
388
+
exportdefaultdefineConfig({
389
+
plugins: [react()],
390
+
test: {
391
+
globals: true,
392
+
environment: 'jsdom',
393
+
setupFiles: './tests/setup.js',
394
+
},
395
+
});
396
+
```
397
+
398
+
- And thats it for the installation; Examples of usage:
0 commit comments