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

process.stdout is undefined in many cases on Windows OS and Vitest uses it in few places #1292

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

Comments

@ghiscoding
Copy link
Contributor

ghiscoding commented May 11, 2022

Describe the bug

process.stdout is often seem to be causing issues on Windows and is often undefined in many cases on Windows OS and Vitest is trying to get TTY width in a few areas by calling process.stdout.columns (and maybe rows too) and this throws an error in Windows. This bug is very similar to another bug #1231 that I opened that got fixed not long ago for the same reason. So as far as I can see process.stdout really seems to be a problem on Windows.

I also found this SO asking similar question:
SO - How can I access stdout.columns in Node on Windows?

and other GitHub projects with similar issue
Facebook Flow - Support for process.stdout.columns

  • from this issue, it seems that process.stdout.columns (and rows) is only available when process.stdout.isTTY is true

Searching for process.stdout in Vitest code is returning 7 places, 1 of them got fixed on Windows by calling console._stdout as mentioned in #1231 and fixed in #1235. Considering this, would it be possible to find alternative to process.stdout to avoid Vitest itself throwing errors on Windows platform?

Reproduction

This simple unit test on Windows will throw a Vitest error coming from process.stdout.columns (where process.stdout is undefined and throws that it cannot read "columns" from undefined)

it('should test a spy', async () => {
    const obj = {
      getUsers: () => Promise.resolve({ id: 123, name: 'John' }),
    };

    const spy = vi.spyOn(obj, 'getUsers');

    expect(await obj.getUsers()).toEqual({ id: 123, name: 'John' });
    expect(spy).toHaveBeenCalled();
    expect(spy).toHaveBeenCalledWith({ id: 999, name: 'John' });
  });

will throw this Vitest error

 FAIL  src/stores/__tests__/userStore.spec.ts > User Store > should test a spy
TypeError: Cannot read properties of undefined (reading 'columns')
 ❯ src/stores/__tests__/userStore.spec.ts:46:16
     44|     expect(await obj.getUsers()).toEqual({ id: 123, name: 'John' });
     45|     expect(spy).toHaveBeenCalled();
     46|     expect(spy).toHaveBeenCalledWith({ id: 999, name: 'John' });
       |                ^
     47|   });
     48| });

if I go in Vitest internal code and change process.stdout.columns to process.stdout && process.stdout.columns then it works and I see the full error

 FAIL  src/stores/__tests__/userStore.spec.ts > User Store > should test a spy
AssertionError: expected "getUsers" to be called with arguments: [ { id: 999, name: 'John' } ]expected "getUsers" to be called with arguments: [ { id: 999, name: 'John' } ]

Received:
    1st getUsers call:

  - Array [
  -   Object {
  -     "id": 999,
  -     "name": "John",
  -   },
  - ]
  + Array []


Number of calls: 1

 ❯ src/stores/__tests__/userStore.spec.ts:46:16
     44|     expect(await obj.getUsers()).toEqual({ id: 123, name: 'John' });
     45|     expect(spy).toHaveBeenCalled();
     46|     expect(spy).toHaveBeenCalledWith({ id: 999, name: 'John' });
       |                ^
     47|   });
     48| });

For my use case, the error seems to come from formatLine

export function formatLine(line: string, outputTruncateLength?: number) {
return cliTruncate(line, (outputTruncateLength ?? (process.stdout.columns || 80)) - 4)
}

changing it to this line no longer throws from Vitest and allows me to see the real error

return cliTruncate(line, (outputTruncateLength ?? (process.stdout && process.stdout.columns || 80)) - 4)

Again this is really similar to the error I had reported earlier in #1231

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

@ghiscoding ghiscoding changed the title process.stdout is undefined in most cases on Windows OS and Vitest uses it in many places process.stdout is undefined in many cases on Windows OS and Vitest uses it in few places May 11, 2022
chaii3 pushed a commit to chaii3/vitest that referenced this issue May 13, 2022
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
@github-actions github-actions bot locked and limited conversation to collaborators Jun 18, 2023
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.

1 participant