|
1 | 1 | import childProcess, { exec } from 'node:child_process' |
2 | 2 | import timers from 'node:timers' |
3 | | -import { expect, test, vi } from 'vitest' |
| 3 | +import { type SpyInstance, afterEach, beforeEach, describe, expect, test, vi } from 'vitest' |
4 | 4 | import { execDefault, execHelloWorld, execImportAll } from '../src/exec' |
5 | 5 | import { dynamicImport } from '../src/dynamic-import' |
6 | 6 |
|
@@ -28,3 +28,31 @@ test('mocked dynamically imported packages', async () => { |
28 | 28 | expect(mod.default).toHaveProperty('clearInterval') |
29 | 29 | expect(mod.default.clearInterval()).toBe('foo') |
30 | 30 | }) |
| 31 | + |
| 32 | +describe('Math.random', () => { |
| 33 | + describe('mock is restored', () => { |
| 34 | + let spy: SpyInstance |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + spy = vi.spyOn(Math, 'random').mockReturnValue(0.1) |
| 38 | + }) |
| 39 | + afterEach(() => { |
| 40 | + spy.mockRestore() |
| 41 | + }) |
| 42 | + |
| 43 | + test('is mocked', () => { |
| 44 | + expect(Math.random()).toBe(0.1) |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + // This used to make dependencies stuck, e.g. birpc |
| 49 | + describe('mock is not restored and leaks', () => { |
| 50 | + beforeEach(() => { |
| 51 | + vi.spyOn(Math, 'random').mockReturnValue(0.1) |
| 52 | + }) |
| 53 | + |
| 54 | + test('is mocked', () => { |
| 55 | + expect(Math.random()).toBe(0.1) |
| 56 | + }) |
| 57 | + }) |
| 58 | +}) |
0 commit comments