Skip to content

test: add comprehensive tests for namespace matching and core utilities#1025

Open
mahmoodhamdi wants to merge 1 commit intodebug-js:masterfrom
mahmoodhamdi:test/namespace-matching-coverage
Open

test: add comprehensive tests for namespace matching and core utilities#1025
mahmoodhamdi wants to merge 1 commit intodebug-js:masterfrom
mahmoodhamdi:test/namespace-matching-coverage

Conversation

@mahmoodhamdi
Copy link
Copy Markdown

What

Added 23 new tests covering previously untested core functionality, bringing test count from 16 to 39.

Why

While working with debug in my Express middleware, I noticed several core functions had no direct test coverage. The matchesTemplate function (which replaced the old regex-based approach) is central to how namespace matching works, yet had zero dedicated tests. Same for coerce, selectColor, and the format string processing logic.

New test coverage

Wildcard namespace matching (11 tests)

  • Simple wildcard at end: app:*
  • Single wildcard for everything: *
  • Wildcard in the middle: app:*:error
  • Multiple wildcards: *:*:error
  • Exact match without wildcards
  • Exclusion patterns: app:*,-app:secret
  • Exclusion with wildcards: *,-app:*
  • Skip precedence over names
  • Multiple comma-separated namespaces
  • Space-delimited namespaces
  • Leading/trailing whitespace handling

enabled() function (4 tests)

  • Returns false when nothing is enabled
  • Exact namespace match
  • Wildcard match
  • Skipped namespace

coerce() function (3 tests)

  • Error with stack trace
  • Error without stack (falls back to message)
  • Non-Error values pass through

selectColor() (2 tests)

  • Consistent color for same namespace
  • Color comes from available colors array

Format string handling (3 tests)

  • %% escape sequence
  • Non-string first argument gets %O prepended
  • Custom formatters

Testing

39 passing (7ms)

All existing tests continue to pass. No source code changes — tests only.

Note: The linter (xo) currently fails on both master and this branch due to util.isDate being removed in Node.js 24. This is a pre-existing issue unrelated to these changes.

Add 23 new tests covering previously untested functionality:

- Wildcard namespace matching (simple, middle, multiple wildcards)
- Exclusion patterns (single, wildcard, precedence over names)
- Multiple namespace delimiters (commas, spaces, whitespace trimming)
- enabled() function (empty state, exact match, wildcard, skip)
- coerce() function (Error with stack, Error without stack, non-Error)
- selectColor() consistency and color pool membership
- Format string handling (%% escape, non-string args, custom formatters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant