The test suite uses Jest as its test runner, Supertest for HTTP-level integration testing of the Express app, and a customDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/nicosaporiti/buda-lightning-invoice/llms.txt
Use this file to discover all available pages before exploring further.
buda-promise mock that replaces the real Buda.com API client so tests are deterministic, require no network access, and need no real Buda credentials. Unit tests exercise each module in complete isolation — helpers, controllers, and middleware are all tested with their dependencies mocked — while integration tests boot the full Express application and assert on the HTTP responses it returns.
Running tests
| Command | Description |
|---|---|
npm test | Run the full Jest test suite once. |
npm run test:coverage | Run the suite and generate a coverage report in the coverage/ directory. Exits with a non-zero code if any coverage threshold is not met. |
npm run test:watch | Run Jest in interactive watch mode. Re-runs affected tests on every file save. |
Test structure
Unit tests (tests/unit/)
Unit tests cover every module in isolation by mocking all external dependencies. The suite includes:
tests/unit/helpers/getInvoice.test.js— tests thatgetInvoiceinstantiates the Buda client with the correct env credentials, callslightning_network_invoiceswith the right arguments, returns theencoded_payment_requeststring, and propagates API errors.tests/unit/helpers/getPaymentConfirmation.test.js— tests thatgetPaymentConfirmationcallsdeposits('btc'), returnstruefor a confirmed matching invoice, returnsfalsefor a pending invoice, returnsfalsefor an empty deposit list, and propagates API errors.tests/unit/controllers/buda.controller.test.js— testsnewInvoice,paymentConfirmation, andcallbackwith mocked helpers. Verifies correct arguments are forwarded, success responses are structured properly, and400is returned on helper failure.tests/unit/middlewares/fields-validator.test.js— testsfieldsValidationby mockingexpress-validator’svalidationResult. Verifiesnext()is called on a clean result and a400witherrors.mapped()is returned on failure.
Integration tests (tests/integration/routes.test.js)
Integration tests use Supertest to send real HTTP requests against the Express application. The buda-promise module is replaced with the mock before index.js is required, so the app is fully booted but never touches the real Buda API.
All four routes are covered:
POST /newinvoice— valid body returns200with{ invoice, amount, msg }; missingamount, missingmsg, non-numericamount,amount < 1, and emptymsgeach return400.POST /status— paid invoice returns200withstatus: true; unpaid returns200withstatus: false; missing invoice returns400.GET /callback— validamountreturns200with LNURL response; optionalcommentis used as memo;amountbelow1000or missing returns400.GET /.well-known/lnurlp/:username— returns the LNURL-pay metadata object (no validation required; tested via the controller unit tests).
Mocks (tests/mocks/buda-promise.mock.js)
The mock module exports MockBuda, a Jest constructor mock, along with the two jest function stubs it exposes — mockLightningNetworkInvoices and mockDeposits — and a constant MOCK_INVOICE string used as the default resolved value. Test files import these stubs so they can override resolved values or simulate rejections using mockResolvedValueOnce / mockRejectedValueOnce.
Coverage thresholds
Coverage thresholds are declared injest.config.js under coverageThreshold.global. If the measured coverage for any metric falls below its threshold, npm run test:coverage exits with a non-zero code and CI will fail.
| Metric | Minimum |
|---|---|
| Branches | 60% |
| Functions | 70% |
| Lines | 70% |
| Statements | 70% |
Coverage is collected only from
controllers/**/*.js, helpers/**/*.js, middlewares/**/*.js, and routes/**/*.js. The buda-promise/ directory is explicitly excluded because it is an external vendor wrapper — its coverage is not tracked.jest.config.js:
Mock strategy
Every test file that exercises code which internally callsrequire('../buda-promise/buda') begins with:
require call and substitutes MockBuda — a jest constructor mock — for the real client. When the production code calls new Buda(apiKey, apiSecret), it actually receives a plain object whose methods are the exported jest function stubs.
The mock module itself is defined once and shared across the entire suite:
mockResolvedValueOnce without affecting other tests. The beforeEach blocks call jest.clearAllMocks() to reset call counts and any per-test overrides between tests, keeping every test independent.