Skip to content

Commit

Permalink
fix: move "mergeConfig" into "vitest/config" and add extension to imp…
Browse files Browse the repository at this point in the history
…orts in the documentation (#3139)
  • Loading branch information
sheremet-va authored Apr 5, 2023
1 parent 74b7ab0 commit 313032c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 50 deletions.
14 changes: 0 additions & 14 deletions docs/advanced/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,6 @@ const vitest = await createVitest('test', {
})
```
## mergeConfig
You can merge Vitest config using [mergeConfig](https://vitejs.dev/guide/api-javascript.html#mergeconfig) function. It returns a new config object.
```ts
import { mergeConfig } from 'vitest/node'

function mergeConfig(
defaults: Record<string, any>,
overrides: Record<string, any>,
isRoot = true,
): Record<string, any>
```

## Vitest
Vitest instance requires the current test mode. It can be either:
Expand Down
26 changes: 13 additions & 13 deletions docs/api/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type Awaitable<T> = T | PromiseLike<T>
For example, having this code you don't care for the return value of `stocks.getInfo` - it maybe a complex object, a string, or anything else. The code will still work.

```ts
import { Stocks } from './stocks'
import { Stocks } from './stocks.js'
const stocks = new Stocks()
stocks.sync('Bill')
if (stocks.getInfo('Bill'))
Expand All @@ -146,7 +146,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { Stocks } from './stocks'
import { Stocks } from './stocks.js'
const stocks = new Stocks()
test('if we know Bill stock, sell apples to him', () => {
Expand All @@ -166,7 +166,7 @@ type Awaitable<T> = T | PromiseLike<T>
For example, having this code you don't care for the return value of `stocks.stockFailed` - it may return any falsy value, but the code will still work.

```ts
import { Stocks } from './stocks'
import { Stocks } from './stocks.js'
const stocks = new Stocks()
stocks.sync('Bill')
if (!stocks.stockFailed('Bill'))
Expand All @@ -177,7 +177,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { Stocks } from './stocks'
import { Stocks } from './stocks.js'
const stocks = new Stocks()
test('if Bill stock hasn\'t failed, sell apples to him', () => {
Expand Down Expand Up @@ -251,7 +251,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { Stocks } from './stocks'
import { Stocks } from './stocks.js'
const stocks = new Stocks()
test('stocks are instance of Stocks', () => {
Expand All @@ -267,7 +267,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { getApples } from './stock'
import { getApples } from './stocks.js'
test('have more then 10 apples', () => {
expect(getApples()).toBeGreaterThan(10)
Expand All @@ -282,7 +282,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { getApples } from './stock'
import { getApples } from './stocks.js'
test('have 11 apples or more', () => {
expect(getApples()).toBeGreaterThanOrEqual(11)
Expand All @@ -297,7 +297,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { getApples } from './stock'
import { getApples } from './stocks.js'
test('have less then 20 apples', () => {
expect(getApples()).toBeLessThan(20)
Expand All @@ -312,7 +312,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { getApples } from './stock'
import { getApples } from './stocks.js'
test('have 11 apples or less', () => {
expect(getApples()).toBeLessThanOrEqual(11)
Expand Down Expand Up @@ -386,7 +386,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { getAllFruits } from './stock'
import { getAllFruits } from './stocks.js'
test('the fruit list contains orange', () => {
expect(getAllFruits()).toContain('orange')
Expand All @@ -402,7 +402,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { getFruitStock } from './stock'
import { getFruitStock } from './stocks.js'
test('apple available', () => {
expect(getFruitStock()).toContainEqual({ fruit: 'apple', count: 5 })
Expand Down Expand Up @@ -1060,7 +1060,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { db } from './db'
import { db } from './db.js'
const cbs = []
Expand Down Expand Up @@ -1113,7 +1113,7 @@ type Awaitable<T> = T | PromiseLike<T>

```ts
import { expect, test } from 'vitest'
import { generateId } from './generators'
import { generateId } from './generators.js'
test('"id" is a number', () => {
expect({ id: generateId() }).toEqual({ id: expect.any(Number) })
Expand Down
7 changes: 5 additions & 2 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ export default defineConfig({
When using a separate `vitest.config.js`, you can also extend Vite's options from another config file if needed:

```ts
import { mergeConfig } from 'vite'
import { defineConfig } from 'vitest/config'
import { defineConfig, mergeConfig } from 'vitest/config'
import viteConfig from './vite.config'

export default mergeConfig(viteConfig, defineConfig({
Expand All @@ -65,6 +64,10 @@ export default mergeConfig(viteConfig, defineConfig({
}))
```

::: warning
`mergeConfig` helper is availabe in Vitest since v0.30.0. You can import it from `vite` directly, if you use lower version.
:::

## Options

:::tip
Expand Down
38 changes: 19 additions & 19 deletions docs/guide/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ The following principles apply
```js
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { Client } from 'pg'
import { failure, success } from './handlers'
import { failure, success } from './handlers.js'

// handlers
export function success(data) {}
Expand Down Expand Up @@ -385,26 +385,26 @@ vi.spyOn(instance, 'method')
```

- Mock exported variables
```ts
// some-path.ts
```js
// some-path.js
export const getter = 'variable'
```
```ts
// some-path.test.ts
import * as exports from 'some-path'
import * as exports from './some-path.js'
vi.spyOn(exports, 'getter', 'get').mockReturnValue('mocked')
```

- Mock exported function

Example with `vi.mock`:
```ts
// ./some-path.ts
// ./some-path.js
export function method() {}
```
```ts
import { method } from './some-path.ts'
vi.mock('./some-path.ts', () => ({
import { method } from './some-path.js'
vi.mock('./some-path.js', () => ({
method: vi.fn()
}))
```
Expand All @@ -415,7 +415,7 @@ Don't forget that `vi.mock` call is hoisted to top of the file. **Do not** put `

Example with `vi.spyOn`:
```ts
import * as exports from 'some-path'
import * as exports from './some-path.js'
vi.spyOn(exports, 'method').mockImplementation(() => {})
```

Expand All @@ -427,8 +427,8 @@ Example with `vi.mock` and prototype:
export class SomeClass {}
```
```ts
import { SomeClass } from 'some-path'
vi.mock('some-path', () => {
import { SomeClass } from './some-path.js'
vi.mock('./some-path.js', () => {
const SomeClass = vi.fn()
SomeClass.prototype.someMethod = vi.fn()
return { SomeClass }
Expand All @@ -438,8 +438,8 @@ vi.mock('some-path', () => {

Example with `vi.mock` and return value:
```ts
import { SomeClass } from 'some-path'
vi.mock('some-path', () => {
import { SomeClass } from './some-path.js'
vi.mock('./some-path.js', () => {
const SomeClass = vi.fn(() => ({
someMethod: vi.fn()
}))
Expand All @@ -451,7 +451,7 @@ vi.mock('some-path', () => {
Example with `vi.spyOn`:

```ts
import * as exports from 'some-path'
import * as exports from './some-path.js'
vi.spyOn(exports, 'SomeClass').mockImplementation(() => {
// whatever suites you from first two examples
})
Expand All @@ -470,15 +470,15 @@ export function useObject() {

```ts
// useObject.js
import { useObject } from 'some-path'
import { useObject } from './some-path.js'
const obj = useObject()
obj.method()
```

```ts
// useObject.test.js
import { useObject } from 'some-path'
vi.mock('some-path', () => {
import { useObject } from './some-path.js'
vi.mock('./some-path.js', () => {
let _cache
const useObject = () => {
if (!_cache) {
Expand All @@ -501,9 +501,9 @@ expect(obj.method).toHaveBeenCalled()
- Mock part of a module

```ts
import { mocked, original } from 'some-path'
vi.mock('some-path', async () => {
const mod = await vi.importActual<typeof import('some-path')>('some-path')
import { mocked, original } from './some-path.js'
vi.mock('./some-path.js', async () => {
const mod = await vi.importActual<typeof import('./some-path.js')>('./some-path.js')
return {
...mod,
mocked: vi.fn()
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface UserConfig extends ViteUserConfig {

// will import vitest declare test in module 'vite'
export { configDefaults, defaultInclude, defaultExclude, coverageConfigDefaults } from './defaults'
export { mergeConfig } from 'vite'

export type { ConfigEnv }
export type UserConfigFn = (env: ConfigEnv) => UserConfig | Promise<UserConfig>
Expand Down
1 change: 0 additions & 1 deletion packages/vitest/src/node/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { configFiles } from '../constants'
import { Vitest } from './core'
import { VitestPlugin } from './plugins'

export { mergeConfig }
export async function createVitest(mode: VitestRunMode, options: UserConfig, viteOverrides: ViteUserConfig = {}) {
const ctx = new Vitest(mode)
const root = resolve(options.root || process.cwd())
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type { Vitest } from './core'
export { createVitest, mergeConfig } from './create'
export { createVitest } from './create'
export { VitestPlugin } from './plugins'
export { startVitest } from './cli-api'

Expand Down

0 comments on commit 313032c

Please sign in to comment.