Repository navigation
[code-infra] Replace the sinon stubs with Vitest mocks - #23472
Conversation
Fifth step away from Sinon, following mui#23443, mui#23460, mui#23464 and mui#23466. Sinon goes from 29 files to 22. A bare `stub()` is a `vi.fn()`, with the configuration methods renamed: stub() -> vi.fn() .returns(value) -> .mockReturnValue(value) .resolves(value) -> .mockResolvedValue(value) .rejects(error) -> .mockRejectedValue(error) .reset() -> .mockReset() .restore() -> .mockRestore() `stub(obj, 'method')` is not a plain `vi.spyOn`. Sinon replaces the method with a no-op, while `vi.spyOn` keeps calling the original, so each of these now spells out the replacement. `stub(console, 'error')` in the "swallow warnings" suite is the case that matters: without an explicit `mockImplementation`, it would stop swallowing. Four of the five spy-on-object sites already restored explicitly, so they only needed the method renamed. `AdapterDayjs` was the one relying on the shared `sinon.restore()`, and it now restores through `onTestFinished`, so this step still needs no `vi.restoreAllMocks()`. Four files were held back from earlier steps only by their `stub` usage and are now fully Sinon-free.
Deploy previewBundle size
Check out the code infra dashboard for more information about this PR. |
|
@claude review |
PR reviewThis is a faithful, mechanical Sinon→Vitest migration of seven test files, and the part the description flags as load-bearing is handled correctly: every Tests (3)1. 🟡
|
- `promptSpy` seeded a resolved value with `mockResolvedValue`, which the `beforeEach` then wiped: `mockReset` restores the implementation passed to `vi.fn(impl)`, but drops one configured afterwards. The seed had been dead under Sinon too, so drop it rather than change what the tests exercise. - Rename the auto-scroll `requestAnimationFrame` mock to `rafStub`. As a local it shadowed the DOM API for the whole suite, and the other spy-on-object sites in this PR already use the `*Stub` name.
|
Took two of the four, and checked the two behavioural claims rather than reasoning about them. Tests 1, the dead
Good catch on the general rule, and it is not what the description's mapping table implies. Simplifications 1, the shadowed Tests 2, the optional chain: this one does not hold. The claim is that So it evaluates to Tests 3, |
Fifth step away from Sinon, following #23443, #23460, #23464 and #23466. Sinon goes from 29 files to 22.
Bare
stub()A standalone stub is a
vi.fn(), with its configuration methods renamed:stub(obj, 'method')is not a plainvi.spyOnThis is the part worth reviewing. Sinon replaces the method with a no-op that returns
undefined;vi.spyOnwraps the method but keeps calling the original. So the replacement has to be spelled out:That one is load-bearing: it backs the
swallow warningssuite inlayout.DataGrid.test.tsx, and a barevi.spyOnthere would stop swallowing and let the suite fail on the warnings it exists to absorb. The same applies to theperformance.now,navigator.clipboard.writeTextandrequestAnimationFramestubs, which all carried acallsFakeand now carry amockImplementation.Restoring
Four of the five spy-on-object sites already restored explicitly, in an
afterEachor atry/finally, so they only needed the method renamed.AdapterDayjs.test.tsxwas the one relying on the sharedsinon.restore(), and said so in a comment. It now restores throughonTestFinished. So this step still needs novi.restoreAllMocks()in the shared teardown.Files that come along
Four files were held back from the earlier steps only by their
stubusage, so their remainingspy()calls and accessors migrate here too, leaving them fully Sinon-free:aiAssistant,layout,rowsandclipboard.Status
typescriptandeslintare clean, and the changed test files pass in the browser.Two things this step caught that are worth knowing for the remaining migrations:
spy(tovi.fn(rewrite silently producesvi.fn(obj, 'method')where the source was a spy-on-object. TypeScript catches it on arity, but only becausevi.fntakes at most one argument. There are no remaining occurrences anywhere in the repo.stub()tovi.fn()does not update the accessors on that variable, and.argson aMockisany, so bothtscandeslintstay green while the test is broken. Only running the suite surfaced it.