Skip to content

Commit

Permalink
feat: add alias option (#1634)
Browse files Browse the repository at this point in the history
* chore: fix typeof check in error

* feat: add alias option

* Update test/core/vitest.config.ts
  • Loading branch information
sheremet-va authored Jul 19, 2022
1 parent d507334 commit 7c2138f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 3 deletions.
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 = /\/(es|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
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function processError(err: any) {
if (typeof err.message === 'string')
err.message = normalizeErrorMessage(err.message)

if (typeof err.cause === 'object' && err.cause.message === 'string')
if (typeof err.cause === 'object' && typeof err.cause.message === 'string')
err.cause.message = normalizeErrorMessage(err.cause.message)
}
catch {}
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 crash because function is defined
customResolver: () => resolve(__dirname, 'src', 'aliased-mod.ts'),
},
],
},
})

0 comments on commit 7c2138f

Please sign in to comment.