Skip to content

Commit

Permalink
Merge branch 'main' into fix/coverage-v8-bundled-sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Aug 9, 2024
2 parents e810a2a + 61b84a2 commit 2bf21c3
Show file tree
Hide file tree
Showing 31 changed files with 400 additions and 260 deletions.
63 changes: 27 additions & 36 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:

jobs:
lint:
runs-on: ubuntu-latest
runs-on: macos-14
steps:
- uses: actions/checkout@v4

Expand All @@ -32,7 +32,27 @@ jobs:
- name: Lint
run: pnpm run lint

changed:
runs-on: macos-14
outputs:
should_skip: ${{ steps.changed-files.outputs.only_changed == 'true' }}

steps:
- uses: actions/checkout@v4

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c # v44.5.7
with:
files: |
docs/**
.github/**
!.github/workflows/ci.yml
**.md
test:
needs: changed
if: needs.changed.outputs.should_skip != 'true'
runs-on: ${{ matrix.os }}

timeout-minutes: 30
Expand All @@ -54,49 +74,34 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c # v44.5.7
with:
files: |
docs/**
.github/**
!.github/workflows/ci.yml
**.md
- uses: ./.github/actions/setup-and-cache
if: steps.changed-files.outputs.only_changed != 'true'
with:
node-version: ${{ matrix.node_version }}

- uses: browser-actions/setup-chrome@v1
if: steps.changed-files.outputs.only_changed != 'true'

- name: Install
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm i

- name: Install Playwright Dependencies
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm exec playwright install chromium --with-deps

- name: Build
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm run build

- name: Test
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm run test:ci

- name: Test Examples
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm run test:examples

- name: Unit Test UI
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm run -C packages/ui test:ui

test-browser:
needs: changed
if: needs.changed.outputs.should_skip != 'true'

runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand All @@ -114,46 +119,32 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c # v44.5.7
with:
files: |
docs/**
.github/**
!.github/workflows/ci.yml
**.md
- uses: ./.github/actions/setup-and-cache
if: steps.changed-files.outputs.only_changed != 'true'
with:
node-version: 20

- uses: browser-actions/setup-chrome@v1
if: ${{ steps.changed-files.outputs.only_changed != 'true' && matrix.browser[0] == 'chromium' }}
if: ${{ matrix.browser[0] == 'chromium' }}
- uses: browser-actions/setup-firefox@v1
if: ${{ steps.changed-files.outputs.only_changed != 'true' && matrix.browser[0] == 'firefox' }}
if: ${{ matrix.browser[0] == 'firefox' }}

- name: Install
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm i

- name: Install Playwright Dependencies
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm exec playwright install ${{ matrix.browser[0] }} --with-deps

- name: Build
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm run build

- name: Test Browser (playwright)
if: steps.changed-files.outputs.only_changed != 'true'
run: pnpm run test:browser:playwright
env:
BROWSER: ${{ matrix.browser[0] }}

- name: Test Browser (webdriverio)
run: pnpm run test:browser:webdriverio
if: ${{ steps.changed-files.outputs.only_changed != 'true' && matrix.browser[1] }}
if: ${{ matrix.browser[1] }}
env:
BROWSER: ${{ matrix.browser[1] }}
4 changes: 4 additions & 0 deletions docs/api/vi.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ vi.mock(import('./path/to/module.js'), async (importOriginal) => {

Under the hood, Vitest still operates on a string and not a module object.

If you are using TypeScript with `paths` aliases configured in `tsconfig.json` however, the compiler won't be able to correctly resolve import types.
In order to make it work, make sure to replace all aliased imports, with their corresponding relative paths.
Eg. use `import('./path/to/module.js')` instead of `import('@/module')`.

::: warning
`vi.mock` is hoisted (in other words, _moved_) to **top of the file**. It means that whenever you write it (be it inside `beforeEach` or `test`), it will actually be called before that.

Expand Down
52 changes: 49 additions & 3 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,54 @@ afterEach(() => {
globalThis.resetBeforeEachTest = true
```

### provide <Version>2.1.0</Version> {#provide}

- **Type:** `Partial<ProvidedContext>`

Define values that can be accessed inside your tests using `inject` method.

:::code-group
```ts [vitest.config.js]
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
provide: {
API_KEY: '123',
},
},
})
```
```ts [my.test.js]
import { expect, inject, test } from 'vitest'

test('api key is defined', () => {
expect(inject('API_KEY')).toBe('123')
})
```
:::

::: warning
Properties have to be strings and values need to be [serializable](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types) because this object will be transferred between different processes.
:::

::: tip
If you are using TypeScript, you will need to augment `ProvidedContext` type for type safe access:

```ts
// vitest.shims.d.ts

declare module 'vitest' {
export interface ProvidedContext {
API_KEY: string
}
}

// mark this file as a module so augmentation works correctly
export {}
```
:::

### globalSetup

- **Type:** `string | string[]`
Expand All @@ -1018,7 +1066,7 @@ Multiple globalSetup files are possible. setup and teardown are executed sequent
::: warning
Global setup runs only if there is at least one running test. This means that global setup might start running during watch mode after test file is changed (the test file will wait for global setup to finish before running).

Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via `provide` method:
Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via [`provide`](#provide) method:

:::code-group
```js [globalSetup.js]
Expand All @@ -1033,8 +1081,6 @@ export default function setup({ provide }: GlobalSetupContext) {
provide('wsPort', 3000)
}

// You can also extend `ProvidedContext` type
// to have type safe access to `provide/inject` methods:
declare module 'vitest' {
export interface ProvidedContext {
wsPort: number
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/browser/assertion-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ Tests in the browser might fail inconsistently due to their asynchronous nature.

```ts
import { expect, test } from 'vitest'
import { screen } from '@testing-library/dom'
import { page } from '@vitest/browser/context'

test('error banner is rendered', async () => {
triggerError()

// @testing-library provides queries with built-in retry-ability
// It will try to find the banner until it's rendered
const banner = await screen.findByRole('alert', {
const banner = page.getByRole('alert', {
name: /error/i,
})

Expand Down
Loading

0 comments on commit 2bf21c3

Please sign in to comment.