{"id":"d5337d8dc77e3bff","repo":"vitest-dev/vitest","slug":"process-nexttick-cannot-be-mocked-inside-child-pro","errorCode":null,"errorMessage":"process.nextTick cannot be mocked inside child_process","messagePattern":"process\\.nextTick cannot be mocked inside child_process","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/mock/timers.ts","lineNumber":161,"sourceCode":"      this._clock.uninstall()\n      this._fakingTime = false\n    }\n  }\n\n  useFakeTimers(): void {\n    const fakeDate = this._fakingDate || Date.now()\n    if (this._fakingDate) {\n      this._clock.uninstall()\n      this._fakingDate = null\n    }\n\n    if (this._fakingTime) {\n      this._clock.uninstall()\n    }\n\n    let toFake = this._userConfig?.toFake\n    if (isChildProcess() && toFake?.includes('nextTick')) {\n      throw new Error(\n        'process.nextTick cannot be mocked inside child_process',\n      )\n    }\n\n    let toNotFake = this._userConfig?.toNotFake\n    if (toFake === undefined && toNotFake === undefined) {\n      // Do not mock timers internally used by node by default. It can still be mocked through userConfig.\n      toFake = (Object.keys(this._fakeTimers.timers) as FakeMethod[])\n        .filter(timer => timer !== 'nextTick' && timer !== 'queueMicrotask')\n    }\n    if (isChildProcess() && toNotFake && !toNotFake.includes('nextTick')) {\n      toNotFake = [...toNotFake, 'nextTick']\n    }\n\n    this._clock = this._fakeTimers.install({\n      now: fakeDate,\n      ...this._userConfig,\n      ...(toFake && { toFake }),","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/integrations/mock/timers.ts#L143-L179","documentation":"`process.nextTick` cannot be safely faked inside a Node `child_process` (the `forks` pool runs each test file in a forked process where `process.send` exists). Mocking `nextTick` there breaks the IPC channel that keeps the worker alive and communicating with the main process. Vitest's `FakeTimers.useFakeTimers()` rejects an explicit `toFake` containing `'nextTick'` when `isChildProcess()` is true.","triggerScenarios":"Calling `vi.useFakeTimers({ toFake: ['nextTick', ...] })` (or passing `fakeTimers.toFake` containing `'nextTick'` via config) while running under the `forks` pool, which sets `process.send`.","commonSituations":"Setting `pool: 'forks'` (or relying on it as default in some setups) and globally enabling `fakeTimers.toFake: ['nextTick']`, or copying a timers config that worked under the `threads` pool into a forks run.","solutions":["Switch the pool to `threads` (`--pool=threads` or `pool: 'threads'`) so nextTick mocking is supported.","Remove `'nextTick'` from `toFake` and rely on the default, which already excludes `nextTick` and `queueMicrotask`.","If you must fake nextTick and stay on forks, that combination is unsupported — restructure the test to avoid nextTick."],"exampleFix":"// before (forks pool)\nvi.useFakeTimers({ toFake: ['setTimeout', 'nextTick'] })\n// after (run with --pool=threads)\n// vitest.config.ts\nexport default defineConfig({ test: { pool: 'threads' } })\nvi.useFakeTimers({ toFake: ['setTimeout', 'nextTick'] })","handlingStrategy":"validation","validationCode":"// before enabling nextTick faking, confirm we are NOT in a child process\nconst isChild = typeof process !== 'undefined' && !!process.send\nif (isChild && (config?.toFake ?? []).includes('nextTick')) {\n  throw new Error('nextTick cannot be faked in forks/child_process; use --pool=threads')\n}","typeGuard":"function canFakeNextTick(toFake: string[] = []): boolean {\n  const isChild = typeof process !== 'undefined' && !!process.send\n  return !(isChild && toFake.includes('nextTick'))\n}","tryCatchPattern":null,"preventionTips":["Default to `pool: 'threads'` when nextTick mocking is required.","Leave `toFake` unset so Vitest excludes nextTick/queueMicrotask automatically."],"tags":["timers","fake-timers","forks-pool","nexttick","config"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}