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

Upgrading from 11.0.2 -> 12.0.0: ReferenceError: __dirname is not defined #1069

Closed
dflock opened this issue Sep 15, 2023 · 5 comments · Fixed by #1071
Closed

Upgrading from 11.0.2 -> 12.0.0: ReferenceError: __dirname is not defined #1069

dflock opened this issue Sep 15, 2023 · 5 comments · Fixed by #1071
Assignees
Labels
bug Something isn't working

Comments

@dflock
Copy link

dflock commented Sep 15, 2023

Describe the bug
My vitest tests run fine using happy-dom 11.0.2. After upgrading to 12.0.0, I get this for every test suite:

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
ReferenceError: __dirname is not defined
 ❯ Function.getHappyDOMVersion node_modules/.pnpm/happy-dom@12.0.0/node_modules/happy-dom/src/navigator/NavigatorUtility.ts:14:45
 ❯ new Window node_modules/.pnpm/happy-dom@12.0.0/node_modules/happy-dom/src/window/Window.ts:215:132
 ❯ new GlobalWindow node_modules/.pnpm/happy-dom@12.0.0/node_modules/happy-dom/lib/window/GlobalWindow.js:11:9
 ❯ Object.setup node_modules/.pnpm/vitest@0.34.4_@vitest+ui@0.34.4_happy-dom@12.0.0_lightningcss@1.21.7/node_modules/vitest/dist/vendor-environments.c9c96bf7.js:559:17
 ❯ withEnv node_modules/.pnpm/vitest@0.34.4_@vitest+ui@0.34.4_happy-dom@12.0.0_lightningcss@1.21.7/node_modules/vitest/dist/entry.js:71:15
 ❯ run node_modules/.pnpm/vitest@0.34.4_@vitest+ui@0.34.4_happy-dom@12.0.0_lightningcss@1.21.7/node_modules/vitest/dist/entry.js:95:3
 ❯ run node_modules/.pnpm/vitest@0.34.4_@vitest+ui@0.34.4_happy-dom@12.0.0_lightningcss@1.21.7/node_modules/vitest/dist/worker.js:87:5
 ❯ node_modules/.pnpm/tinypool@0.7.0/node_modules/tinypool/dist/esm/worker.js:109:20

To Reproduce
Steps to reproduce the behavior:

  1. vitest --ui --coverage.enabled=true

Expected behavior
Tests to run fine, as before.

Screenshots
If applicable, add screenshots to help explain your problem.

Device:

  • OS: Windows 10
  • Browser: Running vitest in Windows Terminal
  • Version:

Additional context
Am using these versions of relevant packages:

  • "vitest": "^0.34.4",
  • "vite": "^4.4.9",
  • "happy-dom": "^11.0.2",
  • "@vitest/coverage-v8": "^0.34.4",
  • "@vitest/ui": "^0.34.4",
@dflock dflock added the bug Something isn't working label Sep 15, 2023
@dflock dflock changed the title Upgrading from 11.0.2 -> 12: ReferenceError: __dirname is not defined Upgrading from 11.0.2 -> 12.0.0: ReferenceError: __dirname is not defined Sep 15, 2023
@dflock
Copy link
Author

dflock commented Sep 15, 2023

Downgrading again fixes the error.
What other information can I provide to help figure this out?

@ekwoka
Copy link
Contributor

ekwoka commented Sep 15, 2023

I am having the same issue.

@belgattitude
Copy link

belgattitude commented Sep 15, 2023

Same issue. Not urgent for me but if it helps to debug:

belgattitude/nextjs-monorepo-example#4377

Run yarn --top-level run turbo test-unit
• Packages in scope: nextjs-app
• Running test-unit in [1](https://github.com/belgattitude/nextjs-monorepo-example/actions/runs/6200335412/job/16834797425?pr=4377#step:9:1) packages
• Remote caching enabled
nextjs-app:test-unit
  cache miss, executing c317effcf0d[8](https://github.com/belgattitude/nextjs-monorepo-example/actions/runs/6200335412/job/16834797425?pr=4377#step:9:9)30d1
  Error: nextjs-app#test-unit: command (/home/runner/work/nextjs-monorepo-example/nextjs-monorepo-example/apps/nextjs-app) yarn run test-unit exited (1)
   ERROR  run failed: command  exited (1)
  
   RUN  v0.34.3 /home/runner/work/nextjs-monorepo-example/nextjs-monorepo-example/apps/nextjs-app
  
  
  ⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯
  
  Vitest caught 8 unhandled errors during the test run.
  This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
  
  ⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯
  ReferenceError: __dirname is not defined
   ❯ Function.getHappyDOMVersion ../../node_modules/happy-dom/src/navigator/NavigatorUtility.ts:14:45
   ❯ new Window ../../node_modules/happy-dom/src/window/Window.ts:215:132
   ❯ new GlobalWindow ../../node_modules/happy-dom/lib/window/GlobalWindow.js:11:[9](https://github.com/belgattitude/nextjs-monorepo-example/actions/runs/6200335412/job/16834797425?pr=4377#step:9:10)
   ❯ Object.setup ../../node_modules/vitest/dist/vendor-environments.8eb4d407.js:559:17
   ❯ withEnv ../../node_modules/vitest/dist/entry.js:68:15
   ❯ run ../../node_modules/vitest/dist/entry.js:92:3
   ❯ run ../../node_modules/vitest/dist/worker.js:87:5
   ❯ ../../node_modules/tinypool/dist/esm/worker.js:[10](https://github.com/belgattitude/nextjs-monorepo-example/actions/runs/6200335412/job/16834797425?pr=4377#step:9:11)9:[20](https://github.com/belgattitude/nextjs-monorepo-example/actions/runs/6200335412/job/16834797425?pr=4377#step:9:21)

Worth to mention that this project runs with "type": "module" (in esm). That might explain

Keep the good work. 1000's thx btw.

@tobua
Copy link

tobua commented Sep 15, 2023

__dirname is no longer available with ES Modules in node (type: "module"), it will work in bun though.

However, with the just released v12 a new file using __dirname was added:

public static getHappyDOMVersion(): string {
		const content = FS.readFileSync(Path.join(__dirname, '..', '..', 'package.json')).toString();

process.cwd() might work in this case, otherwise import.meta.url with a check for CJS and using __dirname in this case...

@capricorn86
Copy link
Owner

capricorn86 commented Sep 15, 2023

Thank you @dflock, @ekwoka, @belgattitude and @tobua for reporting! 🙂

Big thanks to @ekwoka for collaborating on a fix! ⭐

The problem has been fixed now.

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v12.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants