GeezerVison is showing github.com — fix(fakeTimers): force `queueMicrotask` and `nextTick` in `toNotFake`… · vitest-dev/vitest@a47d790
File treeExpand file treeCollapse file tree Open diff view settings packages/vitest/src/integrations/mock
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/config/faketimers.md +1-3Lines changed: 1 addition & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -32,9 +32,7 @@ Mocking `nextTick` is not supported when running Vitest inside `node:child_proce | 32 | 32 | - **Type:** `('setTimeout' | 'clearTimeout' | 'setImmediate' | 'clearImmediate' | 'setInterval' | 'clearInterval' | 'Date' | 'nextTick' | 'hrtime' | 'requestAnimationFrame' | 'cancelAnimationFrame' | 'requestIdleCallback' | 'cancelIdleCallback' | 'performance' | 'queueMicrotask' | 'Intl' | 'Temporal')[]` | 33 | 33 | - **Default:** `[]` | 34 | 34 |
| 35 | | -An array with names of global methods and APIs to keep native. All other available timers will be mocked. For example, to keep `setInterval()` native and mock all other timers, specify this property as `['setInterval']`. | 36 | | - | 37 | | -Mocking `nextTick` is not supported when running Vitest inside `node:child_process` by using `--pool=forks`. When running with `--pool=forks`, Vitest automatically adds `nextTick` to the `toNotFake` array. | | 35 | +An array of global methods and APIs to exclude from fake timers. Vitest always excludes `nextTick` and `queueMicrotask` when this option is used. To fake either API, use `toFake` instead. | 38 | 36 |
| 39 | 37 | ::: warning | 40 | 38 | Using both `toFake` and `toNotFake` together is not supported. | |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/integrations/mock/timers.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -169,6 +169,14 @@ export class FakeTimers { | 169 | 169 | toFake = (Object.keys(this._fakeTimers.timers) as FakeMethod[]) | 170 | 170 | .filter(timer => timer !== 'nextTick' && timer !== 'queueMicrotask') | 171 | 171 | } | | 172 | + else if (toFake === undefined && toNotFake !== undefined) { | | 173 | + // Do not mock timers internally used by node via `toNotFake` | | 174 | + for (const timer of ['nextTick', 'queueMicrotask'] as const) { | | 175 | + if (!toNotFake.includes(timer)) { | | 176 | + toNotFake = [...toNotFake, timer] | | 177 | + } | | 178 | + } | | 179 | + } | 172 | 180 | if (isChildProcess() && toNotFake && !toNotFake.includes('nextTick')) { | 173 | 181 | toNotFake = [...toNotFake, 'nextTick'] | 174 | 182 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/unit/test/fixtures/timers.suite.ts +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -154,7 +154,7 @@ describe('FakeTimers', () => { | 154 | 154 | expect(global.clearImmediate).toBe(origClearImmediate) | 155 | 155 | }) | 156 | 156 |
| 157 | | - it.skipIf(isChildProcess)('mocks process.nextTick when toNotFake does not include nextTick', () => { | | 157 | + it.skipIf(isChildProcess)('does not mock process.nextTick when toNotFake is used without nextTick', () => { | 158 | 158 | const origNextTick = () => {} | 159 | 159 | const global = { | 160 | 160 | Date: FakeDate, | @@ -166,7 +166,21 @@ describe('FakeTimers', () => { | 166 | 166 | } | 167 | 167 | const timers = new FakeTimers({ global, config: { toNotFake: [] } }) | 168 | 168 | timers.useFakeTimers() | 169 | | - expect(global.process.nextTick).not.toBe(origNextTick) | | 169 | + expect(global.process.nextTick).toBe(origNextTick) | | 170 | + }) | | 171 | + | | 172 | + it('does not mock queueMicrotask when toNotFake is used', () => { | | 173 | + const origQueueMicrotask = () => {} | | 174 | + const global = { | | 175 | + Date: FakeDate, | | 176 | + clearTimeout, | | 177 | + queueMicrotask: origQueueMicrotask, | | 178 | + setTimeout, | | 179 | + } | | 180 | + const timers = new FakeTimers({ global, config: { toNotFake: ['Temporal'] } }) | | 181 | + timers.useFakeTimers() | | 182 | + expect(global.queueMicrotask).toBe(origQueueMicrotask) | | 183 | + expect(global.setTimeout).not.toBe(setTimeout) | 170 | 184 | }) | 171 | 185 |
| 172 | 186 | it.runIf(isChildProcess)('does not mock process.nextTick when toNotFake does not include nextTick and is child_process', () => { | |
|