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 fails to run with browser mode, displaying a blank screen #3434

Closed
6 tasks done
hamza1311 opened this issue May 23, 2023 · 14 comments · Fixed by #3580
Closed
6 tasks done

Vitest fails to run with browser mode, displaying a blank screen #3434

hamza1311 opened this issue May 23, 2023 · 14 comments · Fixed by #3580
Labels
feat: browser Issues and PRs related to the browser runner
Milestone

Comments

@hamza1311
Copy link

Describe the bug

Running vitest results in the following error in browser console:

Uncaught (in promise) SyntaxError: The requested module '/node_modules/@vitest/utils/node_modules/pretty-format/build/index.js?v=aa51a3dc' does not provide an export named '__vi_inject__' (at index.js?v=aa51a3dc:3:10)

Only a blank white page is displayed.

Reproduction

  1. Git clone https://github.com/hamza1311/vitest-bug-repro
  2. npm install
  3. npx vitest
  4. Observe console of the browser

System Info

System:
    OS: Linux 6.3 Arch Linux
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i5-12500H
    Memory: 3.47 GB / 15.29 GB
    Container: Yes
    Shell: 3.6.1 - /usr/bin/fish
  Binaries:
    Node: 16.19.1 - /run/user/1000/fnm_multishells/591681_1684781270672/bin/node
    Yarn: 1.22.19 - /run/user/1000/fnm_multishells/591681_1684781270672/bin/yarn
    npm: 8.19.3 - /run/user/1000/fnm_multishells/591681_1684781270672/bin/npm
  Browsers:
    Firefox: 113.0.1
  npmPackages:
    @vitest/browser: ^0.31.1 => 0.31.1 
    @vitest/ui: ^0.31.1 => 0.31.1 
    vite: ^4.3.8 => 4.3.8 
    vitest: ^0.31.1 => 0.31.1

Used Package Manager

npm

Validations

@van-fe
Copy link

van-fe commented May 24, 2023

I don't think that the browser mode's contributors have run their codes practically. This mode never run correctly on my machine after it provides.

@sheremet-va
Copy link
Member

I don't think that the browser mode's contributors have run their codes practically. This mode never run correctly on my machine after it provides.

There is a test for a browser mode that is used to test this feature manually, and it also runs in CI. There is no reason to blame anyone. If you don't have anything constructive to say, just don't say anything.

@enjikaka
Copy link

enjikaka commented May 31, 2023

Getting the same trying to set up vitest. The following is thrown in the test browser console:

Uncaught (in promise) SyntaxError: The requested module '/@fs/Users/xx/dev/xx/node_modules/.pnpm/pretty-format@27.5.1/node_modules/pretty-format/build/index.js?v=e4b3aab2' does not provide an export named '__vi_inject__'

@AriPerkkio
Copy link
Member

The minimal reproduction case here works if you install @vitest/utils explicitly. This can be used as work-around for now. The root cause still needs to be resolved in @vitest/browser package though.

@AriPerkkio AriPerkkio added the feat: browser Issues and PRs related to the browser runner label Jun 1, 2023
AriPerkkio added a commit to AriPerkkio/aria-live-capture that referenced this issue Jun 1, 2023
@enjikaka
Copy link

enjikaka commented Jun 1, 2023

The minimal reproduction case here works if you install @vitest/utils explicitly. This can be used as work-around for now. The root cause still needs to be resolved in @vitest/browser package though.

Thanks. Installing that utils package made it run for me.

@sheremet-va sheremet-va added the bug label Jun 2, 2023
@jd-solanki
Copy link

Installing vitest utils doesn't work for me 🤷🏻‍♂️

@jd-solanki
Copy link

Hey team 👋🏻

I haven't written any tests since I encoutered this issue (maybe a month). Is this planned anytime soon? This will help me decide should I move to jsdom & disable browser mode.

Thanks.

@AriPerkkio
Copy link
Member

In @vitest/browser@0.32.0 the error message has changed to new one:

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
ReferenceError: exports is not defined
 ❯ http:/localhost:63315/node_modules/.pnpm/pretty-format@27.5.1/node_modules/pretty-format/build/index.js?v=d4e0d767:3:23

Now browser mode is trying to load CJS version of pretty-format into browser and fails to run it.

The http://localhost:63315/node_modules/.pnpm/@vitest+utils@0.32.0/node_modules/@vitest/utils/dist/index.js file contains import "/node_modules/.pnpm/pretty-format@27.5.1/node_modules/pretty-format/build/index.js?v=6111ce5f" which loads http://localhost:63315/node_modules/.pnpm/pretty-format@27.5.1/node_modules/pretty-format/build/index.js as CJS:

