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

Mocking Math.random and not restoring makes vitest hang up when coverage is enabled #903

Closed
6 tasks done
sapphi-red opened this issue Mar 7, 2022 · 4 comments
Closed
6 tasks done

Comments

@sapphi-red
Copy link
Contributor

Describe the bug

When you

  • enabled coverage
  • mock Math.random and not restoring the original function after test

vitest hangs up.
If you run the same code with jest, it finishes.

const t = typeof vi !== 'undefined' ? vi : jest

test('Math.random mock', () => {
  // const original = Math.random
  const mocked = t.fn(() => 0.1);
  Math.random = mocked
  // Math.random = original
});

When you restore Math.random it will work. (uncomment line 4 & 7)

I think it does not need to end the test successfully, but at least fail with timeout.
Because it is hard to find which test is hanging up.

Reproduction

https://gist.github.com/sapphi-red/d3454a362c3b66e3654918516c808d7e

  1. Download zip and extract
  2. npm ci
  3. npm run jest runs successfully
  4. npm run vitest never ends

(stackblitz did not work)

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 12.61 GB / 31.93 GB
  Binaries:
    Node: 16.14.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.1.3 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 98.0.4758.102
    Edge: Spartan (44.19041.1266.0), Chromium (99.0.1150.30)
    Internet Explorer: 11.0.19041.1202
  npmPackages:
    vitest: ^0.5.9 => 0.5.9

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

We cannot guarantee that no one uses global APIs when running tests. Just restore it to original after each test:

afterEach(() => {
  Math.random = originalRandom
})

@sapphi-red
Copy link
Contributor Author

sapphi-red commented Mar 8, 2022

I thought the environment which is running tests is isolated from the environment which is collecting coverage.
So even if a test is breaking the environment, it would not effect the main environment.

But actually the test did not fail with timeout. Is this intended? If it is intended, I am ok with closing this issue.

@sheremet-va
Copy link
Member

They are run in the same env (not isolated, like jest does with vm module). Vitest provides isolation through workers, unlike jest, who uses it only to run tests in parallel.

Currently we are not planning on changing the way tests are run, because vm has a poor ESM support (that is why jest is so bad at ESM), but we are ESM-first and it is simply not good enough, so we are waiting for the Node team to make a better support.

@sapphi-red
Copy link
Contributor Author

Thank you for the explanation! I understand.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 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

No branches or pull requests

2 participants