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

console.log doens't work when there is an infinite loop after it. #1405

Closed
6 tasks done
searene opened this issue May 31, 2022 · 6 comments · Fixed by #4786
Closed
6 tasks done

console.log doens't work when there is an infinite loop after it. #1405

searene opened this issue May 31, 2022 · 6 comments · Fixed by #4786
Labels
enhancement New feature or request pr welcome

Comments

@searene
Copy link

searene commented May 31, 2022

Describe the bug

I noticed that console.log doesn't print anything when there's an infinite loop after it. Here is the test:

import { describe, it, expect} from 'vitest';

describe('add', () => {
  it ('1 + 1', () => {
    console.log("abc");
    while (true) {

    }
    expect(1 + 1).toBe(2);
  })
})

Here is the output after running yarn run vitest in the terminal

yarn run v1.22.18
$ /Users/joey/WebstormProjects/vitest-ext-basic-example/node_modules/.bin/vitest

 DEV  v0.12.10 /Users/joey/WebstormProjects/vitest-ext-basic-example

 · add.test.ts (1)

As you can see, abc isn't pointed out.

It should print something because sometimes we write infinite loop by accident, and we want to know where the error is. console.log gives a way to inspect the reason.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-mrqmmn?file=test%2Fbasic.test.ts

System Info

System:
    OS: macOS 12.4
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Memory: 563.12 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.2.0 - ~/.nvm/versions/node/v18.2.0/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.9.0 - ~/.nvm/versions/node/v18.2.0/bin/npm
  Browsers:
    Chrome: 101.0.4951.64
    Safari: 15.5
  npmPackages:
    vite: ^2.9.9 => 2.9.9 
    vitest: ^0.12.6 => 0.12.10

Used Package Manager

yarn

Validations

@sheremet-va
Copy link
Member

sheremet-va commented May 31, 2022

Yes, this is because logging is delegated to the main process on the last tick of the event loop, but infinite loop prevents us from reaching it.

@ilovedesert001
Copy link

Yes, this is because logging is delegated to the main process on the last tick of the event loop, but infinite loop prevents us from reaching it.

is there any way to disable this process when debugging ?

@sheremet-va
Copy link
Member

is there any way to disable this process when debugging ?

No

@emersonthis
Copy link

Even if the logging output isn't available, it seems like the infinite looping should be constrained by the timeout value. So the expected behavior would be that the loop runs for as long as the timeout value allows and then fails. Does this make sense? Would this be something easier to implement?

@sheremet-va
Copy link
Member

is there any way to disable this process when debugging ?

We can probably add an option to not intercept console.log here:

export async function setupConsoleLogSpy() {

@sheremet-va sheremet-va added enhancement New feature or request pr welcome labels May 30, 2023
@hi-ogawa
Copy link
Contributor

is there any way to disable this process when debugging ?

I think you can patch globalThis.console back to direct stdout writing like this:

https://stackblitz.com/edit/vitest-dev-vitest-aff1ys?file=repro.test.ts

import { it, beforeAll } from 'vitest';

beforeAll(async () => {
  const { Console } = await import("node:console");
  globalThis.console = new Console(process.stdout, process.stderr);
});

it ('repro', () => {
  console.log("@@ before-loop");
  let i = 0;
  while (true) {
    console.log("@@ inside-loop", i++);
  }
})

One caveat is that --pool=threads seems to have a problem with flushing output in obvious infinite-loop like above, so it can print only "before-loop" part of logs.
I tested with --pool=forks and it can print "inside-loop" logs.

(On stackblitz, the terminal gets stuck sometimes, so I also tested locally on my PC and the behavior regarding --pool seems to be same).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request pr welcome
Projects
None yet
5 participants