Skip to content

Commit 85e0df7

Browse files
committed
Merge branch 'main' into fix-restrict-file-for-screenshot-error
2 parents b6ceccb + 45085cf commit 85e0df7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+668
-581
lines changed

docs/guide/features.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ Run tests on different machines using [`--shard`](/guide/cli#shard) and [`--repo
237237
All test and coverage results can be merged at the end of your CI pipeline using `--merge-reports` command:
238238

239239
```bash
240-
vitest --shard=1/2 --reporter=blob
241-
vitest --shard=2/2 --reporter=blob
242-
vitest --merge-reports --reporter=junit --coverage.reporter=text
240+
vitest --shard=1/2 --reporter=blob --coverage
241+
vitest --shard=2/2 --reporter=blob --coverage
242+
vitest --merge-reports --reporter=junit --coverage
243243
```
244244

245245
See [`Improving Performance | Sharding`](/guide/improving-performance#sharding) for more information.

docs/guide/mocking.md

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,20 @@ it('can return a value multiple times', () => {
430430

431431
## Requests
432432

433-
Because Vitest runs in Node, mocking network requests is tricky; web APIs are not available, so we need something that will mimic network behavior for us. We recommend [Mock Service Worker](https://mswjs.io/) to accomplish this. It will let you mock both `REST` and `GraphQL` network requests, and is framework agnostic.
433+
Because Vitest runs in Node, mocking network requests is tricky; web APIs are not available, so we need something that will mimic network behavior for us. We recommend [Mock Service Worker](https://mswjs.io/) to accomplish this. It allows you to mock `http`, `WebSocket` and `GraphQL` network requests, and is framework agnostic.
434434

435435
Mock Service Worker (MSW) works by intercepting the requests your tests make, allowing you to use it without changing any of your application code. In-browser, this uses the [Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API). In Node.js, and for Vitest, it uses the [`@mswjs/interceptors`](https://github.com/mswjs/interceptors) library. To learn more about MSW, read their [introduction](https://mswjs.io/docs/)
436436

437437
### Configuration
438438

439439
You can use it like below in your [setup file](/config/#setupfiles)
440-
```js
440+
441+
::: code-group
442+
443+
```js [HTTP Setup]
441444
import { afterAll, afterEach, beforeAll } from 'vitest'
442445
import { setupServer } from 'msw/node'
443-
import { graphql, http, HttpResponse } from 'msw'
446+
import { http, HttpResponse } from 'msw'
444447

445448
const posts = [
446449
{
@@ -458,28 +461,83 @@ export const restHandlers = [
458461
}),
459462
]
460463

464+
const server = setupServer(...restHandlers)
465+
466+
// Start server before all tests
467+
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
468+
469+
// Close server after all tests
470+
afterAll(() => server.close())
471+
472+
// Reset handlers after each test for test isolation
473+
afterEach(() => server.resetHandlers())
474+
```
475+
476+
```js [GrapQL Setup]
477+
import { afterAll, afterEach, beforeAll } from 'vitest'
478+
import { setupServer } from 'msw/node'
479+
import { graphql, HttpResponse } from 'msw'
480+
481+
const posts = [
482+
{
483+
userId: 1,
484+
id: 1,
485+
title: 'first post title',
486+
body: 'first post body',
487+
},
488+
// ...
489+
]
490+
461491
const graphqlHandlers = [
462492
graphql.query('ListPosts', () => {
463-
return HttpResponse.json(
464-
{
465-
data: { posts },
466-
},
467-
)
493+
return HttpResponse.json({
494+
data: { posts },
495+
})
468496
}),
469497
]
470498

471-
const server = setupServer(...restHandlers, ...graphqlHandlers)
499+
const server = setupServer(...graphqlHandlers)
472500

473501
// Start server before all tests
474502
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
475503

476-
// Close server after all tests
504+
// Close server after all tests
477505
afterAll(() => server.close())
478506

479-
// Reset handlers after each test `important for test isolation`
507+
// Reset handlers after each test for test isolation
480508
afterEach(() => server.resetHandlers())
481509
```
482510

511+
```js [WebSocket Setup]
512+
import { afterAll, afterEach, beforeAll } from 'vitest'
513+
import { setupServer } from 'msw/node'
514+
import { ws } from 'msw'
515+
516+
const chat = ws.link('wss://chat.example.com')
517+
518+
const wsHandlers = [
519+
chat.addEventListener('connection', ({ client }) => {
520+
client.addEventListener('message', (event) => {
521+
console.log('Received message from client:', event.data)
522+
// Echo the received message back to the client
523+
client.send(`Server received: ${event.data}`)
524+
})
525+
}),
526+
]
527+
528+
const server = setupServer(...wsHandlers)
529+
530+
// Start server before all tests
531+
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
532+
533+
// Close server after all tests
534+
afterAll(() => server.close())
535+
536+
// Reset handlers after each test for test isolation
537+
afterEach(() => server.resetHandlers())
538+
```
539+
:::
540+
483541
> Configuring the server with `onUnhandledRequest: 'error'` ensures that an error is thrown whenever there is a request that does not have a corresponding request handler.
484542
485543
### More

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"test:browser:playwright": "pnpm -C test/browser run test:playwright"
3737
},
3838
"devDependencies": {
39-
"@antfu/eslint-config": "^3.11.2",
39+
"@antfu/eslint-config": "^3.16.0",
4040
"@antfu/ni": "^23.2.0",
4141
"@playwright/test": "^1.49.1",
4242
"@rollup/plugin-commonjs": "^28.0.2",
@@ -51,7 +51,7 @@
5151
"bumpp": "^9.10.1",
5252
"changelogithub": "^0.13.11",
5353
"esbuild": "^0.24.2",
54-
"eslint": "^9.16.0",
54+
"eslint": "^9.18.0",
5555
"magic-string": "^0.30.17",
5656
"pathe": "^2.0.1",
5757
"rimraf": "^6.0.1",

packages/browser/jest-dom.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ declare namespace matchers {
260260
* @see
261261
* [testing-library/jest-dom#tohaveclass](https://github.com/testing-library/jest-dom#tohaveclass)
262262
*/
263-
toHaveClass(...classNames: Array<string | RegExp>): R
264263
toHaveClass(classNames: string, options?: {exact: boolean}): R
264+
toHaveClass(...classNames: Array<string | RegExp>): R
265265
/**
266266
* @description
267267
* This allows you to check whether the given form element has the specified displayed value (the one the

packages/browser/src/node/commands/screenshot.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export const screenshot: BrowserCommand<[string, ScreenshotOptions]> = async (
1818
const path = options.path
1919
? resolve(dirname(context.testPath), options.path)
2020
: resolveScreenshotPath(
21-
context.testPath,
22-
name,
23-
context.project.config,
24-
)
21+
context.testPath,
22+
name,
23+
context.project.config,
24+
)
2525
const savePath = normalize(path)
2626
await mkdir(dirname(path), { recursive: true })
2727

packages/browser/src/node/plugin.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,12 +579,12 @@ function resolveCoverageFolder(vitest: Vitest) {
579579
const options = vitest.config
580580
const htmlReporter = options.coverage?.enabled
581581
? toArray(options.coverage.reporter).find((reporter) => {
582-
if (typeof reporter === 'string') {
583-
return reporter === 'html'
584-
}
582+
if (typeof reporter === 'string') {
583+
return reporter === 'html'
584+
}
585585

586-
return reporter[0] === 'html'
587-
})
586+
return reporter[0] === 'html'
587+
})
588588
: undefined
589589

590590
if (!htmlReporter) {
@@ -599,8 +599,8 @@ function resolveCoverageFolder(vitest: Vitest) {
599599

600600
const subdir
601601
= Array.isArray(htmlReporter)
602-
&& htmlReporter.length > 1
603-
&& 'subdir' in htmlReporter[1]
602+
&& htmlReporter.length > 1
603+
&& 'subdir' in htmlReporter[1]
604604
? htmlReporter[1].subdir
605605
: undefined
606606

packages/browser/src/node/serverTester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function resolveTester(
3737
// if decoded test file is "__vitest_all__" or not in the list of known files, run all tests
3838
const tests
3939
= testFile === '__vitest_all__'
40-
|| !testFiles.includes(testFile)
40+
|| !testFiles.includes(testFile)
4141
? '__vitest_browser_runner__.files'
4242
: JSON.stringify([testFile])
4343
const iframeId = JSON.stringify(testFile)

packages/browser/utils.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// we cannot bundle it because vitest depend on the @vitest/browser and vise versa
33
// fortunately, the file is quite small
44

5-
import { LocatorSelectors } from '@vitest/browser/context'
5+
import { LocatorSelectors, Locator } from '@vitest/browser/context'
66
import { StringifyOptions } from 'vitest/utils'
77

88
export type PrettyDOMOptions = Omit<StringifyOptions, 'maxLength'>

packages/expect/src/jest-asymmetric-matchers.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ export class ArrayContaining<T = unknown> extends AsymmetricMatcher<Array<T>> {
187187
const matcherContext = this.getMatcherContext()
188188
const result
189189
= this.sample.length === 0
190-
|| (Array.isArray(other)
191-
&& this.sample.every(item =>
192-
other.some(another =>
193-
equals(item, another, matcherContext.customTesters),
194-
),
195-
))
190+
|| (Array.isArray(other)
191+
&& this.sample.every(item =>
192+
other.some(another =>
193+
equals(item, another, matcherContext.customTesters),
194+
),
195+
))
196196

197197
return this.inverse ? !result : result
198198
}

packages/expect/src/jest-expect.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
8181
if (!isNot) {
8282
const message
8383
= utils.flag(this, 'message')
84-
|| 'expected promise to throw an error, but it didn\'t'
84+
|| 'expected promise to throw an error, but it didn\'t'
8585
const error = {
8686
showDiff: false,
8787
}
@@ -469,7 +469,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
469469
const { value, exists } = getValue()
470470
const pass
471471
= exists
472-
&& (args.length === 1 || jestEquals(expected, value, customTesters))
472+
&& (args.length === 1 || jestEquals(expected, value, customTesters))
473473

474474
const valueString
475475
= args.length === 1 ? '' : ` with value ${utils.objDisplay(expected)}`
@@ -751,7 +751,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
751751
if (!isNot) {
752752
const message
753753
= utils.flag(this, 'message')
754-
|| 'expected promise to throw an error, but it didn\'t'
754+
|| 'expected promise to throw an error, but it didn\'t'
755755
const error = {
756756
showDiff: false,
757757
}
@@ -774,7 +774,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
774774
if (!isThrow && !isNot) {
775775
const message
776776
= utils.flag(this, 'message')
777-
|| 'expected function to throw an error, but it didn\'t'
777+
|| 'expected function to throw an error, but it didn\'t'
778778
const error = {
779779
showDiff: false,
780780
}

0 commit comments

Comments
 (0)