Skip to content

Commit

Permalink
Merge branch 'main' into fix/dont-toggle-cursor-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Jan 24, 2025
2 parents f80a646 + 9e40437 commit 95810dd
Show file tree
Hide file tree
Showing 109 changed files with 2,067 additions and 1,669 deletions.
6 changes: 3 additions & 3 deletions .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
"@types/chai",
"@sinonjs/fake-timers",
"cac",
// Pinned due to https://github.com/vitest-dev/vitest/issues/4710
"log-update",
// Pinned because it requires "node" condition in the new version
// But we intentionally build Vitest ambiguously
"find-up",
// Transitive dependency that we patch
"acorn"
"acorn",
// Keep using codemirror 5
"codemirror"
]
}
6 changes: 3 additions & 3 deletions docs/guide/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ Run tests on different machines using [`--shard`](/guide/cli#shard) and [`--repo
All test and coverage results can be merged at the end of your CI pipeline using `--merge-reports` command:

```bash
vitest --shard=1/2 --reporter=blob
vitest --shard=2/2 --reporter=blob
vitest --merge-reports --reporter=junit --coverage.reporter=text
vitest --shard=1/2 --reporter=blob --coverage
vitest --shard=2/2 --reporter=blob --coverage
vitest --merge-reports --reporter=junit --coverage
```

See [`Improving Performance | Sharding`](/guide/improving-performance#sharding) for more information.
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ expect(() => {

See PR for more details: [#5876](https://github.com/vitest-dev/vitest/pull/5876).

### `module` condition export is not resolved by default on Vite 6

Vite 6 allows more flexible [`resolve.conditions`](https://vite.dev/config/shared-options#resolve-conditions) options and Vitest configures it to exclude `module` conditional export by default.

### `Custom` Type is Deprecated <Badge type="danger">API</Badge> {#custom-type-is-deprecated}

The `Custom` type is now an alias for the `Test` type. Note that Vitest updated the public types in 2.1 and changed exported names to `RunnerCustomCase` and `RunnerTestCase`:
Expand Down
80 changes: 69 additions & 11 deletions docs/guide/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,17 +430,20 @@ it('can return a value multiple times', () => {

## Requests

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.
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.

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/)

### Configuration

You can use it like below in your [setup file](/config/#setupfiles)
```js

::: code-group

```js [HTTP Setup]
import { afterAll, afterEach, beforeAll } from 'vitest'
import { setupServer } from 'msw/node'
import { graphql, http, HttpResponse } from 'msw'
import { http, HttpResponse } from 'msw'

const posts = [
{
Expand All @@ -458,28 +461,83 @@ export const restHandlers = [
}),
]

const server = setupServer(...restHandlers)

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

// Close server after all tests
afterAll(() => server.close())

// Reset handlers after each test for test isolation
afterEach(() => server.resetHandlers())
```

```js [GrapQL Setup]
import { afterAll, afterEach, beforeAll } from 'vitest'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw'

const posts = [
{
userId: 1,
id: 1,
title: 'first post title',
body: 'first post body',
},
// ...
]

const graphqlHandlers = [
graphql.query('ListPosts', () => {
return HttpResponse.json(
{
data: { posts },
},
)
return HttpResponse.json({
data: { posts },
})
}),
]

const server = setupServer(...restHandlers, ...graphqlHandlers)
const server = setupServer(...graphqlHandlers)

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

// Close server after all tests
// Close server after all tests
afterAll(() => server.close())

// Reset handlers after each test `important for test isolation`
// Reset handlers after each test for test isolation
afterEach(() => server.resetHandlers())
```

```js [WebSocket Setup]
import { afterAll, afterEach, beforeAll } from 'vitest'
import { setupServer } from 'msw/node'
import { ws } from 'msw'

const chat = ws.link('wss://chat.example.com')

const wsHandlers = [
chat.addEventListener('connection', ({ client }) => {
client.addEventListener('message', (event) => {
console.log('Received message from client:', event.data)
// Echo the received message back to the client
client.send(`Server received: ${event.data}`)
})
}),
]

const server = setupServer(...wsHandlers)

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

// Close server after all tests
afterAll(() => server.close())

// Reset handlers after each test for test isolation
afterEach(() => server.resetHandlers())
```
:::

> 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.
### More
Expand Down
10 changes: 5 additions & 5 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"generate-pwa-icons": "pwa-assets-generator"
},
"dependencies": {
"@vueuse/core": "^12.4.0",
"@vueuse/core": "^12.5.0",
"vue": "^3.5.12"
},
"devDependencies": {
"@iconify-json/carbon": "^1.2.5",
"@iconify-json/logos": "^1.2.4",
"@shikijs/transformers": "^1.27.2",
"@shikijs/vitepress-twoslash": "^1.27.2",
"@shikijs/transformers": "^1.29.1",
"@shikijs/vitepress-twoslash": "^1.29.1",
"@unocss/reset": "^0.65.4",
"@vite-pwa/assets-generator": "^0.2.6",
"@vite-pwa/vitepress": "^0.5.3",
Expand All @@ -32,8 +32,8 @@
"unplugin-vue-components": "^0.28.0",
"vite": "^5.2.8",
"vite-plugin-pwa": "^0.21.1",
"vitepress": "^1.5.0",
"vitepress-plugin-group-icons": "^1.3.4",
"vitepress": "^1.6.3",
"vitepress-plugin-group-icons": "^1.3.5",
"vitepress-plugin-tabs": "^0.5.0",
"workbox-window": "^7.3.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/in-source-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test:run": "vitest run"
},
"devDependencies": {
"typescript": "^5.5.4",
"typescript": "^5.7.3",
"vitest": "latest"
}
}
2 changes: 1 addition & 1 deletion examples/sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"svelte": "^3.59.1",
"svelte-check": "^3.4.3",
"tslib": "^2.5.3",
"typescript": "^5.2.2",
"typescript": "^5.7.3",
"vite": "latest",
"vitest": "latest"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/typecheck/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"devDependencies": {
"@types/node": "^20.11.5",
"@vitest/ui": "latest",
"typescript": "^5.2.2",
"typescript": "^5.7.3",
"vite": "latest",
"vitest": "latest"
},
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vitest/monorepo",
"type": "module",
"version": "3.0.3",
"version": "3.0.4",
"private": true,
"packageManager": "pnpm@9.15.4",
"description": "Next generation testing framework powered by Vite",
Expand Down Expand Up @@ -36,26 +36,26 @@
"test:browser:playwright": "pnpm -C test/browser run test:playwright"
},
"devDependencies": {
"@antfu/eslint-config": "^3.11.2",
"@antfu/eslint-config": "^3.16.0",
"@antfu/ni": "^23.2.0",
"@playwright/test": "^1.49.1",
"@rollup/plugin-commonjs": "^28.0.2",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.0",
"@types/node": "^22.10.7",
"@types/node": "^22.10.9",
"@types/ws": "^8.5.13",
"@vitest/browser": "workspace:*",
"@vitest/coverage-istanbul": "workspace:*",
"@vitest/coverage-v8": "workspace:*",
"@vitest/ui": "workspace:*",
"bumpp": "^9.10.1",
"changelogithub": "^0.13.11",
"bumpp": "^9.10.2",
"changelogithub": "^13.12.1",
"esbuild": "^0.24.2",
"eslint": "^9.16.0",
"eslint": "^9.18.0",
"magic-string": "^0.30.17",
"pathe": "^2.0.1",
"pathe": "^2.0.2",
"rimraf": "^6.0.1",
"rollup": "^4.30.1",
"rollup": "^4.31.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-license": "^3.5.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/jest-dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ declare namespace matchers {
* @see
* [testing-library/jest-dom#tohaveclass](https://github.com/testing-library/jest-dom#tohaveclass)
*/
toHaveClass(...classNames: Array<string | RegExp>): R
toHaveClass(classNames: string, options?: {exact: boolean}): R
toHaveClass(...classNames: Array<string | RegExp>): R
/**
* @description
* This allows you to check whether the given form element has the specified displayed value (the one the
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vitest/browser",
"type": "module",
"version": "3.0.3",
"version": "3.0.4",
"description": "Browser running for Vitest",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down Expand Up @@ -88,7 +88,7 @@
},
"dependencies": {
"@testing-library/dom": "^10.4.0",
"@testing-library/user-event": "^14.6.0",
"@testing-library/user-event": "^14.6.1",
"@vitest/mocker": "workspace:*",
"@vitest/utils": "workspace:*",
"magic-string": "^0.30.17",
Expand All @@ -108,7 +108,7 @@
"flatted": "^3.3.2",
"ivya": "^1.1.1",
"mime": "^4.0.6",
"pathe": "^2.0.1",
"pathe": "^2.0.2",
"periscopic": "^4.0.2",
"playwright": "^1.49.1",
"playwright-core": "^1.49.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/browser/src/node/commands/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export const screenshot: BrowserCommand<[string, ScreenshotOptions]> = async (
const path = options.path
? resolve(dirname(context.testPath), options.path)
: resolveScreenshotPath(
context.testPath,
name,
context.project.config,
)
context.testPath,
name,
context.project.config,
)
const savePath = normalize(path)
await mkdir(dirname(path), { recursive: true })

Expand Down
24 changes: 16 additions & 8 deletions packages/browser/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ export default (parentServer: ParentBrowserProject, base = '/'): Plugin[] => {
}

const url = new URL(req.url, 'http://localhost')
const file = url.searchParams.get('file')
const id = url.searchParams.get('id')
if (!id) {
res.statusCode = 404
res.end()
return
}

const task = parentServer.vitest.state.idMap.get(id)
const file = task?.meta.failScreenshotPath
if (!file) {
res.statusCode = 404
res.end()
Expand Down Expand Up @@ -571,12 +579,12 @@ function resolveCoverageFolder(vitest: Vitest) {
const options = vitest.config
const htmlReporter = options.coverage?.enabled
? toArray(options.coverage.reporter).find((reporter) => {
if (typeof reporter === 'string') {
return reporter === 'html'
}
if (typeof reporter === 'string') {
return reporter === 'html'
}

return reporter[0] === 'html'
})
return reporter[0] === 'html'
})
: undefined

if (!htmlReporter) {
Expand All @@ -591,8 +599,8 @@ function resolveCoverageFolder(vitest: Vitest) {

const subdir
= Array.isArray(htmlReporter)
&& htmlReporter.length > 1
&& 'subdir' in htmlReporter[1]
&& htmlReporter.length > 1
&& 'subdir' in htmlReporter[1]
? htmlReporter[1].subdir
: undefined

Expand Down
3 changes: 3 additions & 0 deletions packages/browser/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export function createBrowserPool(vitest: Vitest): ProcessPool {
}
await project._initBrowserProvider()

if (!project.browser) {
throw new TypeError(`The browser server was not initialized${project.name ? ` for the "${project.name}" project` : ''}. This is a bug in Vitest. Please, open a new issue with reproduction.`)
}
await executeTests(method, project, files)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/node/serverTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function resolveTester(
// if decoded test file is "__vitest_all__" or not in the list of known files, run all tests
const tests
= testFile === '__vitest_all__'
|| !testFiles.includes(testFile)
|| !testFiles.includes(testFile)
? '__vitest_browser_runner__.files'
: JSON.stringify([testFile])
const iframeId = JSON.stringify(testFile)
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// we cannot bundle it because vitest depend on the @vitest/browser and vise versa
// fortunately, the file is quite small

import { LocatorSelectors } from '@vitest/browser/context'
import { LocatorSelectors, Locator } from '@vitest/browser/context'
import { StringifyOptions } from 'vitest/utils'

export type PrettyDOMOptions = Omit<StringifyOptions, 'maxLength'>
Expand Down
4 changes: 2 additions & 2 deletions packages/coverage-istanbul/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vitest/coverage-istanbul",
"type": "module",
"version": "3.0.3",
"version": "3.0.4",
"description": "Istanbul coverage provider for Vitest",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down Expand Up @@ -63,7 +63,7 @@
"@types/istanbul-lib-source-maps": "^4.0.4",
"@types/istanbul-reports": "^3.0.4",
"@types/test-exclude": "^6.0.2",
"pathe": "^2.0.1",
"pathe": "^2.0.2",
"vitest": "workspace:*"
}
}
Loading

0 comments on commit 95810dd

Please sign in to comment.