Integration Testing Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Integration Testing runs in O(n) time and O(n) space, in JavaScript as in every other language.
- ✓Node ships a runner: node:test gives describe, it, before, beforeEach and their option bags, node:assert/strict gives the assertions, and node --test discovers files.
- ✓Reviewers want the real collaborator instead of jest.mock, hooks that return promises rather than take a done callback, and node:assert/strict so 1 never equals "1".
- ✓The JavaScript trap: node-postgres hands back int8 and numeric columns as STRINGS, because a float64 cannot represent every int64 exactly, so SELECT count(*) yields "1" and assert.strictEqual(row.count, 1) fails on a correct query; cast in SQL or parse deliberately.
- ✓Container start is one to three seconds with the image cached; amortise it in a before hook, not per file, or you pay it per child process.
What Integration Testing actually is
Integration Testing in JavaScript: what you actually reach for
JavaScript implementation
import { after, before, beforeEach, afterEach, test } from 'node:test';
import assert from 'node:assert/strict';
import { PostgreSqlContainer } from '@testcontainers/postgresql';
import pg from 'pg';
let container, pool, client;
before(async () => {
container = await new PostgreSqlContainer('postgres:16-alpine').start();
pool = new pg.Pool({ connectionString: container.getConnectionUri(), max: 2 });
await pool.query('CREATE TABLE orders (id serial PRIMARY KEY, sku text NOT NULL)');
}, { timeout: 120_000 });
after(async () => {
await pool.end();
await container.stop();
});
beforeEach(async () => {
client = await pool.connect(); // one real connection, not the pool
await client.query('BEGIN');
});
afterEach(async () => {
await client.query('ROLLBACK');
client.release();
});
test('insert is visible only inside the transaction', async () => {
await client.query('INSERT INTO orders (sku) VALUES ($1)', ['ABC-1']);
const { rows } = await client.query('SELECT count(*)::int AS n FROM orders');
assert.equal(rows[0].n, 1); // ::int, else pg returns the string "1"
});
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Integration Testing?
Node ships a runner: node:test gives describe, it, before, beforeEach and their option bags, node:assert/strict gives the assertions, and node --test discovers files. It runs each test FILE in its own child process, so nothing at module scope is shared between files. Real dependencies come from @testcontainers/postgresql (new PostgreSqlContainer(image).start(), then getConnectionUri()) and the pg driver's Pool and Client. fetch and undici are built in, so an HTTP integration test needs no dependency. Vitest and Jest remain popular mainly for watch mode and mocking, which this style of test does not need.
What is the most common JavaScript mistake when implementing Integration Testing?
node-postgres hands back int8 and numeric columns as STRINGS, because a float64 cannot represent every int64 exactly, so SELECT count(*) yields "1" and assert.strictEqual(row.count, 1) fails on a correct query; cast in SQL or parse deliberately. Second, pool.query() checks out an arbitrary connection per call, so BEGIN on the pool and the following INSERT can land on two different backends and your rollback protects nothing; take a client with pool.connect(). Third, one forgotten await leaves a handle open and the single event loop keeps the process alive after the last assertion, so the suite hangs rather than failing.
How does Integration Testing actually perform in JavaScript?
Container start is one to three seconds with the image cached; amortise it in a before hook, not per file, or you pay it per child process. The runner fans out roughly one process per core, so N files means N pools, and Postgres defaults to 100 max_connections; cap each pool at two or three. A per-test ROLLBACK over a local socket is well under a millisecond, TRUNCATE ... RESTART IDENTITY CASCADE across a handful of tables a few milliseconds, and dropping and recreating a database orders of magnitude more. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for GitHub Copilot, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.