Skip to content

feat: implement Jest testing infrastructure and add initial unit test… - #83

Merged
meshackyaro merged 2 commits into
trustflow-protocol:mainfrom
ABEEGOLD:Establish-test-infrastructure
Aug 20, 2026
Merged

feat: implement Jest testing infrastructure and add initial unit test…#83
meshackyaro merged 2 commits into
trustflow-protocol:mainfrom
ABEEGOLD:Establish-test-infrastructure

Conversation

@ABEEGOLD

Copy link
Copy Markdown
Contributor

Closes #82

## Description

This PR establishes the core test infrastructure for the TrustFlow frontend. It introduces Jest and React Testing Library, configuring them to seamlessly integrate with Next.js 13 pages router and TypeScript.

Crucially, it also introduces robust reusable mock utilities for the Freighter browser extension API and Soroban RPC, allowing developers to write test coverage for heavily on-chain or wallet-dependent hooks/components. As part of this setup, full test coverage was written for `useWallet`, `useAccount`, and both the profile and USDC price API endpoints, achieving >94% coverage on these files. Finally, automated testing has been wired into the CI workflow.


## Type of Change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation update
- [ ] Refactoring (no functional changes)

## Changes Made

- **Testing Dependencies**: Installed `jest`, `ts-jest`, `@testing-library/react`, `@testing-library/jest-dom`, and related packages.
- **Jest Configuration**: Created `jest.config.js` (configured with `ts-jest`) and `jest.setup.js` for environment initialization.
- **Wallet & Contract Mocks**: 
  - Added a global `__mocks__/@stellar/freighter-api.ts` to simulate Freighter wallet connections and transactions.
  - Added `test-utils/soroban-rpc-mock.ts` to simulate Soroban RPC interactions and ledger entries.
- **Test Coverage**: Added comprehensive test suites for `hooks/useAccount.ts`, `hooks/useWallet.ts`, `pages/api/profile.ts`, and `pages/api/usdc-price.ts` (with `NextResponse` mocked for the API edge routes).
- **CI & NPM Scripts**: Added `npm run test` and `npm run test:coverage` to `package.json` and added a testing stage to `.github/workflows/ci.yml`.

## Screenshots (if applicable)

*N/A - Infrastructure and test coverage changes only.*

## Checklist

- [x] My code follows the project's code style
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings or errors
- [x] I have run `npm run lint` and fixed all issues
- [x] I have run `npm run typecheck` with no errors
- [x] I have run `npm run build` successfully
- [x] I have tested my changes locally

## Additional Notes

We had to opt for Jest v29 as Jest v30 was causing node bus errors with Next.js 13 on the CI machine. The tests currently have >94% line coverage for the target files. The Freighter mock utilities are documented inside the mock file itself and can be reused easily for the upcoming UI dispute components.

@vercel

vercel Bot commented Aug 16, 2026

Copy link
Copy Markdown

@ABEEGOLD is attempting to deploy a commit to the Meshack Yaro's projects Team on Vercel.

A member of the Team first needs to authorize it.

@meshackyaro

Copy link
Copy Markdown
Contributor

Closes #82

## Description

This PR establishes the core test infrastructure for the TrustFlow frontend. It introduces Jest and React Testing Library, configuring them to seamlessly integrate with Next.js 13 pages router and TypeScript.

Crucially, it also introduces robust reusable mock utilities for the Freighter browser extension API and Soroban RPC, allowing developers to write test coverage for heavily on-chain or wallet-dependent hooks/components. As part of this setup, full test coverage was written for `useWallet`, `useAccount`, and both the profile and USDC price API endpoints, achieving >94% coverage on these files. Finally, automated testing has been wired into the CI workflow.


## Type of Change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation update
- [ ] Refactoring (no functional changes)

## Changes Made

- **Testing Dependencies**: Installed `jest`, `ts-jest`, `@testing-library/react`, `@testing-library/jest-dom`, and related packages.
- **Jest Configuration**: Created `jest.config.js` (configured with `ts-jest`) and `jest.setup.js` for environment initialization.
- **Wallet & Contract Mocks**: 
  - Added a global `__mocks__/@stellar/freighter-api.ts` to simulate Freighter wallet connections and transactions.
  - Added `test-utils/soroban-rpc-mock.ts` to simulate Soroban RPC interactions and ledger entries.
- **Test Coverage**: Added comprehensive test suites for `hooks/useAccount.ts`, `hooks/useWallet.ts`, `pages/api/profile.ts`, and `pages/api/usdc-price.ts` (with `NextResponse` mocked for the API edge routes).
- **CI & NPM Scripts**: Added `npm run test` and `npm run test:coverage` to `package.json` and added a testing stage to `.github/workflows/ci.yml`.

## Screenshots (if applicable)

*N/A - Infrastructure and test coverage changes only.*

## Checklist

- [x] My code follows the project's code style
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings or errors
- [x] I have run `npm run lint` and fixed all issues
- [x] I have run `npm run typecheck` with no errors
- [x] I have run `npm run build` successfully
- [x] I have tested my changes locally

