Migrate from sinon to Vitest mocking utilities #20352
Draft
+1,606
−1,602
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Replaces sinon with Vitest's native mocking utilities across the entire codebase. Consolidates testing infrastructure to a single mocking library.
Changes
Test utilities:
spy()→vi.fn()stub()→vi.fn().mockReturnValue()/vi.spyOn()useFakeTimers()→vi.useFakeTimers()+vi.setSystemTime()sinon.restore()→vi.restoreAllMocks()SinonSpytype →MockInstanceAssertion conversions:
.callCount→toHaveBeenCalledTimes().calledOnce→toHaveBeenCalledOnce().called→toHaveBeenCalled().lastCall.args[n]→.mock.calls[mock.calls.length - 1][n].lastCall.firstArg→.mock.calls[mock.calls.length - 1][0].firstCall.args[n]→.mock.calls[0][n].args[n]→.mock.calls[n].resetHistory()→.mockClear()sinonAssert.callOrder(spy1, spy2)→expect(spy1.mock.invocationCallOrder[0]).toBeLessThan(spy2.mock.invocationCallOrder[0])Dependencies:
sinonand@types/sinonfrom devDependenciesExample
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
iojs.orgcurl -q --fail --compressed -L -s REDACTED -o -(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Convert all usages of sinon to Vitest's mocking utilities
Overview
Migrate the entire codebase from sinon to Vitest's built-in mocking utilities to consolidate testing tools and leverage Vitest's native capabilities.
Files to Update
1. Test Utilities
test/utils/helperFn.tsimport { spy } from 'sinon'withimport { vi } from 'vitest'spy()calls tovi.fn()in thespyApifunctiontest/utils/pickers/misc.tsimport sinon from 'sinon'withimport { vi } from 'vitest'sinon.stub()tovi.fn()instubMatchMediafunctiontest/utils/setupFakeClock.tsimport { useFakeTimers } from 'sinon'withimport { vi } from 'vitest'useFakeTimers()calls tovi.useFakeTimers()fakeClock.runToLast()with equivalent Vitest methodfakeClock.restore()withvi.useRealTimers()test/utils/scheduler/StoreSpy.tsximport { spy as sinonSpy, SinonSpy } from 'sinon'withimport { vi, MockInstance } from 'vitest'sinonSpy()tovi.spyOn()SinonSpytype withMockInstanceorReturnType<typeof vi.spyOn>restore()tomockRestore()test/setupVitest.tsimport sinon from 'sinon'sinon.restore()inafterEachwithvi.restoreAllMocks()test/utils/pickers/assertions.tsSinonSpytype import with Vitest'sMockInstancetype2. Component Tests
packages/x-internals/src/EventManager/EventManager.test.tsimport { spy, assert as sinonAssert } from 'sinon'withimport { vi, expect } from 'vitest'spy()calls tovi.fn()sinonAssert.callOrder(listener1, listener2)with manual assertions using mock call order tracking:packages/x-data-grid/src/tests/rows.DataGrid.test.tsximport { spy, stub } from 'sinon'withimport { vi } from 'vitest'spy()calls tovi.fn()stub()calls tovi.fn()orvi.spyOn()packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsxspyimports withvi.fn()valueSetterMockto usevi.fn()packages/x-date-pickers/src/DatePicker/tests/DatePicker.test.tsximport { spy } from 'sinon'withimport { vi } from 'vitest'spy()calls tovi.fn()packages/x-scheduler/src/internals/components/event-popover/EventPopover.test.tsxspyimport withvi.fn()packages/x-scheduler-headless/src/utils/SchedulerStore/tests/event.SchedulerStore.test.tsimport { spy } from 'sinon'withimport { vi } from 'vitest'spy()calls tovi.fn()Key Conversions
Basic Spy/Stub
Spying on Methods
Fake Timers
Call Order Assertions
Cleanup
Dependencies
After the migration, remove sinon from package.json:
sinonfromdevDependencies@types/sinonif presentTesting
Ensure all existing tests pass after the migration. The behavior should remain identical, just using Vitest's native mocking instead of sinon.
Benefits
This pull request was created as a result of the following prompt from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.