Skip to content

Commit

Permalink
feat: add alias option
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 12, 2022
1 parent 3718ded commit 12052a8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/.vitepress/components.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/vue-next/pull/3399
// Read more: https://github.com/vuejs/core/pull/3399
import '@vue/runtime-core'

declare module '@vue/runtime-core' {
Expand Down
6 changes: 6 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ This might potentially cause some misalignment if a package has different logic

Interpret CJS module's default as named exports.

### alias

- **Type:** `Record<string, string> | Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>`

Define custom aliases when running inside tests. They will be merged with aliases from `resolve.alias`.

### globals

- **Type:** `boolean`
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-node/src/externalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ESM_FOLDER_RE = /\/esm\/(.*\.js)$/

const defaultInline = [
/virtual:/,
/\.ts$/,
/\.[mc]?ts$/,
]

const depsExternal = [
Expand Down
4 changes: 4 additions & 0 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
// by default Vite resolves `module` field, which not always a native ESM module
// setting this option can bypass that and fallback to cjs version
mainFields: [],
alias: preOptions.alias,
},
server: {
...preOptions.api,
Expand All @@ -101,6 +102,9 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
if (viteConfigTest.watch === false)
viteConfigTest.run = true

if ('alias' in viteConfigTest)
delete viteConfigTest.alias

// viteConfig.test is final now, merge it for real
options = deepMerge(
{},
Expand Down
9 changes: 8 additions & 1 deletion packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommonServerOptions } from 'vite'
import type { AliasOptions, CommonServerOptions } from 'vite'
import type { PrettyFormatOptions } from 'pretty-format'
import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers'
import type { BuiltinReporters } from '../node/reporters'
Expand Down Expand Up @@ -393,6 +393,13 @@ export interface InlineConfig {
*/
seed?: number
}

/**
* Specifies an `Object`, or an `Array` of `Object`,
* which defines aliases used to replace values in `import` or `require` statements.
* Will be merged with the default aliases inside `resolve.alias`.
*/
alias?: AliasOptions
}

export interface UserConfig extends InlineConfig {
Expand Down
1 change: 1 addition & 0 deletions test/core/src/aliased-mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isAliased = true
7 changes: 7 additions & 0 deletions test/core/test/alias.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-expect-error aliased to ../src/aliased-mod.ts
import { isAliased } from 'test-alias'
import { expect, test } from 'vitest'

test('check that test.alias works', () => {
expect(isAliased).toBe(true)
})
8 changes: 8 additions & 0 deletions test/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,13 @@ export default defineConfig({
sequence: {
seed: 101,
},
alias: [
{
find: 'test-alias',
replacement: '',
// vitest doesn't crach because function is defined
customResolver: () => resolve(__dirname, 'src', 'aliased-mod.ts'),
},
],
},
})

0 comments on commit 12052a8

Please sign in to comment.