Skip to content

Commit 313032c

Browse files
authoredApr 5, 2023
fix: move "mergeConfig" into "vitest/config" and add extension to imports in the documentation (#3139)
1 parent 74b7ab0 commit 313032c

File tree

7 files changed

+39
-50
lines changed

7 files changed

+39
-50
lines changed
 

‎docs/advanced/api.md

-14
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@ const vitest = await createVitest('test', {
4040
})
4141
```
4242
43-
## mergeConfig
44-
45-
You can merge Vitest config using [mergeConfig](https://vitejs.dev/guide/api-javascript.html#mergeconfig) function. It returns a new config object.
46-
47-
```ts
48-
import { mergeConfig } from 'vitest/node'
49-
50-
function mergeConfig(
51-
defaults: Record<string, any>,
52-
overrides: Record<string, any>,
53-
isRoot = true,
54-
): Record<string, any>
55-
```
56-
5743
## Vitest
5844
5945
Vitest instance requires the current test mode. It can be either:

‎docs/api/expect.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type Awaitable<T> = T | PromiseLike<T>
135135
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.
136136

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

147147
```ts
148148
import { expect, test } from 'vitest'
149-
import { Stocks } from './stocks'
149+
import { Stocks } from './stocks.js'
150150
const stocks = new Stocks()
151151

152152
test('if we know Bill stock, sell apples to him', () => {
@@ -166,7 +166,7 @@ type Awaitable<T> = T | PromiseLike<T>
166166
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.
167167

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

178178
```ts
179179
import { expect, test } from 'vitest'
180-
import { Stocks } from './stocks'
180+
import { Stocks } from './stocks.js'
181181
const stocks = new Stocks()
182182

183183
test('if Bill stock hasn\'t failed, sell apples to him', () => {
@@ -251,7 +251,7 @@ type Awaitable<T> = T | PromiseLike<T>
251251

252252
```ts
253253
import { expect, test } from 'vitest'
254-
import { Stocks } from './stocks'
254+
import { Stocks } from './stocks.js'
255255
const stocks = new Stocks()
256256

257257
test('stocks are instance of Stocks', () => {
@@ -267,7 +267,7 @@ type Awaitable<T> = T | PromiseLike<T>
267267

268268
```ts
269269
import { expect, test } from 'vitest'
270-
import { getApples } from './stock'
270+
import { getApples } from './stocks.js'
271271

272272
test('have more then 10 apples', () => {
273273
expect(getApples()).toBeGreaterThan(10)
@@ -282,7 +282,7 @@ type Awaitable<T> = T | PromiseLike<T>
282282

283283
```ts
284284
import { expect, test } from 'vitest'
285-
import { getApples } from './stock'
285+
import { getApples } from './stocks.js'
286286

287287
test('have 11 apples or more', () => {
288288
expect(getApples()).toBeGreaterThanOrEqual(11)
@@ -297,7 +297,7 @@ type Awaitable<T> = T | PromiseLike<T>
297297

298298
```ts
299299
import { expect, test } from 'vitest'
300-
import { getApples } from './stock'
300+
import { getApples } from './stocks.js'
301301

302302
test('have less then 20 apples', () => {
303303
expect(getApples()).toBeLessThan(20)
@@ -312,7 +312,7 @@ type Awaitable<T> = T | PromiseLike<T>
312312

313313
```ts
314314
import { expect, test } from 'vitest'
315-
import { getApples } from './stock'
315+
import { getApples } from './stocks.js'
316316

317317
test('have 11 apples or less', () => {
318318
expect(getApples()).toBeLessThanOrEqual(11)
@@ -386,7 +386,7 @@ type Awaitable<T> = T | PromiseLike<T>
386386

387387
```ts
388388
import { expect, test } from 'vitest'
389-
import { getAllFruits } from './stock'
389+
import { getAllFruits } from './stocks.js'
390390

391391
test('the fruit list contains orange', () => {
392392
expect(getAllFruits()).toContain('orange')
@@ -402,7 +402,7 @@ type Awaitable<T> = T | PromiseLike<T>
402402

403403
```ts
404404
import { expect, test } from 'vitest'
405-
import { getFruitStock } from './stock'
405+
import { getFruitStock } from './stocks.js'
406406

407407
test('apple available', () => {
408408
expect(getFruitStock()).toContainEqual({ fruit: 'apple', count: 5 })
@@ -1060,7 +1060,7 @@ type Awaitable<T> = T | PromiseLike<T>
10601060

10611061
```ts
10621062
import { expect, test } from 'vitest'
1063-
import { db } from './db'
1063+
import { db } from './db.js'
10641064

10651065
const cbs = []
10661066

@@ -1113,7 +1113,7 @@ type Awaitable<T> = T | PromiseLike<T>
11131113

11141114
```ts
11151115
import { expect, test } from 'vitest'
1116-
import { generateId } from './generators'
1116+
import { generateId } from './generators.js'
11171117

11181118
test('"id" is a number', () => {
11191119
expect({ id: generateId() }).toEqual({ id: expect.any(Number) })

‎docs/config/index.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ export default defineConfig({
5454
When using a separate `vitest.config.js`, you can also extend Vite's options from another config file if needed:
5555

5656
```ts
57-
import { mergeConfig } from 'vite'
58-
import { defineConfig } from 'vitest/config'
57+
import { defineConfig, mergeConfig } from 'vitest/config'
5958
import viteConfig from './vite.config'
6059

6160
export default mergeConfig(viteConfig, defineConfig({
@@ -65,6 +64,10 @@ export default mergeConfig(viteConfig, defineConfig({
6564
}))
6665
```
6766

67+
::: warning
68+
`mergeConfig` helper is availabe in Vitest since v0.30.0. You can import it from `vite` directly, if you use lower version.
69+
:::
70+
6871
## Options
6972

7073
:::tip

‎docs/guide/mocking.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ The following principles apply
171171
```js
172172
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
173173
import { Client } from 'pg'
174-
import { failure, success } from './handlers'
174+
import { failure, success } from './handlers.js'
175175

176176
// handlers
177177
export function success(data) {}
@@ -385,26 +385,26 @@ vi.spyOn(instance, 'method')
385385
```
386386

387387
- Mock exported variables
388-
```ts
389-
// some-path.ts
388+
```js
389+
// some-path.js
390390
export const getter = 'variable'
391391
```
392392
```ts
393393
// some-path.test.ts
394-
import * as exports from 'some-path'
394+
import * as exports from './some-path.js'
395395
vi.spyOn(exports, 'getter', 'get').mockReturnValue('mocked')
396396
```
397397

398398
- Mock exported function
399399

400400
Example with `vi.mock`:
401401
```ts
402-
// ./some-path.ts
402+
// ./some-path.js
403403
export function method() {}
404404
```
405405
```ts
406-
import { method } from './some-path.ts'
407-
vi.mock('./some-path.ts', () => ({
406+
import { method } from './some-path.js'
407+
vi.mock('./some-path.js', () => ({
408408
method: vi.fn()
409409
}))
410410
```
@@ -415,7 +415,7 @@ Don't forget that `vi.mock` call is hoisted to top of the file. **Do not** put `
415415

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

@@ -427,8 +427,8 @@ Example with `vi.mock` and prototype:
427427
export class SomeClass {}
428428
```
429429
```ts
430-
import { SomeClass } from 'some-path'
431-
vi.mock('some-path', () => {
430+
import { SomeClass } from './some-path.js'
431+
vi.mock('./some-path.js', () => {
432432
const SomeClass = vi.fn()
433433
SomeClass.prototype.someMethod = vi.fn()
434434
return { SomeClass }
@@ -438,8 +438,8 @@ vi.mock('some-path', () => {
438438

439439
Example with `vi.mock` and return value:
440440
```ts
441-
import { SomeClass } from 'some-path'
442-
vi.mock('some-path', () => {
441+
import { SomeClass } from './some-path.js'
442+
vi.mock('./some-path.js', () => {
443443
const SomeClass = vi.fn(() => ({
444444
someMethod: vi.fn()
445445
}))
@@ -451,7 +451,7 @@ vi.mock('some-path', () => {
451451
Example with `vi.spyOn`:
452452

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

471471
```ts
472472
// useObject.js
473-
import { useObject } from 'some-path'
473+
import { useObject } from './some-path.js'
474474
const obj = useObject()
475475
obj.method()
476476
```
477477

478478
```ts
479479
// useObject.test.js
480-
import { useObject } from 'some-path'
481-
vi.mock('some-path', () => {
480+
import { useObject } from './some-path.js'
481+
vi.mock('./some-path.js', () => {
482482
let _cache
483483
const useObject = () => {
484484
if (!_cache) {
@@ -501,9 +501,9 @@ expect(obj.method).toHaveBeenCalled()
501501
- Mock part of a module
502502

503503
```ts
504-
import { mocked, original } from 'some-path'
505-
vi.mock('some-path', async () => {
506-
const mod = await vi.importActual<typeof import('some-path')>('some-path')
504+
import { mocked, original } from './some-path.js'
505+
vi.mock('./some-path.js', async () => {
506+
const mod = await vi.importActual<typeof import('./some-path.js')>('./some-path.js')
507507
return {
508508
...mod,
509509
mocked: vi.fn()

‎packages/vitest/src/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface UserConfig extends ViteUserConfig {
66

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

1011
export type { ConfigEnv }
1112
export type UserConfigFn = (env: ConfigEnv) => UserConfig | Promise<UserConfig>

‎packages/vitest/src/node/create.ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { configFiles } from '../constants'
77
import { Vitest } from './core'
88
import { VitestPlugin } from './plugins'
99

10-
export { mergeConfig }
1110
export async function createVitest(mode: VitestRunMode, options: UserConfig, viteOverrides: ViteUserConfig = {}) {
1211
const ctx = new Vitest(mode)
1312
const root = resolve(options.root || process.cwd())

‎packages/vitest/src/node/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export type { Vitest } from './core'
2-
export { createVitest, mergeConfig } from './create'
2+
export { createVitest } from './create'
33
export { VitestPlugin } from './plugins'
44
export { startVitest } from './cli-api'
55

0 commit comments

Comments
 (0)
Please sign in to comment.