Skip to content

test: cover req.is() array argument and res.redirect() misuse branches - #7405

Closed
arjun2075 wants to merge 1 commit into
expressjs:masterfrom
arjun2075:test/redirect-and-is-branch-coverage
Closed

test: cover req.is() array argument and res.redirect() misuse branches#7405
arjun2075 wants to merge 1 commit into
expressjs:masterfrom
arjun2075:test/redirect-and-is-branch-coverage

Conversation

@arjun2075

Copy link
Copy Markdown

Two branches were unexercised by the existing test suite:

  • lib/request.jsreq.is() was only ever called with a bare string in tests, so !Array.isArray(types) never took its false path (line 273).
  • lib/response.jsres.redirect()'s deprecation guards for a missing address (!address) and a non-string address were never triggered (lines 827, 831).

Changes

  • test/req.is.jsreq.is() given an array of types (matching, and no match)
  • test/res.redirect.jsres.redirect() called with no address, and with a non-string address

Coverage (measured locally with npm run test-cov)

File Before After
lib/request.js 98.11% branch (line 273 uncovered) 100% branch
lib/response.js 93.71% branch (lines 827, 831, 835 uncovered) 94.68% branch (line 835 remains)

Line 835 (res.redirect() called with a non-number status) is not covered here: any non-integer status value is rejected by res.status() later in the same function, so there's no way to exercise that deprecation branch through a request that completes successfully. Flagging it in case it's worth a follow-up (either loosening the validation order, or accepting it stays untested).

Testing

Ran the full suite locally (npm test) multiple times with this change — passes consistently. Note: I also observed an intermittent, pre-existing "WebSockets request was expected" failure in res.render.js on master (~1 in 10 runs, unrelated to this change, appears to be a port-reuse race between adjacent supertest servers). Happy to open a separate issue for that if useful — didn't want to scope-creep this PR.

Two branches were unexercised by the existing suite:
- lib/request.js: req.is() only had tests passing a bare string, so
  the !Array.isArray(types) branch never took its false path.
- lib/response.js: res.redirect()'s deprecation guards for a missing
  or non-string address were never triggered.

Adds:
- req.is.js: req.is() given an array of types (matching type, no match)
- res.redirect.js: res.redirect() with no address, and with a
  non-string address

request.js reaches 100% branch coverage. response.js improves from
93.71% to 94.68% branch coverage; the remaining uncovered branch
(non-number status) can't be exercised via a successful redirect,
since res.status() throws synchronously on any non-integer value --
noted in the PR description rather than forcing a test around it.
@arjun2075

Copy link
Copy Markdown
Author

Closing — #7112 already covers this (and more: it also covers the non-number-status branch, which I'd left untested). #7112 was opened first. Apologies for the duplicate.

1 similar comment
@arjun2075

Copy link
Copy Markdown
Author

Closing — #7112 already covers this (and more: it also covers the non-number-status branch, which I'd left untested). #7112 was opened first. Apologies for the duplicate.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants