diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index e6944c4d192c..60684870324d 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -5,7 +5,7 @@ import type { TestModule, TestSuite } from '../reporters/reported-tasks' import type { TestSpecification } from '../spec' import type { UserConfig, VitestEnvironment, VitestRunMode } from '../types/config' import { mkdirSync, writeFileSync } from 'node:fs' -import { dirname, relative, resolve } from 'pathe' +import { dirname, isAbsolute, relative, resolve } from 'pathe' import { CoverageProviderMap } from '../../utils/coverage' import { createVitest } from '../create' import { FilesNotFoundError, GitNotFoundError, IncludeTaskLocationDisabledError, LocationFilterFileNotFoundError, RangeLocationFilterProvidedError } from '../errors' @@ -317,7 +317,7 @@ function getEnvPackageName(env: VitestEnvironment) { if (env in envPackageNames) { return (envPackageNames as any)[env] } - if (env[0] === '.' || env[0] === '/') { + if (env[0] === '.' || isAbsolute(env)) { return null } return `vitest-environment-${env}` diff --git a/test/cli/test/run-custom-env.test.ts b/test/cli/test/run-custom-env.test.ts index a252167159bc..382127c859de 100644 --- a/test/cli/test/run-custom-env.test.ts +++ b/test/cli/test/run-custom-env.test.ts @@ -1,3 +1,4 @@ +import { createRequire } from 'node:module' import { expect, test } from 'vitest' import { runVitest } from '../../test-utils' @@ -17,3 +18,21 @@ test('correctly runs tests if custom env is a file', async () => { expect(stderr).toBe('') expect(exitCode).toBe(0) }) + +test('correctly runs tests if custom env is an absolute path', async () => { + const require = createRequire(import.meta.url) + + const { stderr, exitCode } = await runVitest({ + root: './fixtures/custom-file-env', + config: false, + environment: require.resolve('vitest-environment-custom'), + environmentOptions: { + custom: { + option: 'custom-option', + }, + }, + }) + + expect(stderr).toBe('') + expect(exitCode).toBe(0) +})