Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix/omit-node-polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel-G committed Sep 22, 2023
2 parents d42f62d + 4e94120 commit a6a9db3
Show file tree
Hide file tree
Showing 93 changed files with 1,402 additions and 1,295 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
if: ${{ github.event.issue.pull_request && (github.event.comment.body == '/bench' || github.event.comment.body == '/benchmark') }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install pnpm
uses: pnpm/action-setup@v2
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-and-cache

Expand All @@ -36,7 +36,7 @@ jobs:
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-and-cache

Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
fail-fast: false

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-and-cache
with:
Expand All @@ -93,7 +93,7 @@ jobs:
timeout-minutes: 10

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-and-cache
with:
Expand All @@ -116,7 +116,7 @@ jobs:
env:
BROWSER: ${{ matrix.browser[0] }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-and-cache
with:
Expand Down Expand Up @@ -156,7 +156,7 @@ jobs:
env:
BROWSER: ${{ matrix.browser[0] }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-and-cache
with:
Expand Down Expand Up @@ -187,7 +187,7 @@ jobs:
timeout-minutes: 30

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-and-cache
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/components/Contributors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { contributors } from '../contributors'
<template>
<div flex="~ wrap gap2" justify-center>
<a v-for="{ name, avatar } of contributors" :key="name" :href="`https://github.com/${name}`" m-0 rel="noopener noreferrer" :aria-label="`${name} on GitHub`">
<img loading="lazy" :src="avatar" width="50" height="50" rounded-full h-12 w-12 :alt="`${name}'s avatar`">
<img loading="lazy" decoding="async" :src="avatar" width="50" height="50" rounded-full h-12 w-12 :alt="`${name}'s avatar`">
</a>
</div>
</template>
5 changes: 4 additions & 1 deletion docs/.vitepress/components/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { teamMembers } from '../contributors'
loading="lazy"
src="/sponsors/antfu.svg"
alt="Anthony Fu's sponsors"
decoding="async"
>
</a>
</div>
Expand All @@ -52,6 +53,7 @@ import { teamMembers } from '../contributors'
loading="lazy"
src="/sponsors/sheremet-va.svg"
alt="Vladimir's sponsors"
decoding="async"
>
</a>
</div>
Expand All @@ -66,13 +68,14 @@ import { teamMembers } from '../contributors'
loading="lazy"
src="/sponsors/patak-dev.svg"
alt="Patak's sponsors"
decoding="async"
>
</a>
</div>
</div>
<p class="text-center opacity-75">
<a href="https://www.netlify.com" rel="noopener noreferrer">
<img src="/netlify.svg" alt="Deploys by Netlify" width="114" height="151">
<img src="/netlify.svg" alt="Deploys by Netlify" width="114" height="151" decoding="async">
</a>
</p>
</div>
Expand Down
8 changes: 5 additions & 3 deletions docs/.vitepress/scripts/fetch-avatars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ async function fetchAvatars() {

async function fetchSponsors() {
await fs.ensureDir(dirSponsors)
await download('https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg', join(dirSponsors, 'antfu.svg'))
await download('https://cdn.jsdelivr.net/gh/patak-dev/static/sponsors.svg', join(dirSponsors, 'patak-dev.svg'))
await download('https://cdn.jsdelivr.net/gh/sheremet-va/static/sponsors.svg', join(dirSponsors, 'sheremet-va.svg'))
await Promise.all([
download('https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg', join(dirSponsors, 'antfu.svg')),
download('https://cdn.jsdelivr.net/gh/patak-dev/static/sponsors.svg', join(dirSponsors, 'patak-dev.svg')),
download('https://cdn.jsdelivr.net/gh/sheremet-va/static/sponsors.svg', join(dirSponsors, 'sheremet-va.svg')),
])
}

fetchAvatars()
Expand Down
12 changes: 6 additions & 6 deletions docs/.vitepress/scripts/transformHead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export async function transformHead({ pageData }: TransformContext): Promise<Hea
head.push(['link', { rel: 'preconnect', href: link }])
})

head.push(['link', { rel: 'prefetch', href: '/logo.svg' }])
head.push(['link', { rel: 'prefetch', href: '/logo.svg', as: 'image' }])
if (home) {
head.push(['link', { rel: 'prefetch', href: '/logo-shadow.svg' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/antfu.svg' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/sheremet-va.svg' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/patak-dev.svg' }])
head.push(['link', { rel: 'prefetch', href: '/netlify.svg' }])
head.push(['link', { rel: 'prefetch', href: '/logo-shadow.svg', as: 'image' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/antfu.svg', as: 'image' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/sheremet-va.svg', as: 'image' }])
head.push(['link', { rel: 'prefetch', href: '/sponsors/patak-dev.svg', as: 'image' }])
head.push(['link', { rel: 'prefetch', href: '/netlify.svg', as: 'image' }])
}

return head
Expand Down
104 changes: 95 additions & 9 deletions docs/api/vi.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ import { vi } from 'vitest'
```ts
// increment.test.js
import { vi } from 'vitest'
// axios is a default export from `__mocks__/axios.js`
import axios from 'axios'
// increment is a named export from `src/__mocks__/increment.js`
import { increment } from '../increment.js'
Expand Down Expand Up @@ -371,7 +371,7 @@ test('importing the next module imports mocked one', async () => {

```ts
import { vi } from 'vitest'
import { data } from './data.js' // Will not get reevaluated beforeEach test
beforeEach(() => {
Expand All @@ -398,12 +398,6 @@ Does not reset mocks registry. To clear mocks registry, use [`vi.unmock`](#vi-un

Will call [`.mockRestore()`](/api/mock#mockrestore) on all spies. This will clear mock history and reset its implementation to the original one.

## vi.restoreCurrentDate

- **Type:** `() => void`

Restores `Date` back to its native implementation.

## vi.stubEnv

- **Type:** `(name: string, value: string) => Vitest`
Expand Down Expand Up @@ -706,8 +700,100 @@ unmockedIncrement(30) === 31

The implementation is based internally on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers).

## vi.isFakeTimers

- **Type:** `() => boolean`
- **Version:** Since Vitest 0.34.5

Returns `true` if fake timers are enabled.

## vi.useRealTimers

- **Type:** `() => Vitest`

When timers are run out, you may call this method to return mocked timers to its original implementations. All timers that were run before will not be restored.

### vi.waitFor

- **Type:** `function waitFor<T>(callback: WaitForCallback<T>, options?: number | WaitForOptions): Promise<T>`
- **Version**: Since Vitest 0.34.5

Wait for the callback to execute successfully. If the callback throws an error or returns a rejected promise it will continue to wait until it succeeds or times out.

This is very useful when you need to wait for some asynchronous action to complete, for example, when you start a server and need to wait for it to start.

```ts
import { expect, test, vi } from 'vitest'
import { createServer } from './server.js'
test('Server started successfully', async () => {
const server = createServer()
await vi.waitFor(
() => {
if (!server.isReady)
throw new Error('Server not started')
console.log('Server started')
}, {
timeout: 500, // default is 1000
interval: 20, // default is 50
}
)
expect(server.isReady).toBe(true)
})
```

It also works for asynchronous callbacks

```ts
// @vitest-environment jsdom
import { expect, test, vi } from 'vitest'
import { getDOMElementAsync, populateDOMAsync } from './dom.js'
test('Element exists in a DOM', async () => {
// start populating DOM
populateDOMAsync()
const element = await vi.waitFor(async () => {
// try to get the element until it exists
const element = await getDOMElementAsync() as HTMLElement | null
expect(element).toBeTruthy()
expect(element.dataset.initialized).toBeTruthy()
return element
}, {
timeout: 500, // default is 1000
interval: 20, // default is 50
})
expect(element).toBeInstanceOf(HTMLElement)
})
```

If `vi.useFakeTimers` is used, `vi.waitFor` automatically calls `vi.advanceTimersByTime(interval)` in every check callback.

### vi.waitUntil

- **Type:** `function waitUntil(callback: WaitUntilCallback, options?: number | WaitUntilOptions): Promise`
- **Version**: Since Vitest 0.34.5

This is similar to `vi.waitFor`, but if the callback throws any errors, execution is immediately interrupted and an error message is received. If the callback returns falsy value, the next check will continue until truthy value is returned. This is useful when you need to wait for something to exist before taking the next step.

Look at the example below. We can use `vi.waitUntil` to wait for the element to appear on the page, and then we can do something with the element.

```ts
import { expect, test, vi } from 'vitest'
test('Element render correctly', async () => {
const element = await vi.waitUntil(
() => document.querySelector('.element'),
{
timeout: 500, // default is 1000
interval: 20, // default is 50
}
)
// do something with the element
expect(element.querySelector('.element-child')).toBeTruthy()
})
```
34 changes: 32 additions & 2 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1078,10 +1078,10 @@ See [istanbul documentation](https://github.com/istanbuljs/nyc#coverage-threshol

- **Type:** `boolean`
- **Default:** `false`
- **Available for providers:** `'v8'`
- **Available for providers:** `'v8' | 'istanbul'`
- **CLI:** `--coverage.100`, `--coverage.100=false`

Shortcut for `--check-coverage --lines 100 --functions 100 --branches 100 --statements 100`.
Shortcut for `--coverage.lines 100 --coverage.functions 100 --coverage.branches 100 --coverage.statements 100`.

#### coverage.ignoreClassMethods

Expand Down Expand Up @@ -1641,3 +1641,33 @@ export default defineConfig({
})
```

### diff

- **Type:** `string`
- **CLI:** `--diff=<value>`
- **Version:** Since Vitest 0.34.5

Path to a diff config that will be used to generate diff interface. Useful if you want to customize diff display.

:::code-group
```ts [vitest.diff.ts]
import type { DiffOptions } from 'vitest'
import c from 'picocolors'
export default {
aIndicator: c.bold('--'),
bIndicator: c.bold('++'),
omitAnnotationLines: true,
} satisfies DiffOptions
```

```ts [vitest.config.js]
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
diff: './vitest.diff.ts'
}
})
```
:::
2 changes: 1 addition & 1 deletion examples/mocks/test/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('mocking with factory', () => {
expect((example as any).then).toBe('a then export')
expect((example as any).mocked).toBe(true)
expect(example.square(2, 3)).toBe(5)
expect(example.asyncSquare(2, 3)).resolves.toBe(5)
await expect(example.asyncSquare(2, 3)).resolves.toBe(5)
})

test('successfully with actual', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vitest/monorepo",
"type": "module",
"version": "0.34.4",
"version": "0.34.5",
"private": true,
"packageManager": "pnpm@8.7.4",
"description": "A blazing fast unit test framework powered by Vite",
Expand Down
3 changes: 1 addition & 2 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": "0.34.4",
"version": "0.34.5",
"description": "Browser running for Vitest",
"license": "MIT",
"funding": "https://opencollective.com/vitest",
Expand Down Expand Up @@ -53,7 +53,6 @@
"@vitest/ui": "workspace:*",
"@vitest/ws-client": "workspace:*",
"periscopic": "^3.1.0",
"rollup": "^3.26.0",
"vitest": "workspace:*"
}
}
4 changes: 4 additions & 0 deletions packages/browser/src/client/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createClient } from '@vitest/ws-client'
import type { ResolvedConfig } from 'vitest'
import type { CancelReason, VitestRunner } from '@vitest/runner'
import type { VitestExecutor } from 'vitest/src/runtime/execute'
import { createBrowserRunner } from './runner'
import { importId } from './utils'
import { setupConsoleLogSpy } from './logger'
Expand Down Expand Up @@ -101,6 +102,7 @@ async function runTests(paths: string[], config: ResolvedConfig) {
const {
startTests,
setupCommonEnv,
loadDiffConfig,
takeCoverageInsideWorker,
} = await importId('vitest/browser') as typeof import('vitest/browser')

Expand All @@ -122,6 +124,8 @@ async function runTests(paths: string[], config: ResolvedConfig) {
config.snapshotOptions.snapshotEnvironment = new BrowserSnapshotEnvironment()

try {
runner.config.diffOptions = await loadDiffConfig(config, executor as VitestExecutor)

await setupCommonEnv(config)
const files = paths.map((path) => {
return (`${config.root}/${path}`).replace(/\/+/g, '/')
Expand Down
Loading

0 comments on commit a6a9db3

Please sign in to comment.