Skip to content

Commit

Permalink
fix: compat with frozen Math (#3527)
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg authored Jun 20, 2023
1 parent 150cfaa commit 0db6709
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 8 deletions.
4 changes: 0 additions & 4 deletions packages/browser/src/client/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ import type {
import type { VitestClient } from '@vitest/ws-client'

const { get } = Reflect
const safeRandom = Math.random

function withSafeTimers(getTimers: typeof getSafeTimers, fn: () => void) {
const { setTimeout, clearTimeout, nextTick, setImmediate, clearImmediate } = getTimers()

const currentSetTimeout = globalThis.setTimeout
const currentClearTimeout = globalThis.clearTimeout
const currentRandom = globalThis.Math.random
const currentNextTick = globalThis.process.nextTick
const currentSetImmediate = globalThis.setImmediate
const currentClearImmediate = globalThis.clearImmediate

try {
globalThis.setTimeout = setTimeout
globalThis.clearTimeout = clearTimeout
globalThis.Math.random = safeRandom
globalThis.process.nextTick = nextTick
globalThis.setImmediate = setImmediate
globalThis.clearImmediate = clearImmediate
Expand All @@ -30,7 +27,6 @@ function withSafeTimers(getTimers: typeof getSafeTimers, fn: () => void) {
finally {
globalThis.setTimeout = currentSetTimeout
globalThis.clearTimeout = currentClearTimeout
globalThis.Math.random = currentRandom
globalThis.setImmediate = currentSetImmediate
globalThis.clearImmediate = currentClearImmediate
nextTick(() => {
Expand Down
4 changes: 0 additions & 4 deletions packages/vitest/src/runtime/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ import {
import { getWorkerState } from '../utils/global'

const { get } = Reflect
const safeRandom = Math.random

function withSafeTimers(fn: () => void) {
const { setTimeout, clearTimeout, nextTick, setImmediate, clearImmediate } = getSafeTimers()

const currentSetTimeout = globalThis.setTimeout
const currentClearTimeout = globalThis.clearTimeout
const currentRandom = globalThis.Math.random
const currentNextTick = globalThis.process.nextTick
const currentSetImmediate = globalThis.setImmediate
const currentClearImmediate = globalThis.clearImmediate

try {
globalThis.setTimeout = setTimeout
globalThis.clearTimeout = clearTimeout
globalThis.Math.random = safeRandom
globalThis.process.nextTick = nextTick
globalThis.setImmediate = setImmediate
globalThis.clearImmediate = clearImmediate
Expand All @@ -30,7 +27,6 @@ function withSafeTimers(fn: () => void) {
finally {
globalThis.setTimeout = currentSetTimeout
globalThis.clearTimeout = currentClearTimeout
globalThis.Math.random = currentRandom
globalThis.setImmediate = currentSetImmediate
globalThis.clearImmediate = currentClearImmediate
nextTick(() => {
Expand Down

0 comments on commit 0db6709

Please sign in to comment.