Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add alias option #1634

Merged
merged 3 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be named something else than alias ?

Genuinely asking cause it's not immediately clear to me how they compare/plug into the vite ones (alias in vite 2 and resolve.alias in vite 3). Easy to be confused when quickly checking config or looking for an existing issue.

Could it be renamed ? (ie: extraAlias, moduleMapper, testAlias...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s on test.alias, what is confusing here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right, no confusion. My bad, I was confused :)


### 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
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'),
},
],
},
})