From 048931af9ab17ec27e90bc43bde914c2c76f1dc0 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 5 Apr 2023 17:59:24 +0200 Subject: [PATCH 1/2] fix: move "mergeConfig" into "vitest/config" and add extension to imports in the documentation --- docs/advanced/api.md | 14 ----------- docs/api/expect.md | 26 ++++++++++---------- docs/config/index.md | 3 +-- docs/guide/mocking.md | 38 +++++++++++++++--------------- packages/vitest/src/config.ts | 1 + packages/vitest/src/node/create.ts | 1 - packages/vitest/src/node/index.ts | 2 +- 7 files changed, 35 insertions(+), 50 deletions(-) diff --git a/docs/advanced/api.md b/docs/advanced/api.md index d9b48db9f551..c58aee76961a 100644 --- a/docs/advanced/api.md +++ b/docs/advanced/api.md @@ -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, - overrides: Record, - isRoot = true, -): Record -``` - ## Vitest Vitest instance requires the current test mode. It can be either: diff --git a/docs/api/expect.md b/docs/api/expect.md index 924bdc111a9b..448b80a5c4ba 100644 --- a/docs/api/expect.md +++ b/docs/api/expect.md @@ -135,7 +135,7 @@ type Awaitable = T | PromiseLike 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')) @@ -146,7 +146,7 @@ type Awaitable = T | PromiseLike ```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', () => { @@ -166,7 +166,7 @@ type Awaitable = T | PromiseLike 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')) @@ -177,7 +177,7 @@ type Awaitable = T | PromiseLike ```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', () => { @@ -251,7 +251,7 @@ type Awaitable = T | PromiseLike ```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', () => { @@ -267,7 +267,7 @@ type Awaitable = T | PromiseLike ```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) @@ -282,7 +282,7 @@ type Awaitable = T | PromiseLike ```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) @@ -297,7 +297,7 @@ type Awaitable = T | PromiseLike ```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) @@ -312,7 +312,7 @@ type Awaitable = T | PromiseLike ```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) @@ -386,7 +386,7 @@ type Awaitable = T | PromiseLike ```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') @@ -402,7 +402,7 @@ type Awaitable = T | PromiseLike ```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 }) @@ -1060,7 +1060,7 @@ type Awaitable = T | PromiseLike ```ts import { expect, test } from 'vitest' - import { db } from './db' + import { db } from './db.js' const cbs = [] @@ -1113,7 +1113,7 @@ type Awaitable = T | PromiseLike ```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) }) diff --git a/docs/config/index.md b/docs/config/index.md index 3bd1ce981718..64dde4d742a8 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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({ diff --git a/docs/guide/mocking.md b/docs/guide/mocking.md index ad0d5e12b652..d229324cb4e1 100644 --- a/docs/guide/mocking.md +++ b/docs/guide/mocking.md @@ -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) {} @@ -385,13 +385,13 @@ 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') ``` @@ -399,12 +399,12 @@ vi.spyOn(exports, 'getter', 'get').mockReturnValue('mocked') 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() })) ``` @@ -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(() => {}) ``` @@ -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 } @@ -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() })) @@ -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 }) @@ -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) { @@ -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('some-path') +import { mocked, original } from './some-path.js' +vi.mock('./some-path.js', async () => { + const mod = await vi.importActual('./some-path.js') return { ...mod, mocked: vi.fn() diff --git a/packages/vitest/src/config.ts b/packages/vitest/src/config.ts index 91ea018bf49f..b2ef089549c9 100644 --- a/packages/vitest/src/config.ts +++ b/packages/vitest/src/config.ts @@ -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 diff --git a/packages/vitest/src/node/create.ts b/packages/vitest/src/node/create.ts index 9f46f6a1d09f..e254af4456f3 100644 --- a/packages/vitest/src/node/create.ts +++ b/packages/vitest/src/node/create.ts @@ -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()) diff --git a/packages/vitest/src/node/index.ts b/packages/vitest/src/node/index.ts index e06018460653..804c24f70293 100644 --- a/packages/vitest/src/node/index.ts +++ b/packages/vitest/src/node/index.ts @@ -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' From c9ebb56bf5237a62c7838d4899529922b76a6985 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 5 Apr 2023 18:21:33 +0200 Subject: [PATCH 2/2] chore: add a warning --- docs/config/index.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/config/index.md b/docs/config/index.md index 64dde4d742a8..7e402953740a 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -64,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