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

fix: compat with frozen Math #3527

Merged
merged 1 commit into from Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions packages/browser/src/client/rpc.ts
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
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