Skip to content

refactor(tests): decompose getTestInstance to reduce complexity#8

Merged
mroderick merged 5 commits intomainfrom
refactor/getTestInstance-complexity
Apr 18, 2026
Merged

refactor(tests): decompose getTestInstance to reduce complexity#8
mroderick merged 5 commits intomainfrom
refactor/getTestInstance-complexity

Conversation

@mroderick
Copy link
Copy Markdown
Collaborator

@mroderick mroderick commented Apr 18, 2026

Summary

Refactors test/helpers/test-instance.js to address code quality issues identified by fallow.

Changes

  • Extract parseSessionCookie helper (eliminates duplication)
  • Extract extractCookieFromError helper (reduces complexity)
  • Extract createTestAuth factory function
  • Extract createTestClient factory function
  • Extract createGetAuthHeaders factory function
  • Fix magic link closure issue using external array

Measured Improvements

# Before
$ npx fallow dupes
test/helpers/test-instance.js:134-141
test/helpers/test-instance.js:159-166
Identical code blocks detected

$ npx fallow health --complexity
test/helpers/test-instance.js:86 getAuthHeaders
  13 cyclomatic   20 cognitive   66 lines

$ npx fallow health
test/helpers/test-instance.js
  :27 getTestInstance  120 lines

# After
$ npx fallow dupes
✓ No code duplication found

$ npx fallow health --complexity
✓ No high complexity functions

$ npx fallow health
✓ No large functions in test-instance.js

All 28 tests pass.

…cation

The getAuthHeaders function contained two identical 8-line blocks for
parsing session cookies from Set-Cookie headers. Extract this logic into
a standalone helper function.

Fallow report (before):
  test/helpers/test-instance.js:134-141
  test/helpers/test-instance.js:159-166
  Identical code blocks detected

Fallow report (after):
  No duplication in test-instance.js

This is the first step in reducing the complexity of getTestInstance.
The getAuthHeaders function had high cyclomatic (13) and cognitive (20)
complexity due to nested conditionals for handling 302 redirect errors.
Extract the cookie extraction logic from error responses into a dedicated
helper function.

Fallow health report (before):
  test/helpers/test-instance.js:86 getAuthHeaders
    13 cyclomatic   20 cognitive   66 lines
  Functions exceeding cyclomatic or cognitive complexity thresholds

Fallow health report (after):
  No high complexity functions

This refactoring separates the error handling concern and reduces the
cognitive load of the main getAuthHeaders function.
Extract the Better Auth configuration logic from getTestInstance into a
dedicated createTestAuth factory function. This separates the auth setup
concern from the orchestration logic.

Key changes:
- Created createTestAuth(db, magicLinksStore) function
- Fixed closure issue by using external magicLinksStore array instead of
  auth._testMagicLinks property
- getTestInstance now orchestrates rather than configures

Fallow health improvement:
- getTestInstance reduced from 120 lines to ~100 lines
- Auth configuration now isolated and reusable
Extract the client helper creation logic from getTestInstance into a
dedicated createTestClient factory function. This isolates the admin
operations configuration.

Key changes:
- Created createTestClient(auth) function
- Client helper logic now separated from main orchestration
- Improves testability of admin operations

Fallow health improvement:
- getTestInstance further reduced in size
- Client configuration now isolated and reusable
Extract the getAuthHeaders helper creation from getTestInstance into a
dedicated createGetAuthHeaders factory function. This completes the
decomposition of the large getTestInstance function.

Key changes:
- Created createGetAuthHeaders(auth, magicLinksStore) function
- Returns the async getAuthHeaders function
- Uses external magicLinksStore instead of auth._testMagicLinks

Fallow health improvement:
- getTestInstance reduced to ~20 lines (pure orchestration)
- Large function warning completely resolved
- All factory functions are independently testable
@mroderick mroderick force-pushed the refactor/getTestInstance-complexity branch from 9a3ce33 to 1c4bda6 Compare April 18, 2026 05:54
@mroderick mroderick marked this pull request as ready for review April 18, 2026 06:12
@mroderick mroderick requested a review from till April 18, 2026 07:57
@mroderick mroderick merged commit a6c3a6b into main Apr 18, 2026
5 checks passed
@mroderick mroderick deleted the refactor/getTestInstance-complexity branch April 18, 2026 10:27
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