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

stdout.write is undefined on Windows when called from try/catch #1231

Closed
6 tasks done
ghiscoding opened this issue May 4, 2022 · 0 comments · Fixed by #1235
Closed
6 tasks done

stdout.write is undefined on Windows when called from try/catch #1231

ghiscoding opened this issue May 4, 2022 · 0 comments · Fixed by #1235

Comments

@ghiscoding
Copy link
Contributor

ghiscoding commented May 4, 2022

Describe the bug

On a Vue 3 + Vite + Vitest project, when an error is thrown from any of my unit tests it goes into the try/catch from the collect.ts file, it tries to use process.stdout.write but on my Windows platform stdout seems to always be undefined and so I never really see my code error but instead I see Vitest error of stdout being undefined. So it's really hard to work from my hand when I can't see the real error. The error thrown from Vitest itself comes from process.stdout.write('\0') from this line

https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/runtime/collect.ts#L58-L65

if I add this line to Vitest codebase, I see it as undefined and is most probably a Windows platform specific problem

console.log(`process.stdout >>> ${typeof process.stdout}`);
### display: process.stdout >>> undefined

Fix

I found a fix that works on Windows and we can simply use console._stdout and that solution is used by WinstonJS in their code base which you can find at this line in which they say the following:

"Node.js maps process.stdout to console._stdout."

with that in mind we can do this quick change:

catch (e) {
  //...
  // not sure thy, this line is needed to trigger the error
-  process.stdout.write('\0')
+  if (console._stdout) {
+   // Node.js maps `process.stdout` to `console._stdout`.
+    console._stdout.write('\0')
+  } else {
+    process.stdout.write('\0')
+  }
}

and voilà, I'm finally able see my own error instead of Vitest internal code complaining about stdout being undefined.

I tried to create a PR with the fix but I'm having problem with pnpm so, can someone apply this fix and create a PR for me? 🙏
Thank you.

Reproduction

The error I'm getting on Windows platform comes from Vitest code itself and throws before it has a chance to show the real error:

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Vitest catched 1 unhandled error during the test run. This might cause false positive tests.
Please, resolve all the errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯TypeError: Cannot read properties of undefined (reading 'write')
 ❯ collectTests node_modules/vitest/dist/vendor-entry.17835032.js:682:22
    680|       //   process.stdout.write("\0");
    681|       // }
    682|       process.stdout.write("\0");
       |                      ^
    683|     }
    684|     calculateHash(file);
 ❯ async startTests node_modules/vitest/dist/vendor-entry.17835032.js:961:17
 ❯ async node_modules/vitest/dist/vendor-entry.17835032.js:992:7
 ❯ async withEnv node_modules/vitest/dist/vendor-entry.17835032.js:529:5
 ❯ async run node_modules/vitest/dist/vendor-entry.17835032.js:991:5
 ❯ async ../../../file:/C:/sourcecode/PSX-Selector-greener/node_modules/tinypool/dist/esm/worker.js:96:20

with console._stdout Fix (shown in previous paragraph), I can finally see the real error coming from my code instead of Vites's internal code and that becomes a lot more useful :)

SyntaxError: Unexpected token 'export'
 ❯ Object.compileFunction ../../../node:vm:352:18

Module C:\sourcecode\PSX-Selector-greener\node_modules\@bundled-es-modules\axios\axios.js:2545 seems to be an ES Module but shipped in a CommonJS package. You might want to create an issue 
to the package "C:\sourcecode\PSX-Selector-greener\node_modules\@bundled-es-modules\axios\axios.js:2545" asking them to ship the file in .mjs extension or add "type": "module" in their package.json.

As a temporary workaround you can try to inline the package by updating your config:

// vitest.config.js
export default {
  test: {
    deps: {
      inline: [
        "C:\sourcecode\PSX-Selector-greener\node_modules\@bundled-es-modules\axios\axios.js:2545"
      ]
    }
  }
}

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/7]⎯

System Info

System:
    OS: Windows 10 10.0.19043
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 13.19 GB / 31.75 GB
  Binaries:
    Node: 16.13.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.15 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 8.1.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 100.0.4896.127
    Edge: Spartan (44.19041.1266.0), Chromium (101.0.1210.32)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    vite: ^2.9.7 => 2.9.5
    vitest: workspace:* => 0.10.2

Used Package Manager

yarn

Validations

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants