test: Do testing with jest instead of mocha for paginator - #9224
Conversation
There was a problem hiding this comment.
Code Review
This pull request migrates the testing framework for the core/paginator package from Mocha, Sinon, and Proxyquire to Jest and ts-jest. This involves updating configuration files, replacing test scripts, and rewriting test assertions and mocks to use Jest's native capabilities. Feedback on these changes highlights a major version mismatch in package.json between jest (v30) and ts-jest (v29), suggesting a downgrade to version 29 for compatibility. Additionally, the newly introduced uuid and @types/uuid dependencies are unused and should be removed to prevent unnecessary bloat.
| "@types/jest": "^30.0.0", | ||
| "@types/node": "^22.13.8", | ||
| "@types/proxyquire": "^1.3.31", | ||
| "@types/sinon": "^17.0.4", | ||
| "c8": "^10.1.3", | ||
| "codecov": "^3.8.3", | ||
| "@types/uuid": "^10.0.0", | ||
| "gts": "^6.0.2", | ||
| "jest": "^30.4.2", | ||
| "jsdoc": "^4.0.4", | ||
| "jsdoc-fresh": "^6.0.0", | ||
| "jsdoc-region-tag": "^5.0.0", | ||
| "mocha": "^11.1.0", | ||
| "linkinator": "^6.1.2", | ||
| "path-to-regexp": "^8.2.0", | ||
| "proxyquire": "^2.1.3", | ||
| "sinon": "^19.0.2", | ||
| "typescript": "^5.8.2" | ||
| "ts-jest": "^29.4.10", | ||
| "typescript": "^5.8.2", | ||
| "uuid": "^11.1.0" |
There was a problem hiding this comment.
This PR introduces a couple of issues in devDependencies:
- Jest Version Mismatch: There is a major version mismatch between
jest(^30.4.2) /@types/jest(^30.0.0) andts-jest(^29.4.10).ts-jestmajor versions must align with thejestmajor version to prevent compatibility issues and runtime errors. Since Jest 30 is not yet stable, it is highly recommended to downgradejestand@types/jestto version 29. - Unused Dependencies: Both
uuidand@types/uuidhave been added todevDependencies, but they are not imported or used anywhere in the codebase (the tests use the built-incrypto.randomUUID()). They should be removed to avoid unnecessary bloat.
"@types/jest": "^29.5.12",
"@types/node": "^22.13.8",
"gts": "^6.0.2",
"jest": "^29.7.0",
"jsdoc": "^4.0.4",
"jsdoc-fresh": "^6.0.0",
"jsdoc-region-tag": "^5.0.0",
"linkinator": "^6.1.2",
"path-to-regexp": "^8.2.0",
"ts-jest": "^29.4.10",
"typescript": "^5.8.2"References
- When managing dependencies in package.json, ensure that @types packages are kept in sync with the major version of their corresponding main packages.
There was a problem hiding this comment.
Made these dependency modifications
| 'performance', | ||
| ], | ||
| }); | ||
| jest.useFakeTimers(); |
There was a problem hiding this comment.
This is the only thing I'm a bit skeptical about because I've seen cases where a very particular subset of methods need to be faked, but its probably fine.
There was a problem hiding this comment.
I hit a few of those. The specific issue was that sinon started hooking stuff like nextTick(), so it was breaking tests that literally just wanted to finish on the next tick. If jest didn't make that change, it should be completely fine. If it did, we might still have to look at those.
Description
Migrates the
@google-cloud/paginatorpackage test suite and configuration from Mocha, Sinon, and c8 to Jest andts-jest.Key changes include:
mocha,@types/mocha,sinon,@types/sinon,c8,proxyquire,@types/proxyquire).jest,ts-jest, and@types/jest.jest.config.jsconfigured withts-jest..nycrcand updated.gitignoreforcoverage/.package.jsontest script to usejest --coverage.test/index.tsandtest/resource-stream.tsto native Jest assertions (expect(...).toBe(),expect(...).toEqual(), etc.).proxyquireand Sinon sandboxes/stubs/spies/timers withjest.mock(),jest.spyOn(),jest.fn(), andjest.useFakeTimers().async/awaitand wrapped callback-baseddone()handlers intry/catchblocks to prevent hanging on test failures.commits were migrated from #9218 to pass the cla check.
Impact
@google-cloud/paginatorwith repository-wide efforts to standardize test infrastructure on Jest.ts-jest, and integrated code coverage reporting.