Skip to content

Commit

Permalink
fix(runner): ensure Vitest is deduped (#3489)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 2, 2023
1 parent 8a11e63 commit 2deb70a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 29 deletions.
3 changes: 0 additions & 3 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { collectTests } from './collect'
import { processError } from './utils/error'
import { setCurrentTest } from './test-state'
import { hasFailed, hasTests } from './utils/tasks'
import { markVersion } from './version'

const now = Date.now

Expand Down Expand Up @@ -358,8 +357,6 @@ export async function runFiles(files: File[], runner: VitestRunner) {
}

export async function startTests(paths: string[], runner: VitestRunner) {
markVersion()

await runner.onBeforeCollect?.(paths)

const files = await collectTests(paths, runner)
Expand Down
3 changes: 0 additions & 3 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import type { VitestRunner } from './types/runner'
import { createChainable } from './utils/chain'
import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context'
import { getHooks, setFn, setHooks } from './map'
import { checkVersion } from './version'

// apis
export const suite = createSuite()
export const test = createTest(
function (name: string, fn?: TestFunction, options?: number | TestOptions) {
checkVersion()
getCurrentSuite().test.fn.call(this, name, fn, options)
},
)
Expand Down Expand Up @@ -192,7 +190,6 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m

function createSuite() {
function suiteFn(this: Record<string, boolean | undefined>, name: string, factory?: SuiteFactory, options?: number | TestOptions) {
checkVersion()
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
const currentSuite = getCurrentSuite()

Expand Down
22 changes: 0 additions & 22 deletions packages/runner/src/version.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CSSEnablerPlugin } from './cssEnabler'
import { CoverageTransform } from './coverageTransform'
import { MocksPlugin } from './mocks'
import { resolveOptimizerConfig } from './utils'
import { VitestResolver } from './vitestResolver'

export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('test')): Promise<VitePlugin[]> {
const userConfig = deepMerge({}, options) as UserConfig
Expand Down Expand Up @@ -208,6 +209,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
? await UIPlugin()
: null,
MocksPlugin(),
VitestResolver(ctx),
]
.filter(notNullish)
}
19 changes: 19 additions & 0 deletions packages/vitest/src/node/plugins/vitestResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Plugin } from 'vite'
import { join } from 'pathe'
import type { Vitest } from '../core'

export function VitestResolver(ctx: Vitest): Plugin {
return {
name: 'vitest:resolve-root',
enforce: 'pre',
async resolveId(id) {
if (id === 'vitest' || id.startsWith('@vitest/')) {
return this.resolve(
id,
join(ctx.config.root, 'index.html'),
{ skipSelf: true },
)
}
},
}
}
2 changes: 2 additions & 0 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EnvReplacerPlugin } from './envReplacer'
import { GlobalSetupPlugin } from './globalSetup'
import { MocksPlugin } from './mocks'
import { resolveOptimizerConfig } from './utils'
import { VitestResolver } from './vitestResolver'

interface WorkspaceOptions extends UserWorkspaceConfig {
root?: string
Expand Down Expand Up @@ -150,5 +151,6 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
CoverageTransform(project.ctx),
GlobalSetupPlugin(project, project.ctx.logger),
MocksPlugin(),
VitestResolver(project.ctx),
]
}
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getColors, getType } from '@vitest/utils'
import { isNodeBuiltin } from 'vite-node/utils'
import { getWorkerState } from '../utils/global'
import { getAllMockableProperties } from '../utils/base'
import { spyOn } from '../integrations/spy'
import type { MockFactory, PendingSuiteMock } from '../types/mocker'
import { spyOn } from '../integrations/spy'
import type { VitestExecutor } from './execute'

const filterPublicKeys = ['__esModule', Symbol.asyncIterator, Symbol.hasInstance, Symbol.isConcatSpreadable, Symbol.iterator, Symbol.match, Symbol.matchAll, Symbol.replace, Symbol.search, Symbol.split, Symbol.species, Symbol.toPrimitive, Symbol.toStringTag, Symbol.unscopables]
Expand Down

0 comments on commit 2deb70a

Please sign in to comment.