Skip to content

test: add unit tests for Context model helpers and problem middleware#2072

Open
chengyixu wants to merge 1 commit intoasyncapi:masterfrom
chengyixu:test/add-coverage-context-and-problem-middleware
Open

test: add unit tests for Context model helpers and problem middleware#2072
chengyixu wants to merge 1 commit intoasyncapi:masterfrom
chengyixu:test/add-coverage-context-and-problem-middleware

Conversation

@chengyixu
Copy link

What?

Adds unit tests targeting the low-coverage areas identified in issues #2016 and #2019.

New test files

test/unit/domains/models/Context.test.ts (closes #2016)

Covers the uncovered branches of isContextFileEmpty() — the pure helper that is called inside every mutating Context function:

  • Empty store with no current field → true
  • Store with entries → false
  • current set with populated store → false
  • current set with empty store (two keys so length check fails) → false

Also validates the IContextFile interface shapes for documentation clarity.

test/unit/apps/api/middlewares/problem.middleware.test.ts (closes #2019)

Covers all uncovered branches of problemMiddleware:

  • 4xx error → correct status code and JSON body, no stack/cause
  • 5xx error → status 500, JSON body returned
  • Falsy status (0) → defaults to 500
  • error.get() throws (non-ProblemException) → catch block calls next(err)
  • Type prefix stripping path exercised (no crash)

Why?

Both Context.ts and problem.middleware.ts had very low branch coverage masked by aggregate totals. These tests directly target the branches listed in the issue descriptions.

How to test

npm run unit:test -- --grep "Context model"
npm run unit:test -- --grep "problemMiddleware"

Addresses issues asyncapi#2016 and asyncapi#2019:
- test/unit/domains/models/Context.test.ts: covers uncovered branches of
  isContextFileEmpty() including empty store, non-empty store, current set,
  and two-key edge case; also validates IContextFile interface shapes.
- test/unit/apps/api/middlewares/problem.middleware.test.ts: covers all
  branches of problemMiddleware: 4xx response, 5xx response with no
  stack/cause, status defaulting to 500 when falsy, non-ProblemException
  falling into catch block calling next(), and type prefix stripping.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Mar 21, 2026

⚠️ No Changeset found

Latest commit: 7173fbf

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To Triage

Development

Successfully merging this pull request may close these issues.

[TEST] : low coverage in lib/apps/api/middlewares [TEST] : low coverage in lib/domains/models

1 participant