Skip to content

test: add unit tests for logger and update check utils - #2518

Open
ZayanKhan-12 wants to merge 1 commit into
ChromeDevTools:mainfrom
ZayanKhan-12:chore/tests-logger-updates
Open

test: add unit tests for logger and update check utils#2518
ZayanKhan-12 wants to merge 1 commit into
ChromeDevTools:mainfrom
ZayanKhan-12:chore/tests-logger-updates

Conversation

@ZayanKhan-12

Copy link
Copy Markdown
Contributor

Why

src/utils/logger.ts and src/utils/check-for-updates.ts had no unit test coverage in tests/utils/. Both contain observable behavior (file log sink, namespace enabling, flush timeout, update cache freshness/once-per-process logic) that can regress silently, so this adds focused tests asserting the current behavior. No src/ changes.

What's covered

tests/utils/logger.test.ts (6 tests)

  • saveLogsToFile writes mcp:log output to the given file, appends (flag a+) instead of truncating, writes one newline-terminated line per log call, and enables the mcp:log debug namespace even when previously disabled.
  • flushLogs resolves once the stream has finished, and rejects when the stream does not finish within the timeout (via a stub stream whose end callback never fires).
  • Global debug state (enabled namespaces, debug.log sink) is saved and restored around the suite.

tests/utils/check-for-updates.test.ts (8 tests)

  • Early return when CHROME_DEVTOOLS_MCP_NO_UPDATE_CHECKS is set (the test runner sets this globally, so the tests save/clear/restore it).
  • First run with no cache: writes {version: VERSION} to ~/.cache/chrome-devtools-mcp/latest.json and spawns check-latest-version.js detached with stdio: 'ignore'.
  • Warns with Update available: <current> -> <cached> plus the caller-provided message when the cached version is newer; no warning when equal.
  • Fresh cache (<24h): no re-check spawn, cache untouched. Stale cache (>24h): re-check spawn, mtime refreshed, content preserved.
  • Invalid-JSON cache is ignored without throwing.
  • Once-per-process guard, including resetUpdateCheckFlagForTesting().
  • Tests redirect os.homedir() to a per-test temp dir and stub child_process.spawn/console.warn via node:test's mock.method, so no real home-directory files are touched and no subprocess/network activity happens.

What's not covered (and why)

  • saveLogsToFile's stream error handler calls process.exit(1), which cannot be exercised in-process without killing the test runner.
  • The spawned bin/check-latest-version.js subprocess itself (registry fetch) is out of scope for these unit tests; spawn is stubbed.
  • Observation (not asserted, no behavior change made): while logs are redirected to a file, debug.log is replaced with a plain chunks.join(' ') sink, so printf-style placeholders (e.g. logger('hello %s', 'world')) are written literally as hello %s world instead of being substituted the way the default util.formatWithOptions-based sink does. The tests use plain messages to assert current behavior; happy to file a follow-up issue if that quirk is worth fixing.

Testing

  • npm run build — clean.
  • node scripts/test.js tests/utils/logger.test.ts tests/utils/check-for-updates.test.ts — 14/14 pass.
  • npm run test:no-build — 702 tests, 702 pass, 0 fail.
  • npm run check-format — eslint + prettier clean.

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented Aug 8, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@ZayanKhan-12
ZayanKhan-12 force-pushed the chore/tests-logger-updates branch from 96734b7 to c795bf3 Compare August 8, 2026 21:46
@Lightning00Blade
Lightning00Blade self-requested a review August 10, 2026 09:40
assert.ok(content.endsWith('\n'));
});

it('enables the mcp:log debug namespace', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a side-effect and we should not test for it.

@@ -0,0 +1,170 @@
/**

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is test in tests/check-for-updates.test.ts Why do we need this?

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.

3 participants