Repository navigation
test(storage): isolate Sinon sandboxes and fake timers in unit tests - #9919
danieljbruce wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors several test files to use Sinon sandboxes instead of global Sinon methods to prevent test pollution and ensure proper cleanup. While this is a great improvement, in transfer-manager.ts, a sandbox is created inline but never restored, which could lead to resource leaks in persistent test runner environments. It is recommended to declare this sandbox at the suite level and restore it in an after hook.
| describe('Transfer Manager', () => { | ||
| const BUCKET_NAME = 'test-bucket'; | ||
| const STORAGE = sinon.stub( | ||
| const STORAGE = sinon.createSandbox().stub( |
There was a problem hiding this comment.
Creating an isolated sandbox with sinon.createSandbox() is a great way to prevent test pollution. However, since this sandbox is never restored, it can lead to resource leaks in persistent test runner environments (such as watch mode). Consider declaring the sandbox at the suite level and restoring it in an after hook.
const transferManagerSandbox = sinon.createSandbox();
const STORAGE = transferManagerSandbox.stub(|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors several test files to use sandbox-specific Sinon methods instead of global Sinon methods, ensuring better test isolation and preventing global state leakage. Feedback on these changes highlights two main areas for improvement: first, in resumable-upload.ts, a custom restoreStubs() helper should be extracted and called via setImmediate to safely restore stubs across asynchronous boundaries; second, in transfer-manager.ts, defining a stubbed STORAGE instance at the module level is an anti-pattern that can cause test pollution, and it should instead be instantiated and stubbed within test hooks.
| describe('Transfer Manager', () => { | ||
| const BUCKET_NAME = 'test-bucket'; | ||
| const STORAGE = sinon.stub( | ||
| const STORAGE = sinon.createSandbox().stub( |
There was a problem hiding this comment.
Defining a stubbed STORAGE instance at the module level using sinon.createSandbox().stub(...) without ever restoring it is an anti-pattern. While it isolates the stubs from global sinon.restore() calls, sharing a single stubbed instance across all tests in the file can lead to test pollution if any test modifies the stub's behavior.
Consider instantiating and stubbing STORAGE within a before or beforeEach hook, and restoring the sandbox in after or afterEach to ensure clean test isolation.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Description
Isolates Sinon sandboxes and fake timers across Storage unit tests.
Impact
Fixes Bun presubmit unit test timeouts caused by leaked global timers.
Changes
sinon.useFakeTimersandglobal.setTimeoutspying inhandwritten/storage/test/resumable-upload.tswithin a dedicatedsinon.SinonSandboxsosandbox.restore()restores the spy before uninstalling the fake clock, preventing a leaked spy on Sinon's default sandbox from overwritingglobal.setTimeoutwith a dead fake timer whensinon.restore()runs later.sinoncalls (stub,spy,useFakeTimers,restore) with per-suite sandboxes inhandwritten/storage/test/file.ts,handwritten/storage/test/signer.ts,handwritten/storage/test/index.ts, andhandwritten/storage/test/transfer-manager.ts, and delete(global as any).resumableUploadinfile.tsteardown.fs.createReadStreamstub inhandwritten/storage/test/bucket.tsto capture only the targettextfile.txtstream so unrelated concurrent reads (such as ADC lookup) cannot overwritereadStream.Testing
handwritten/storage/test/resumable-upload.tsverifying thatglobal.setTimeoutremains functional afterexponential back offteardown and a subsequentsinon.restore().resumable-upload.js->file.js->util.jsexecution order passes under both Bun 1.4.2 and Node.js (--no-parallel).handwritten/storageunit test suite 5 times under Bun 1.4.2 and 5 times under Node.js (1,104 passing, 0 failures across all 10 runs).ci/run_conditional_tests.sh --strict(Node.js and Bun),node ./bin/linter.mjs --strict,pnpm --dir handwritten/storage run lint, andcore/test-utilsbreaking-change check all pass.Alternatives
setTimeoutSpymanually beforeclock.restore()on Sinon's default sandbox alone would leave other Storage tests exposed to cross-file state leaks whenever test execution order varies across runtimes.bin/run-test.cjswould only mask the test-order dependency rather than fixing the underlying fake-timer and sandbox leak.presubmit-bunfailing on pull requests that touchhandwritten/storage(as seen in chore(deps): fix Renovate configuration and remove stale file #9912) wheneverresumable-upload.jsruns beforefile.js.