[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat: add alias option (#1634)

* chore: fix typeof check in error

* feat: add alias option

* Update test/core/vitest.config.ts

authored by

Vladimir and committed by
GitHub
(Jul 19, 2022, 5:44 PM +0300) 7c2138f2 d5073341

+36 -3
+6
docs/config/index.md
··· 109 109 110 110 Interpret CJS module's default as named exports. 111 111 112 + ### alias 113 + 114 + - **Type:** `Record<string, string> | Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>` 115 + 116 + Define custom aliases when running inside tests. They will be merged with aliases from `resolve.alias`. 117 + 112 118 ### globals 113 119 114 120 - **Type:** `boolean`
+8
test/core/vitest.config.ts
··· 56 56 sequence: { 57 57 seed: 101, 58 58 }, 59 + alias: [ 60 + { 61 + find: 'test-alias', 62 + replacement: '', 63 + // vitest doesn't crash because function is defined 64 + customResolver: () => resolve(__dirname, 'src', 'aliased-mod.ts'), 65 + }, 66 + ], 59 67 }, 60 68 })
+1 -1
packages/vite-node/src/externalize.ts
··· 8 8 9 9 const defaultInline = [ 10 10 /virtual:/, 11 - /\.ts$/, 11 + /\.[mc]?ts$/, 12 12 ] 13 13 14 14 const depsExternal = [
+1
test/core/src/aliased-mod.ts
··· 1 + export const isAliased = true
+7
test/core/test/alias.test.ts
··· 1 + // @ts-expect-error aliased to ../src/aliased-mod.ts 2 + import { isAliased } from 'test-alias' 3 + import { expect, test } from 'vitest' 4 + 5 + test('check that test.alias works', () => { 6 + expect(isAliased).toBe(true) 7 + })
+1 -1
packages/vitest/src/runtime/error.ts
··· 81 81 if (typeof err.message === 'string') 82 82 err.message = normalizeErrorMessage(err.message) 83 83 84 - if (typeof err.cause === 'object' && err.cause.message === 'string') 84 + if (typeof err.cause === 'object' && typeof err.cause.message === 'string') 85 85 err.cause.message = normalizeErrorMessage(err.cause.message) 86 86 } 87 87 catch {}
+8 -1
packages/vitest/src/types/config.ts
··· 1 - import type { CommonServerOptions } from 'vite' 1 + import type { AliasOptions, CommonServerOptions } from 'vite' 2 2 import type { PrettyFormatOptions } from 'pretty-format' 3 3 import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers' 4 4 import type { BuiltinReporters } from '../node/reporters' ··· 393 393 */ 394 394 seed?: number 395 395 } 396 + 397 + /** 398 + * Specifies an `Object`, or an `Array` of `Object`, 399 + * which defines aliases used to replace values in `import` or `require` statements. 400 + * Will be merged with the default aliases inside `resolve.alias`. 401 + */ 402 + alias?: AliasOptions 396 403 } 397 404 398 405 export interface UserConfig extends InlineConfig {
+4
packages/vitest/src/node/plugins/index.ts
··· 75 75 // by default Vite resolves `module` field, which not always a native ESM module 76 76 // setting this option can bypass that and fallback to cjs version 77 77 mainFields: [], 78 + alias: preOptions.alias, 78 79 }, 79 80 server: { 80 81 ...preOptions.api, ··· 100 101 const viteConfigTest = (viteConfig.test as any) || {} 101 102 if (viteConfigTest.watch === false) 102 103 viteConfigTest.run = true 104 + 105 + if ('alias' in viteConfigTest) 106 + delete viteConfigTest.alias 103 107 104 108 // viteConfig.test is final now, merge it for real 105 109 options = deepMerge(