Skip to content

Failed to load custom environment from JS file #4237

@paescuj

Description

@paescuj

Describe the bug

The documentation at https://vitest.dev/guide/environment.html#custom-environment states that since v0.34.0 a custom environment can be a valid JS file (instead of a vitest-environment-${name} package).

However, with a configuration like the following

export default defineConfig({
  test: {
    environment: './custom.js',
  },
});

it still tries to load the custom environment from a package:

 MISSING DEP  Can not find dependency 'vitest-environment-./custom.js'

The resolve logic seems to differentiate correctly here:

const packageId = name[0] === '.' || name[0] === '/'
? resolve(root, name)
: await resolvePath(`vitest-environment-${name}`, { url: [root] }) ?? resolve(root, name)

So I'd guess there's an issue in the loader used at the next line:

const pkg = await loader.executeId(normalize(packageId))


Also – at least with the package setup – it seems like the file is preprocessed again, despite the statement from v0.34.0:

Custom environment now should be processed before Vitest can consume it. It means that you cannot specify a path to a TS file or use import paths that should be processed by Vite.

For example, a setup like this works fine: https://github.com/vitest-dev/vitest/blob/main/test/env-custom/vitest-environment-custom/package.json

Related to #4196?

If this is indeed supported again and once this issue here has been addressed, could the documentation be updated to specify a path to a valid JS/TS file?

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-uefbmr

System Info

System:
    OS: macOS 13.5.2
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 9.84 GB / 32.00 GB
    Shell: 5.2.15 - /usr/local/bin/bash
  Binaries:
    Node: 18.18.0 - /usr/local/opt/node@18/bin/node
    npm: 10.1.0 - /usr/local/bin/npm
    pnpm: 8.8.0 - /usr/local/bin/pnpm
  npmPackages:
    vitest: 0.34.6 => 0.34.6

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions