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

Vitest Browser Mode 2.1.4 regression: ReferenceError: process is not defined #6872

Closed
6 tasks done
ezzatron opened this issue Nov 6, 2024 · 11 comments
Closed
6 tasks done
Labels
feat: browser Issues and PRs related to the browser runner pending triage

Comments

@ezzatron
Copy link

ezzatron commented Nov 6, 2024

Describe the bug

When using Vitest Browser Mode with Next.js + the next/link component, there's a regression in Vitest 2.1.4 that produces the following error:

ReferenceError: process is not defined
 ❯ node_modules/next/dist/client/has-base-path.js ../../../../../node_modules/.vite/deps/next_link.js:553:20
 ❯ node_modules/next/dist/shared/lib/router/utils/is-local-url.js ../../../../../node_modules/.vite/deps/next_link.js:579:24
 ❯ node_modules/next/dist/client/resolve-href.js ../../../../../node_modules/.vite/deps/next_link.js:[16](https://github.com/ezzatron/vitest-browser-process-not-defined-repro/actions/runs/11713569546/job/32626585117#step:6:17)58:23
 ❯ node_modules/next/dist/client/link.js ../../../../../node_modules/.vite/deps/next_link.js:2360:24
 ❯ node_modules/next/link.js ../../../../../node_modules/.vite/deps/next_link.js:2732:22

Possibly related to #6842.

Reproduction

See https://github.com/ezzatron/vitest-browser-process-not-defined-repro

You can see the repo working under Vitest 2.1.3 and not working under 2.1.4 here: https://github.com/ezzatron/vitest-browser-process-not-defined-repro/actions/runs/11713569546:

Screenshot 2024-11-07 at 09 32 25

System Info

Not relevant, happens under GHA and locally on my machine, but anyway:

  System:
    OS: macOS 14.7.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 2.04 GB / 32.00 GB
    Shell: 5.9 - /opt/homebrew/bin/zsh
  Binaries:
    Node: 23.1.0 - /opt/homebrew/bin/node
    npm: 10.9.0 - /opt/homebrew/bin/npm
    bun: 1.1.34 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 130.0.6723.116
    Edge: 130.0.2849.68
    Safari: 18.1
  npmPackages:
    @vitejs/plugin-react: ^4.3.3 => 4.3.3
    @vitest/browser: 2.1.4 => 2.1.4
    vitest: 2.1.4 => 2.1.4

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 6, 2024

process not available on browser mode is intended. Can you check how next/link uses process?

@hi-ogawa hi-ogawa added the feat: browser Issues and PRs related to the browser runner label Nov 6, 2024
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 6, 2024

Hmm, I remember they have many process.env defines, so you might need to this manually https://github.com/vercel/next.js/blob/d8c1875800903542bd4e89116f4c8aa32cc853bf/packages/next/src/build/webpack/plugins/define-env-plugin.ts#L257 or you can try catch all process.env for now.

export default defineConfig({
  define: {
    'process.env': JSON.stringify({})
  }
})

This should give mostly same behavior as v2.1.3.

@ezzatron
Copy link
Author

ezzatron commented Nov 7, 2024

Hmm, I remember they have many process.env defines

Yes, I believe the offending line is this one: https://github.com/vercel/next.js/blob/d8c1875800903542bd4e89116f4c8aa32cc853bf/packages/next/src/client/has-base-path.ts#L3

@ezzatron
Copy link
Author

ezzatron commented Nov 7, 2024

So I guess this is because of #6718 which landed in 2.1.4?

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 7, 2024

Yes, that PR. there shouldn't be process nor process.env by default on browser testing, so it's working intended.

@ezzatron
Copy link
Author

ezzatron commented Nov 7, 2024

No worries, it seems like explicitly defining process.env should work fine as a workaround. Are there any docs that should be updated to clarify this? I guess there's probably nothing about specifically using Vitest Browser Mode with Next.js yet.

@ezzatron ezzatron closed this as completed Nov 7, 2024
@ezzatron
Copy link
Author

ezzatron commented Nov 7, 2024

Sorry, I spoke too soon. That fix only works if you're not using Vitest workspaces. When I went to apply it to my actual project, the define config doesn't help, regardless of whether it's in vitest.config.ts or vitest.workspace.ts.

I've updated the reproduction to show this: https://github.com/ezzatron/vitest-browser-process-not-defined-repro/actions/runs/11714232660

@ezzatron ezzatron reopened this Nov 7, 2024
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 7, 2024

Okay, that could be a different workspace issue. You need to use either extends: './vitest.config.ts' like this:

// vitest.config.ts
import react from "@vitejs/plugin-react";
import { defineConfig } from "vitest/config";

export default defineConfig({
  plugins: [react()],
  define: {
    "process.env": JSON.stringify({}),
  },
  test: {
    watch: false,
  },
});


// vitest.workspace.ts
import react from "@vitejs/plugin-react";
import { defineProject, defineWorkspace } from "vitest/config";

export default defineWorkspace([
  {
    extends: './vitest.config.ts',
    test: {
      name: "project",
      browser: {
        enabled: true,
        headless: true,
        provider: "playwright",
        name: "chromium",
      },
    },
  },
]);

or point to a dedicated config file from workspace

// vitest.config.browser.ts
import react from "@vitejs/plugin-react";
import { defineConfig } from "vitest/config";

export default defineConfig({
  plugins: [react()],
  define: {
    "process.env": JSON.stringify({}),
  },
  test: {
    name: "project",
    browser: {
      enabled: true,
      headless: true,
      provider: "playwright",
      name: "chromium",
    },
  },
});

// vitest.workspace.ts
export default defineWorkspace([
  './vitest.config.browser.ts'
]);

I'm not exactly sure if this is a bug or not. We know there is some limitations regarding how we can pass config object to browser mode and things work better if there is a dedicated config file for browser mode vite server to consume. Probably similar to #5015

@ezzatron
Copy link
Author

ezzatron commented Nov 7, 2024

Thank you! Can confirm that using extends seems to work for my use case.

@ezzatron ezzatron closed this as completed Nov 7, 2024
@hi-ogawa
Copy link
Contributor

hi-ogawa commented Nov 7, 2024

I'm investigating further and I'm not sure what I said in #6872 (comment) is correct. It looks like there's something odd going on specifically to define and process.env.

(But again, having a dedicated config file for browser mode should work more consistent with normal Vite dev usage.)

@ezzatron
Copy link
Author

ezzatron commented Nov 7, 2024

(But again, having a dedicated config file for browser mode should work more consistent with normal Vite dev usage.)

Sounds good, will keep an eye out for it. Appreciate the help 🙏

@github-actions github-actions bot locked and limited conversation to collaborators Nov 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: browser Issues and PRs related to the browser runner pending triage
Projects
None yet
Development

No branches or pull requests

2 participants