Skip to content

chore: migrate core/common to jest#8746

Open
pearigee wants to merge 3 commits into
mainfrom
pearigee-jest-core-common1
Open

chore: migrate core/common to jest#8746
pearigee wants to merge 3 commits into
mainfrom
pearigee-jest-core-common1

Conversation

@pearigee

@pearigee pearigee commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Migration to Jest & ts-jest

Successfully migrated the @google-cloud/common test suite from Mocha, Sinon, and c8 to Jest and ts-jest. Removed legacy configuration files (.mocharc.js, .nycrc), added coverage/ to .gitignore, and introduced jest.config.js to compile and execute tests on TypeScript files directly.

Key Unit Test Fixes & Isolation

  • Hoisted Mocks: Standardized Jest mocks by resolving actual modules inside mock factories to avoid ReferenceError warnings due to Jest's top-level hoisting of jest.mock().
  • Sandbox & State Leaks: Prevented state leakage across tests by deep-copying the shared authClient object (extend(true, {}, authClient, ...)) rather than mutating it directly.
  • Asynchronous Leaks: Resolved a subtle issue in the authentication tests where a mocked authorizeRequest function resolved to undefined instead of the request options. This originally caused a TypeError in decorateRequest that was thrown asynchronously, bypassing done() and leaking as a failure into subsequent tests.

System Test Reliability & ESM Mocking

Note: System tests were broken on current versions of Node, this PR includes the following fixes:

  • Port and Timeout Isolation: Allocated distinct ports (8118, 8119, 8120) to mock servers in system-test/common.ts to eliminate EADDRINUSE conflicts. Wrapped asynchronous callbacks in structured try...catch blocks to immediately close active servers and fail the test when assertions fail, preventing test runs from hanging.
  • ESM Dynamic Import Workaround: Avoided requiring Node's --experimental-vm-modules CLI flag by mocking gaxios and teeny-request (which dynamically import node-fetch v3) at the top level of the system test file. The mocks redirect HTTP transport through Node's native global fetch API, enabling standard CommonJS execution in Jest.

@pearigee pearigee requested a review from a team as a code owner June 24, 2026 18:38
@pearigee pearigee added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 24, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request migrates the core/common package's test suite from Mocha, Sinon, and Node's assert module to Jest. The changes include updating configuration files, dependencies, and rewriting tests to use Jest assertions and mocking utilities. The review feedback highlights that the migration is incomplete in test/service-object.ts due to remaining assert imports and calls. Additionally, the reviewer points out an anti-pattern of executing expect assertions inside a hoisted jest.mock factory function, recommending capturing the options in a local variable to assert on them within the test block instead.

Comment thread core/common/test/service-object.ts
Comment thread core/common/test/service-object.ts
Comment thread core/common/test/service-object.ts
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants