Skip to content

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

@sapphi-red

Description

@sapphi-red

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions