feat: refactor testing infrastructure to use test containers and fixtures (#976) - #1058
Merged
Smartdevs17 merged 2 commits intoSep 1, 2026
Conversation
…ures (Smartdevs17#976) - Rewrite backend/tests/setup/testContainer.ts with full production-ready test container infrastructure: * TestContainerManager: full lifecycle (start/stop), in-memory entity store with seedDatabase, cleanDatabase, createSavepoint/rollbackToSavepoint for per-test isolation, named takeSnapshot/restoreSnapshot, findById/findAll/ findWhere/upsert/delete/count query helpers * FixtureLoader: typed factories for all core domain entities (plan, user, subscription, merchant, invoice) with auto-incrementing IDs, sensible defaults, and fullSubscriptionScenario() for complete linked graphs * DatabaseSeeder: fluent builder that inserts in dependency order (merchants → plans → users → subscriptions → invoices) * TestClock: deterministic time control with advance/advanceHours/advanceDays /set/reset for time-sensitive tests * TestEventBus: in-memory event capture with assertPublished/assertNotPublished /ofType/last/count for integration assertions without real message brokers * RedisFixture: tracks keys for automatic cleanup, supports pattern cleanup * detectFlakyTest: retry-based flakiness detection with pass/fail counts and error messages * withTiming + assertWithinMs: performance assertions with configurable limits * createTestContext: convenience factory wiring all components together * Exports testContainerManager singleton for backward compatibility - Expand backend/tests/integration/testContainer.test.ts from 3 smoke tests to 70+ cases covering all components: * Lifecycle: start/stop idempotency, initialSeed, data cleared on restart * Seeding: findById, findAll, findWhere, upsert, delete, count, cleanDatabase (targeted and full), error on missing id * Savepoints: rollback restores state, stacking works correctly * Snapshots: take/restore/getSnapshot, error on unknown id * FixtureLoader: all factories, overrides, unique IDs, fullSubscriptionScenario * DatabaseSeeder: dependency ordering, chaining, withRaw * detectFlakyTest: always-pass, always-fail, intermittent, error capture * TestClock: advance/hours/days, set, date, iso, reset, chaining * TestEventBus: publish/ofType/last/count/assert/clear, copy isolation * RedisFixture: set/get, cleanup, cleanupPattern, trackedCount * withTiming/assertWithinMs: result passthrough, pass/fail cases * createTestContext: full lifecycle smoke test - Update jest.backend.config.js to include backend/tests/**/*.test.ts and backend/billing/tests/**/*.{test,spec}.ts in testMatch; exclude load/ dir - Update docs/integration-tests.md with full API reference, usage examples, per-test isolation pattern, and performance benchmarks Closes Smartdevs17#976
|
@Clement-coder Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Owner
|
Thanks for contributing! The changes have been merged. Feel free to leave a review or feedback. |
|
|
||
| it('delete removes a row and returns true', async () => { | ||
| await mgr.seedDatabase({ users: [{ id: 'u1', email: 'a@b.com' }] }); | ||
| expect(mgr.delete('users', 'u1')).toBe(true); |
| }); | ||
|
|
||
| it('delete returns false for unknown id', async () => { | ||
| expect(mgr.delete('users', 'ghost')).toBe(false); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #976
Refactors the backend testing infrastructure from minimal stubs to a full production-ready test container and fixture system, with 70+ test cases covering every component.
What changed
backend/tests/setup/testContainer.ts(rewritten)TestContainerManagerstartContainer/stopContainer, idempotent start, optionalinitialSeedon bootseedDatabase(data)— bulk inserts with required-id validationcleanDatabase(entities?)— targeted or full clearcreateSavepoint/rollbackToSavepoint— stack-based per-test isolation (analogous to SQL SAVEPOINT)takeSnapshot(id)/restoreSnapshot(id)— named checkpoints for complex scenariosfindById,findAll,findWhere,upsert,delete,countFixtureLoaderplan,user,subscription,merchant,invoiceresetCounter()for deterministic test IDsfullSubscriptionScenario()— builds a complete merchant → plan → user → subscription → invoice graph with all foreign keys wiredDatabaseSeeder— fluent builder inserting in dependency order (merchants → plans → users → subscriptions → invoices)TestClock— deterministic time:advance,advanceHours,advanceDays,set,resetTestEventBus— in-memory event capture:publish,assertPublished,assertNotPublished,ofType,last,count,clearRedisFixture— tracks keys for automatic cleanup, supports glob-pattern cleanupdetectFlakyTest— retry-based flakiness detection returning{ isFlaky, passed, failed, errors, durationMs }withTiming/assertWithinMs— performance assertionscreateTestContext— convenience factory wiring all components:backend/tests/integration/testContainer.test.ts(expanded)From 3 smoke tests to 70+ cases:
jest.backend.config.js(updated)backend/tests/**/*.test.tsandbackend/billing/tests/**/*.{test,spec}.tstotestMatchbackend/tests/load/(load tests are not unit/integration tests)docs/integration-tests.md(updated)Full API reference, usage examples, per-test isolation pattern, and performance benchmark targets.
Acceptance criteria