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

fix: works with yarn pnp #1667

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
3 changes: 1 addition & 2 deletions packages/vitest/src/integrations/env/edge-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { importModule } from 'local-pkg'
import type { Environment } from '../../types'
import { populateGlobal } from './utils'

export default <Environment>({
name: 'edge-runtime',
async setup(global) {
const { EdgeVM } = await importModule('@edge-runtime/vm') as typeof import('@edge-runtime/vm')
const { EdgeVM } = await import('@edge-runtime/vm')
const vm = new EdgeVM({
extend: (context) => {
context.global = context
Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/integrations/env/happy-dom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { importModule } from 'local-pkg'
import type { Environment } from '../../types'
import { populateGlobal } from './utils'

Expand All @@ -7,7 +6,7 @@ export default <Environment>({
async setup(global) {
// happy-dom v3 introduced a breaking change to Window, but
// provides GlobalWindow as a way to use previous behaviour
const { Window, GlobalWindow } = await importModule('happy-dom') as typeof import('happy-dom')
const { Window, GlobalWindow } = await import('happy-dom')
const win = new (GlobalWindow || Window)()

const { keys, originals } = populateGlobal(global, win, { bindFunctions: true })
Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/integrations/env/jsdom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { importModule } from 'local-pkg'
import type { Environment, JSDOMOptions } from '../../types'
import { populateGlobal } from './utils'

Expand All @@ -10,7 +9,7 @@ export default <Environment>({
JSDOM,
ResourceLoader,
VirtualConsole,
} = await importModule('jsdom') as typeof import('jsdom')
} = await import('jsdom')
const {
html = '<!DOCTYPE html>',
userAgent,
Expand Down
10 changes: 7 additions & 3 deletions packages/vitest/src/node/cli-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'pathe'
import type { UserConfig as ViteUserConfig } from 'vite'
import { envPackageNames } from '../integrations/env'
import type { UserConfig } from '../types'
Expand All @@ -20,7 +21,10 @@ export async function startVitest(cliFilters: string[], options: CliOptions, vit
if (options.run)
options.watch = false

if (!await ensurePackageInstalled('vite')) {
// this shouldn't affect _application root_ that can be changed inside config
const root = resolve(options.root || process.cwd())

if (!await ensurePackageInstalled('vite', root)) {
process.exitCode = 1
return false
}
Expand All @@ -31,15 +35,15 @@ export async function startVitest(cliFilters: string[], options: CliOptions, vit
const ctx = await createVitest(options, viteOverrides)

if (ctx.config.coverage.enabled) {
if (!await ensurePackageInstalled('c8')) {
if (!await ensurePackageInstalled('c8', root)) {
process.exitCode = 1
return false
}
}

if (ctx.config.environment && ctx.config.environment !== 'node') {
const packageName = envPackageNames[ctx.config.environment]
if (!await ensurePackageInstalled(packageName)) {
if (!await ensurePackageInstalled(packageName, root)) {
process.exitCode = 1
return false
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
let haveStarted = false

async function UIPlugin() {
await ensurePackageInstalled('@vitest/ui')
await ensurePackageInstalled('@vitest/ui', ctx.config?.root || options.root || process.cwd())
return (await import('@vitest/ui')).default(options.uiBase)
}

Expand Down
6 changes: 4 additions & 2 deletions packages/vitest/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ export function getFullName(task: Task) {

export async function ensurePackageInstalled(
dependency: string,
promptInstall = !process.env.CI && process.stdout.isTTY,
root: string,
) {
if (isPackageExists(dependency))
if (isPackageExists(dependency, { paths: [root] }))
return true

const promptInstall = !process.env.CI && process.stdout.isTTY

process.stderr.write(c.red(`${c.inverse(c.red(' MISSING DEP '))} Can not find dependency '${dependency}'\n\n`))

if (!promptInstall)
Expand Down