## Additional Notes

We had to opt for Jest v29 as Jest v30 was causing node bus errors with Next.js 13 on the CI machine. The tests currently have >94% line coverage for the target files. The Freighter mock utilities are documented inside the mock file itself and can be reused easily for the upcoming UI dispute components.

The critical blocker is the missing testPathIgnorePatterns in jest.config.js.

File: jest.config.js
Lines: 1–11

The jest.config.js is missing testPathIgnorePatterns, which is causing Jest to pick up compiled test files from .next/ during CI runs. This is why the test suite fails with errors like "Page /api/profile.test does not export a default function."

Please update the config to:

module.exports = {
preset: 'ts-jest',
testEnvironment: 'jest-environment-jsdom',
setupFilesAfterEnv: ['/jest.setup.js'],
moduleNameMapper: {
'^@/(.*)$': '/$1',
},
testPathIgnorePatterns: ['/.next/', '/node_modules/'],
transform: {
'^.+\.(ts|tsx)$': 'ts-jest',
},
};

This ensures Jest only runs against your source test files (.test.ts in hooks/ and pages/api/) and ignores Next.js's build output.

Why this matters: The CI is currently failing because Next.js compiles the source test files into .next/server/ and Jest is running against both the source files (which pass) and the compiled versions (which fail because they're not page handlers). Without this exclude pattern, every PR that modifies tests will fail CI, blocking merges. This is a required fix before the PR can be merged.


Scope: General — test timing stability

The useWallet.test.ts tests are triggering React warnings about state updates not wrapped in act(). While the tests currently pass, this indicates the async polling behavior may be tightly coupled to test timing.

Consider either:

1. Explicitly managing timers in the waitFor() callback, or
2. Using waitFor with an explicit timeout that aligns with the polling interval in useWallet.ts

This will eliminate the console warnings and make the tests more resilient to timing changes.


**Why this matters:** These warnings are usually harmless in passing tests, but they signal that the test is racing with asynchronous state updates. As the hook or polling logic evolves, this could become flaky. Addressing it now keeps the test suite stable and prevents future CI intermittency.

Overall Assessment:

- Code Quality: High — mocks are well-structured, tests are meaningful, configuration is sound
- Test Coverage: Excellent (>94% on target files as stated)
- Risk Level: Low on code, High on CI readiness (must be fixed before merge)
- Blocking Issue: Jest configuration must exclude .next/ directory and any other compiled output from test runs
- jest.config.js is missing testPathIgnorePatterns: Add a pattern to exclude .next/ and node_modules/ so compiled Next.js output isn't picked up by Jest. This is the direct cause of the CI failures. Update line 1–11 to include:

testPathIgnorePatterns: ['<rootDir>/.next/', '<rootDir>/node_modules/'],

- Possible race condition in useWallet polling test: The fake timer tests wrap state updates in act(), but polling may occur outside the test's control. Consider explicitly advancing timers in waitFor() callbacks or ensuring the polling interval aligns with manual timer advances to eliminate the console warnings.

Possible Improvements

- Document the mocking pattern in a testing guide: While the mock files include inline comments, a dedicated TESTING.md or docs/testing.md would help future contributors quickly understand how to use the Freighter and Soroban RPC mocks for new features. Reference it from the PR description or README.

- Add a .test. pattern to .eslintignore or similar: If not already present, test files should be excluded from strict linting rules that might apply to production code (e.g., no-unused-vars for test helpers).

- Consider snapshot tests for API response schemas: The profile and USDC price API tests are solid, but adding snapshot tests for the mock fallback payloads would catch unintended changes to the contract shape over time.

@meshackyaro

Copy link
Copy Markdown
Contributor

Hi @ABEEGOLD — thank you for this excellent and thorough contribution!

This PR sets up a robust Jest + React Testing Library environment for our Next.js + TypeScript frontend, and you've gone above and beyond by providing reusable mocks for Freighter and Soroban RPC, comprehensive unit tests for critical hooks and API routes (notably useWallet, useAccount, profile and usdc-price), and wiring tests into CI. The documentation inside the mocks is helpful and the npm scripts make running tests straightforward.

A couple of small, optional suggestions:

  • Add a short README/CONTRIBUTING snippet (or TESTING.md) that documents how to run the test suite and the purpose of the Freighter/Soroban mocks, so maintainers and new contributors can get started quickly.
  • Consider a short note in the CI job explaining the Jest v29 choice (helpful historical context).

Overall this is a high-quality, well-documented change that materially improves our developer experience and confidence in on-chain/wallet-dependent code. Great work — approved once CI is green!

@vercel

vercel Bot commented Aug 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
trustflow-frontend Ready Ready Preview Aug 20, 2026 6:16am

@meshackyaro
meshackyaro merged commit 4733d20 into trustflow-protocol:main Aug 20, 2026
4 checks passed
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.

Establish test infrastructure with Freighter/RPC mocking and cover highest-risk hooks

2 participants