Skip to content

Commit

Permalink
Merge branch 'main' into fix/browser-unhandled
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 22, 2024
2 parents 44cabb0 + 5d6d801 commit a910c31
Show file tree
Hide file tree
Showing 142 changed files with 2,736 additions and 1,803 deletions.
32 changes: 31 additions & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ List of files included in coverage as glob patterns
#### coverage.extension

- **Type:** `string | string[]`
- **Default:** `['.js', '.cjs', '.mjs', '.ts', '.mts', '.cts', '.tsx', '.jsx', '.vue', '.svelte', '.marko']`
- **Default:** `['.js', '.cjs', '.mjs', '.ts', '.mts', '.tsx', '.jsx', '.vue', '.svelte', '.marko']`
- **Available for providers:** `'v8' | 'istanbul'`
- **CLI:** `--coverage.extension=<extension>`, `--coverage.extension=<extension1> --coverage.extension=<extension2>`

Expand Down Expand Up @@ -1345,6 +1345,11 @@ Shortcut for `--coverage.thresholds.lines 100 --coverage.thresholds.functions 10

Sets thresholds for files matching the glob pattern.

::: tip NOTE
Vitest counts all files, including those covered by glob-patterns, into the global coverage thresholds.
This is different from Jest behavior.
:::

<!-- eslint-skip -->
```ts
{
Expand Down Expand Up @@ -1372,6 +1377,31 @@ Sets thresholds for files matching the glob pattern.
}
```

##### coverage.thresholds[glob-pattern].100

- **Type:** `boolean`
- **Default:** `false`
- **Available for providers:** `'v8' | 'istanbul'`

Sets thresholds to 100 for files matching the glob pattern.

<!-- eslint-skip -->
```ts
{
coverage: {
thresholds: {
// Thresholds for all files
functions: 95,
branches: 70,
// Thresholds for matching glob pattern
'src/utils/**.ts': { 100: true },
'**/math.ts': { 100: true }
}
}
}
```

#### coverage.ignoreEmptyLines

- **Type:** `boolean`
Expand Down
10 changes: 7 additions & 3 deletions docs/guide/browser/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,20 @@ Vitest exposes several `playwright` specific properties on the command context.
- `context` refers to the unique [BrowserContext](https://playwright.dev/docs/api/class-browsercontext).

```ts
import { defineCommand } from '@vitest/browser'
import { BrowserCommand } from 'vitest/node'

export const myCommand = defineCommand(async (ctx, arg1, arg2) => {
export const myCommand: BrowserCommand<[string, number]> = async (
ctx,
arg1: string,
arg2: number
) => {
if (ctx.provider.name === 'playwright') {
const element = await ctx.iframe.findByRole('alert')
const screenshot = await element.screenshot()
// do something with the screenshot
return difference
}
})
}
```

::: tip
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/browser/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Vitest browser requires spinning up the provider and the browser during the init

## Cross-Browser Testing

When you specify a browser name in the browser option, Vitest will try to run the specified browser using [WebdriverIO](https://webdriver.io/) by default, and then run the tests there. This feature makes cross-browser testing easy to use and configure in environments like a CI. If you don't want to use WebdriverIO, you can configure the custom browser provider by using `browser.provider` option.
When you specify a browser name in the browser option, Vitest will try to run the specified browser using `preview` by default, and then run the tests there. If you don't want to use `preview`, you can configure the custom browser provider by using `browser.provider` option.

To specify a browser using the CLI, use the `--browser` flag followed by the browser name, like this:

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/cli-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
| `--coverage.enabled` | Enables coverage collection. Can be overridden using the `--coverage` CLI option (default: `false`) |
| `--coverage.include <pattern>` | Files included in coverage as glob patterns. May be specified more than once when using multiple patterns (default: `**`) |
| `--coverage.exclude <pattern>` | Files to be excluded in coverage. May be specified more than once when using multiple extensions (default: Visit [`coverage.exclude`](https://vitest.dev/config/#coverage-exclude)) |
| `--coverage.extension <extension>` | Extension to be included in coverage. May be specified more than once when using multiple extensions (default: `[".js", ".cjs", ".mjs", ".ts", ".mts", ".cts", ".tsx", ".jsx", ".vue", ".svelte"]`) |
| `--coverage.extension <extension>` | Extension to be included in coverage. May be specified more than once when using multiple extensions (default: `[".js", ".cjs", ".mjs", ".ts", ".mts", ".tsx", ".jsx", ".vue", ".svelte"]`) |
| `--coverage.clean` | Clean coverage results before running tests (default: true) |
| `--coverage.cleanOnRerun` | Clean coverage report on watch rerun (default: true) |
| `--coverage.reportsDirectory <path>` | Directory to write coverage report to (default: ./coverage) |
Expand Down
6 changes: 6 additions & 0 deletions docs/guide/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ By default, you can use these environments:
- `happy-dom` emulates browser environment by providing Browser API, and considered to be faster than jsdom, but lacks some API, uses [`happy-dom`](https://github.com/capricorn86/happy-dom) package
- `edge-runtime` emulates Vercel's [edge-runtime](https://edge-runtime.vercel.app/), uses [`@edge-runtime/vm`](https://www.npmjs.com/package/@edge-runtime/vm) package

::: info
When using `jsdom` or `happy-dom` environments, Vitest follows the same rules that Vite does when importing [CSS](https://vitejs.dev/guide/features.html#css) and [assets](https://vitejs.dev/guide/features.html#static-assets). If importing external dependency fails with `unknown extension .css` error, you need to inline the whole import chain manually by adding all packages to [`server.deps.external`](/config/#server-deps-external). For example, if the error happens in `package-3` in this import chain: `source code -> package-1 -> package-2 -> package-3`, you need to add all three packages to `server.deps.external`.

Since Vitest 2.0.4 the `require` of CSS and assets inside the external dependencies are resolved automatically.
:::

## Environments for Specific Files

When setting `environment` option in your config, it will apply to all the test files in your project. To have more fine-grained control, you can use control comments to specify environment for specific files. Control comments are comments that start with `@vitest-environment` and are followed by the environment name:
Expand Down
9 changes: 9 additions & 0 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ Where Jest does it by default, when mocking a module and wanting this mocking to
server.deps.inline: ["lib-name"]
```