'use strict';

Object.defineProperty(exports, '__esModule', {
//                   ^^^^^^^^
// ReferenceError: exports is not defined
    value: true
});
exports.default = exports.DEFAULT_OPTIONS = void 0;
exports.format = format;
exports.plugins = void 0;

var _ansiStyles = _interopRequireDefault(require('ansi-styles'));
...

This cannot be reproduced in Vitest monorepo but using the packages in separate project always runs into this. Installing @vitest/utils as work-around still works though.

@NathanVss
Copy link

In @vitest/browser@0.32.0 the error message has changed to new one:

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
ReferenceError: exports is not defined
 ❯ http:/localhost:63315/node_modules/.pnpm/pretty-format@27.5.1/node_modules/pretty-format/build/index.js?v=d4e0d767:3:23

Now browser mode is trying to load CJS version of pretty-format into browser and fails to run it.

The http://localhost:63315/node_modules/.pnpm/@vitest+utils@0.32.0/node_modules/@vitest/utils/dist/index.js file contains import "/node_modules/.pnpm/pretty-format@27.5.1/node_modules/pretty-format/build/index.js?v=6111ce5f" which loads http://localhost:63315/node_modules/.pnpm/pretty-format@27.5.1/node_modules/pretty-format/build/index.js as CJS:

'use strict';

Object.defineProperty(exports, '__esModule', {
//                   ^^^^^^^^
// ReferenceError: exports is not defined
    value: true
});
exports.default = exports.DEFAULT_OPTIONS = void 0;
exports.format = format;
exports.plugins = void 0;

var _ansiStyles = _interopRequireDefault(require('ansi-styles'));
...

This cannot be reproduced in Vitest monorepo but using the packages in separate project always runs into this. Installing @vitest/utils as work-around still works though.

I have the exact same error but installing @vitest/utils as a devDependency does not solve the problem on my side. I tried to delete node_modules and yarn.lock to double-check but the error is still occurring 😕

@AriPerkkio
Copy link
Member

This is fixed and released in vitest@0.32.2 and @vitest/browser@0.32.2. The @vitest/utils package is no longer needed to be installed as work-around.

@NathanVss
Copy link

This is fixed and released in vitest@0.32.2 and @vitest/browser@0.32.2. The @vitest/utils package is no longer needed to be installed as work-around.

Sadly those are the versions installed on my side with the bug still ocurring

$ yarn list | grep vitest

│  ├─ @vitest/browser@0.32.2
│  ├─ @vitest/coverage-c8@0.32.2
│  ├─ @vitest/ui@0.32.2
│  ├─ vitest-fetch-mock@0.2.2
│  ├─ vitest@0.32.2
├─ @vitest/browser@0.32.2
├─ @vitest/coverage-c8@0.32.2
├─ @vitest/expect@0.32.2
│  ├─ @vitest/spy@0.32.2
│  ├─ @vitest/utils@0.32.2
├─ @vitest/runner@0.32.2
│  ├─ @vitest/utils@0.32.2
├─ @vitest/snapshot@0.32.2
├─ @vitest/spy@0.32.2
├─ @vitest/ui@0.32.2
│  ├─ @vitest/utils@0.32.2
├─ @vitest/utils@0.32.2
├─ vitest-fetch-mock@0.2.2
├─ vitest@0.32.2
│  ├─ @vitest/expect@0.32.2
│  ├─ @vitest/runner@0.32.2
│  ├─ @vitest/snapshot@0.32.2
│  ├─ @vitest/spy@0.32.2
│  ├─ @vitest/utils@0.32.2

@userquin
Copy link
Member

@NathanVss can you provide a minimal repro using yarn?

@getflourish
Copy link

Came here because I’ve installed vitest into a fresh vite project and can’t run any test:
Error: Your application tried to access ansi-styles, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.

@reekystive
Copy link

Add this two dependencies to project package.json resolved my issue.

"pretty-format": "^29.5.0",
"diff-sequences": "^29.4.3"

I'm using vitest 0.32.4.

Without pretty-format I'm getting:

ReferenceError: exports is not defined
 ❯ <...>/node_modules/@vitest/utils/node_modules/pretty-format/build/index.js?v=4c9d75d5:3:23

Without diff-sequences I'm getting:

SyntaxError: The requested module '<...>/node_modules/@vitest/utils/node_modules/diff-sequences/build/index.js?v=4c9d75d5' does not provide an export named '__vi_inject__'

This only happens while I'm adding vitest to an existing project (that project is using yarn). If I start a new project, It seems there's no any issue. So I think maybe It's something related to dependencies' version.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 19, 2023
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
Projects
None yet
10 participants