Skip to content

Commit

Permalink
fix: prevent birpc timeouts when Math.random mock is not restored (
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed May 29, 2023
1 parent 4e996ae commit cd5d58b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/ui/package.json
Expand Up @@ -69,7 +69,7 @@
"@vitest/ws-client": "workspace:*",
"@vueuse/core": "^10.1.2",
"ansi-to-html": "^0.7.2",
"birpc": "0.2.11",
"birpc": "0.2.12",
"codemirror": "^5.65.13",
"codemirror-theme-vars": "^0.1.2",
"cypress": "^12.11.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/package.json
Expand Up @@ -174,7 +174,7 @@
"@types/micromatch": "^4.0.2",
"@types/prompts": "^2.4.4",
"@types/sinonjs__fake-timers": "^8.1.2",
"birpc": "0.2.11",
"birpc": "0.2.12",
"chai-subset": "^1.6.0",
"cli-truncate": "^3.1.0",
"event-target-polyfill": "^0.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/ws-client/package.json
Expand Up @@ -39,7 +39,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"birpc": "0.2.11",
"birpc": "0.2.12",
"flatted": "^3.2.7",
"ws": "^8.13.0"
},
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion test/core/test/mock-internals.test.ts
@@ -1,6 +1,6 @@
import childProcess, { exec } from 'node:child_process'
import timers from 'node:timers'
import { expect, test, vi } from 'vitest'
import { type SpyInstance, afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { execDefault, execHelloWorld, execImportAll } from '../src/exec'
import { dynamicImport } from '../src/dynamic-import'

Expand Down Expand Up @@ -28,3 +28,31 @@ test('mocked dynamically imported packages', async () => {
expect(mod.default).toHaveProperty('clearInterval')
expect(mod.default.clearInterval()).toBe('foo')
})

describe('Math.random', () => {
describe('mock is restored', () => {
let spy: SpyInstance

beforeEach(() => {
spy = vi.spyOn(Math, 'random').mockReturnValue(0.1)
})
afterEach(() => {
spy.mockRestore()
})

test('is mocked', () => {
expect(Math.random()).toBe(0.1)
})
})

// This used to make dependencies stuck, e.g. birpc
describe('mock is not restored and leaks', () => {
beforeEach(() => {
vi.spyOn(Math, 'random').mockReturnValue(0.1)
})

test('is mocked', () => {
expect(Math.random()).toBe(0.1)
})
})
})

0 comments on commit cd5d58b

Please sign in to comment.