### expect.getState().currentTestName

Vitest's `test` names are joined with a `>` symbol to make it easier to distinguish tests from suites, while Jest uses an empty space (` `).

```
- `${describeTitle} ${testTitle}`
+ `${describeTitle} > ${testTitle}`
```

### Envs

Just like Jest, Vitest sets `NODE_ENV` to `test`, if it wasn't set before. Vitest also has a counterpart for `JEST_WORKER_ID` called `VITEST_POOL_ID` (always less than or equal to `maxThreads`), so if you rely on it, don't forget to rename it. Vitest also exposes `VITEST_WORKER_ID` which is a unique ID of a running worker - this number is not affected by `maxThreads`, and will increase with each created worker.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export function foo() {
}

export function foobar(injectedFoo) {
return injectedFoo !== foo // false
return injectedFoo === foo // false
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ vitest -u

When calling `toMatchSnapshot()`, we store all snapshots in a formatted snap file. That means we need to escape some characters (namely the double-quote `"` and backtick `` ` ``) in the snapshot string. Meanwhile, you might lose the syntax highlighting for the snapshot content (if they are in some language).

To improve this case, we introduce [`toMatchFileSnapshot()`](/api/expect#tomatchfilesnapshot) to explicitly snapshot in a file. This allows you to assign any file extension to the snapshot file, and making them more readable.
In light of this, we introduced [`toMatchFileSnapshot()`](/api/expect#tomatchfilesnapshot) to explicitly match against a file. This allows you to assign any file extension to the snapshot file, and makes them more readable.

```ts
import { expect, it } from 'vitest'
Expand Down
8 changes: 4 additions & 4 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
"devDependencies": {
"@iconify-json/carbon": "^1.1.36",
"@iconify-json/logos": "^1.1.43",
"@shikijs/vitepress-twoslash": "^1.10.0",
"@unocss/reset": "^0.61.0",
"@shikijs/vitepress-twoslash": "^1.10.3",
"@unocss/reset": "^0.61.3",
"@vite-pwa/assets-generator": "^0.2.4",
"@vite-pwa/vitepress": "^0.5.0",
"@vitejs/plugin-vue": "latest",
"fast-glob": "^3.3.2",
"https-localhost": "^4.7.1",
"unocss": "^0.61.0",
"unocss": "^0.61.3",
"unplugin-vue-components": "^0.27.2",
"vite": "^5.2.8",
"vite-plugin-pwa": "^0.20.0",
"vitepress": "^1.2.3",
"vitepress": "^1.3.1",
"workbox-window": "^7.1.0"
}
}
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default antfu(

// TODO: migrate and turn it back on
'ts/ban-types': 'off',
'ts/no-unsafe-function-type': 'off',

'no-restricted-imports': [
'error',
Expand Down
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vitest/monorepo",
"type": "module",
"version": "2.0.3",
"version": "2.0.4",
"private": true,
"packageManager": "pnpm@9.5.0",
"description": "Next generation testing framework powered by Vite",
Expand Down Expand Up @@ -36,34 +36,34 @@
"test:browser:playwright": "pnpm -C test/browser run test:playwright"
},
"devDependencies": {
"@antfu/eslint-config": "^2.21.2",
"@antfu/ni": "^0.21.12",
"@playwright/test": "^1.45.0",
"@antfu/eslint-config": "^2.22.2",
"@antfu/ni": "^0.22.0",
"@playwright/test": "^1.45.1",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/node": "^20.14.9",
"@types/ws": "^8.5.10",
"@types/node": "^20.14.10",
"@types/ws": "^8.5.11",
"@vitest/browser": "workspace:*",
"@vitest/coverage-istanbul": "workspace:*",
"@vitest/coverage-v8": "workspace:*",
"@vitest/ui": "workspace:*",
"bumpp": "^9.4.1",
"esbuild": "^0.22.0",
"eslint": "^9.6.0",
"esbuild": "^0.23.0",
"eslint": "^9.7.0",
"fast-glob": "^3.3.2",
"magic-string": "^0.30.10",
"pathe": "^1.1.2",
"rimraf": "^6.0.1",
"rollup": "^4.18.0",
"rollup": "^4.18.1",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-license": "^3.5.1",
"tsx": "^4.16.0",
"typescript": "^5.5.2",
"rollup-plugin-license": "^3.5.2",
"tsx": "^4.16.2",
"typescript": "^5.5.3",
"vite": "^5.3.3",
"vitest": "workspace:*",
"zx": "^8.1.3"
"zx": "^8.1.4"
},
"pnpm": {
"overrides": {
Expand Down
14 changes: 7 additions & 7 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": "2.0.3",
"version": "2.0.4",
"description": "Browser running for Vitest",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down Expand Up @@ -76,17 +76,17 @@
}
},
"dependencies": {
"@testing-library/dom": "^10.2.0",
"@testing-library/dom": "^10.3.1",
"@testing-library/user-event": "^14.5.2",
"@vitest/utils": "workspace:*",
"magic-string": "^0.30.10",
"msw": "^2.3.1",
"sirv": "^2.0.4",
"ws": "^8.17.1"
"ws": "^8.18.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.4.6",
"@types/ws": "^8.5.10",
"@types/ws": "^8.5.11",
"@vitest/runner": "workspace:*",
"@vitest/ui": "workspace:*",
"@vitest/ws-client": "workspace:*",
Expand All @@ -95,10 +95,10 @@
"flatted": "^3.3.1",
"pathe": "^1.1.2",
"periscopic": "^4.0.2",
"playwright": "^1.45.0",
"playwright-core": "^1.45.0",
"playwright": "^1.45.1",
"playwright-core": "^1.45.1",
"safaridriver": "^0.1.2",
"vitest": "workspace:*",
"webdriverio": "^8.39.0"
"webdriverio": "^8.39.1"
}
}
17 changes: 16 additions & 1 deletion packages/browser/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,22 @@ export default () =>
format: 'esm',
},
external,
plugins,
plugins: [
{
name: 'no-side-effects',
async resolveId(id, importer) {
// Clipboard injects "afterEach" callbacks
// We mark it as having no side effects to prevent it from being included in the bundle
if (id.includes('dataTransfer/Clipboard')) {
return {
...await this.resolve(id, importer),
moduleSideEffects: false,
}
}
},
},
...plugins,
],
},
{
input: './src/client/tester/context.ts',
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client/tester/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class VitestBrowserClientMocker {
try {
Object.defineProperty(newContainer, property, descriptor)
}
catch (error) {
catch {
// Ignore errors, just move on to the next prop.
}
continue
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client/tester/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function createBrowserRunner(
try {
await updateFilesLocations(files, this.sourceMapCache)
}
catch (_) {}
catch {}
}
return rpc().onCollected(files)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/browser/src/client/tester/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ async function executeTests(method: 'run' | 'collect', files: string[]) {
try {
await setupCommonEnv(config)
for (const file of files) {
state.filepath = file

if (method === 'run') {
await startTests([file], runner)
}
Expand Down
Loading

0 comments on commit a910c31

Please sign in to comment.