Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
notNullish,
} from '@vitest/utils'
import { relative } from 'pathe'
import * as vite from 'vite'
import { defaultPort } from '../../constants'
import { configDefaults } from '../../defaults'
import { generateScopedClassName } from '../../integrations/css/css-modules'
Expand Down Expand Up @@ -76,22 +77,12 @@ export async function VitestPlugin(

const resolveOptions = getDefaultResolveOptions()

const config: ViteConfig = {
let config: ViteConfig = {
root: viteConfig.test?.root || options.root,
define: {
// disable replacing `process.env.NODE_ENV` with static string by vite:client-inject
'process.env.NODE_ENV': 'process.env.NODE_ENV',
},
esbuild:
viteConfig.esbuild === false
? false
: {
// Lowest target Vitest supports is Node18
target: viteConfig.esbuild?.target || 'node18',
sourcemap: 'external',
// Enables using ignore hint for coverage providers with @preserve keyword
legalComments: 'inline',
},
resolve: {
...resolveOptions,
alias: testConfig.alias,
Expand Down Expand Up @@ -147,6 +138,35 @@ export async function VitestPlugin(
},
}

if ('rolldownVersion' in vite) {
config = {
...config,
// eslint-disable-next-line ts/ban-ts-comment
// @ts-ignore rolldown-vite only
oxc: viteConfig.oxc === false
? false
: {
// eslint-disable-next-line ts/ban-ts-comment
// @ts-ignore rolldown-vite only
target: viteConfig.oxc?.target || 'node18',
},
}
}
else {
config = {
...config,
esbuild: viteConfig.esbuild === false
? false
: {
// Lowest target Vitest supports is Node18
target: viteConfig.esbuild?.target || 'node18',
sourcemap: 'external',
// Enables using ignore hint for coverage providers with @preserve keyword
legalComments: 'inline',
},
}
}

// inherit so it's available in VitestOptimizer
// I cannot wait to rewrite all of this in Vitest 4
if (options.cache != null) {
Expand Down
41 changes: 31 additions & 10 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ResolvedConfig, TestProjectInlineConfiguration } from '../types/co
import { existsSync, readFileSync } from 'node:fs'
import { deepMerge } from '@vitest/utils'
import { basename, dirname, relative, resolve } from 'pathe'
import * as vite from 'vite'
import { configDefaults } from '../../defaults'
import { generateScopedClassName } from '../../integrations/css/css-modules'
import { VitestFilteredOutProjectError } from '../errors'
Expand Down Expand Up @@ -117,7 +118,7 @@ export function WorkspaceVitestPlugin(
const root = testConfig.root || viteConfig.root || options.root

const resolveOptions = getDefaultResolveOptions()
const config: ViteConfig = {
let config: ViteConfig = {
root,
define: {
// disable replacing `process.env.NODE_ENV` with static string by vite:client-inject
Expand All @@ -127,15 +128,6 @@ export function WorkspaceVitestPlugin(
...resolveOptions,
alias: testConfig.alias,
},
esbuild: viteConfig.esbuild === false
? false
: {
// Lowest target Vitest supports is Node18
target: viteConfig.esbuild?.target || 'node18',
sourcemap: 'external',
// Enables using ignore hint for coverage providers with @preserve keyword
legalComments: 'inline',
},
server: {
// disable watch mode in workspaces,
// because it is handled by the top-level watcher
Expand All @@ -162,6 +154,35 @@ export function WorkspaceVitestPlugin(
test: {},
}

if ('rolldownVersion' in vite) {
config = {
...config,
// eslint-disable-next-line ts/ban-ts-comment
// @ts-ignore rolldown-vite only
oxc: viteConfig.oxc === false
? false
: {
// eslint-disable-next-line ts/ban-ts-comment
// @ts-ignore rolldown-vite only
target: viteConfig.oxc?.target || 'node18',
},
}
}
else {
config = {
...config,
esbuild: viteConfig.esbuild === false
? false
: {
// Lowest target Vitest supports is Node18
target: viteConfig.esbuild?.target || 'node18',
sourcemap: 'external',
// Enables using ignore hint for coverage providers with @preserve keyword
legalComments: 'inline',
},
}
}

;(config.test as ResolvedConfig).defines = defines

const classNameStrategy
Expand Down
19 changes: 19 additions & 0 deletions test/core/test/esnext-decorator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, it } from 'vitest'

it('decorators work', () => {
expect(Sut.mocked).toBe(true)
expect(new Sut()).toBeInstanceOf(Sut)
})

function exampleDecorator(ClassExample: any, context: ClassDecoratorContext): any {
if (context.kind !== 'class') {
throw new Error('not a class to decorate')
}
ClassExample.mocked = true
return ClassExample
}

@exampleDecorator
class Sut {
static mocked = false
}
18 changes: 0 additions & 18 deletions test/core/test/esnext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ it.skipIf(Number(version) < 20)('"v" flag in regexp', () => {
expect('👍🏼👍🏼👍🏼'.match(regexp)).toEqual(['👍🏼', '👍🏼', '👍🏼'])
})

it('decorators work', () => {
expect(Sut.mocked).toBe(true)
expect(new Sut()).toBeInstanceOf(Sut)
})

it('new "using" feature', () => {
let getResource = (): any => {
throw new Error('don\'t call me')
Expand All @@ -37,16 +32,3 @@ function resourceful(resourceDefault: string) {
},
}
}

function exampleDecorator(ClassExample: any, context: ClassDecoratorContext): any {
if (context.kind !== 'class') {
throw new Error('not a class to decorate')
}
ClassExample.mocked = true
return ClassExample
}

@exampleDecorator
class Sut {
static mocked = false
}
2 changes: 1 addition & 1 deletion test/core/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default defineConfig({
...defaultExclude,
// FIXME: wait for ecma decorator support in rolldown/oxc
// https://github.com/oxc-project/oxc/issues/9170
...(rolldownVersion ? ['**/esnext.test.ts'] : []),
...(rolldownVersion ? ['**/esnext-decorator.test.ts'] : []),
],
slowTestThreshold: 1000,
testTimeout: process.env.CI ? 10_000 : 5_000,
Expand Down
Loading