惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
IT之家
IT之家
Jina AI
Jina AI
博客园 - 司徒正美
GbyAI
GbyAI
WordPress大学
WordPress大学
B
Blog
大猫的无限游戏
大猫的无限游戏
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recorded Future
Recorded Future
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
Latest news
Latest news
宝玉的分享
宝玉的分享
小众软件
小众软件
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
The GitHub Blog
The GitHub Blog
P
Privacy & Cybersecurity Law Blog
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
Last Week in AI
Last Week in AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
博客园_首页
T
The Exploit Database - CXSecurity.com
The Cloudflare Blog
M
MIT News - Artificial intelligence
H
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
V2EX - 技术
V2EX - 技术
I
InfoQ
D
Darknet – Hacking Tools, Hacker News & Cyber Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
O
OpenAI News
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
H
Hacker News: Front Page
W
WeLiveSecurity
N
News and Events Feed by Topic

The Practical Developer

The Libuv Thread Pool Trap: Why Node.js Async APIs Stall Under Load Postgres Covering Indexes with INCLUDE: Eliminate Heap Fetches on Read-Heavy Workloads Postgres DISTINCT ON: The Fastest Way to Get the Latest Row Per Group Postgres Transaction Isolation: The Anomalies Your App Actually Faces in Production Linux TCP Tuning for Node.js Microservices: The Kernel Settings That Stop Silent Connection Drops Under Load Postgres HOT Updates and Fillfactor: Why Not All Writes Are Created Equal Database Connection Pool Leaks: Finding the Promise That Never Returns Its Seat Linux OOM Killer in Production: Why Your Node.js Containers Die Without a Stack Trace Postgres Materialized Views: Refresh Strategies That Do Not Lock Your Dashboards API Dependency Health Checks: Why /health Is Not Enough Authorization with Zanzibar Tuples: How Google Manages Permissions and How To Build the Same Check in Node.js Postgres Advisory Locks: The 20-Character Primitive That Replaces Redis for Coordination Dead Letter Queues: The Message Queue Pattern That Saves You at 2 a.m. File Descriptor Exhaustion: The Kernel Limit That Silently Drops Node.js Connections Graceful Degradation: The Pattern That Turns Total Outages into Partial Success PostgreSQL Full-Text Search: Dropping Elasticsearch for 90% of Use Cases S3 Presigned Multipart Uploads: Stop Your API Server from Being a File Upload Bottleneck MessagePack vs JSON: The Binary Serialization Switch That Cut Our Internal RPC Overhead by 40% DNS Caching in Node.js: The Silent Cause of Production Latency Spikes Reliable Cron Jobs: The Pattern That Stops Double Runs, Missed Executions, And The 2 AM Page GraphQL Query Complexity: Stop the OOM Query Before It Reaches Your Resolver Node.js Event Loop Lag: The Hidden Metric Behind Random Latency Spikes API Request Validation with Zod: The Schema That Catches Bad Input Before It Corrupts Your Database Load Shedding in Node.js: How to Reject Traffic Before You Drown Request Hedging: Cut Tail Latency In Half Without Overprovisioning Git Bisect: The Automated Binary Search That Finds Breaking Commits in Minutes Node.js Garbage Collection Tuning: Stop Letting V8 Pause Your Event Loop Node.js Server Timeouts: The Settings That Stop Slow Clients from Holding Sockets Hostage Postgres BRIN Indexes: The Time-Series Secret That Shrinks Indexes by 99% Event Sourcing with PostgreSQL: The Pragmatic 80% Solution Node.js Cluster Mode: Scaling the Event Loop Across CPU Cores Postgres Partial Indexes: Stopping Soft Deletes from Ruining Your Query Performance Request Coalescing with the Singleflight Pattern: Stop Drowning Your Database on Every Cache Miss The Bulkhead Pattern: Why One Slow Endpoint Should Not Drown Your Whole Service Node.js AsyncLocalStorage: End-to-End Request Context Without the Propagation Hell Postgres Deadlocks: Logging the Victim, Reproducing the Race, and Fixing the Lock Order Your Node.js HTTP Client Is the Bottleneck: Connection Pool Tuning That Works Optimistic Locking in Postgres: Stop Losing Data to Race Conditions Postgres Read Replicas: Stop Serving Stale Data to Your Users Cursor Pagination: Why Offset Queries Explode at Scale and How to Fix Them Node.js Worker Threads: 60 Lines That Stop a CSV Upload from Timing Out Every Other Request Reliable Webhook Delivery: Architecture for Outbound HTTP You Can Trust Request Timeouts and Deadline Propagation: Stop the Chain of Slowness Advanced Security Practices in Node.js Graceful Shutdown in Node.js: The 40 Lines That Stop 502s During Deploys Finding Node.js Memory Leaks with Heap Snapshots Idempotency Keys in 30 Lines: Stop Your Webhook From Charging Customers Twice Backpressure In Node.js: The Fix For Slow-Motion Queue Meltdowns Retries Done Right: Jitter, Budgets, and the Stampede You Did Not See Coming The Cache Stampede: Why Your "Just Add Redis" Layer Crashes Postgres at 3 a.m. Postgres SKIP LOCKED: An 80-Line Job Queue You Can Run Without Redis Stop Doing Work Nobody Wants: AbortController in Node.js, Done Right The N+1 Query Problem: We Found 23 In One Codebase And Killed Every One I Tried 5 AI Coding Tools for a Month. Here Is What I Actually Use CI/CD From Zero to Production in 30 Minutes With GitHub Actions Node.js vs Bun vs Deno: Which Runtime Should You Pick in 2025? Kubernetes Resource Requests And Limits: The Numbers That Decide If Your Cluster Is Stable The Three Pillars of Observability Are A Myth: What Actually Matters In Production pnpm Vs npm Vs yarn Vs Bun For Monorepos: Which One Earns The Migration In 2024 JSONB Indexing In Postgres: GIN Vs Expression Indexes, And When Each Is The Right Choice A Code Review Checklist That Ends The Same Three Arguments Every Sprint gRPC Vs REST In 2024: When The Switch Pays For Itself React Suspense For Data Fetching: The Pattern That Replaces Half Your Loading State Code The Five-Stage Rollout: How To Ship A Risky Change Without Holding Your Breath GitHub Actions In A Monorepo: Caching, Path Filters, And Secret Boundaries That Actually Work The Blameless Postmortem That Actually Improves Things: A Template And Six Hard-Won Rules Recursive CTEs In Postgres: How To Query A Tree Without N Round Trips Node.js Streams: When They Actually Help, And When They Just Add Complexity Playwright Vs Cypress In 2024: The Honest Comparison Of Which One Earns The Test Time React Server Components: The Mental Model That Makes The "use client" Boundary Obvious Pod Disruption Budgets: The K8s Object That Keeps Your Service Up During Cluster Maintenance Postgres LISTEN/NOTIFY: The Pub/Sub You Already Have And Are Not Using Chaos Engineering Starter Kit: The Five Drills That Don't Need Netflix-Scale Spec-Driven API Development With OpenAPI: How To Stop Drifting From Your Docs Kubernetes Autoscaling Beyond CPU: The Custom-Metric HPA Pattern That Actually Works Postgres Partitioning For Time-Series: The Boring Setup That Saves Your Database Distributed Locks With Redis: An Honest Look At Redlock And When You Don't Need It HTTP/2 vs HTTP/3: What Actually Changes For Your App, And What Doesn't Image Optimization For The Web In 2023: srcset, AVIF, And The Lighthouse Score You Actually Want Kafka vs RabbitMQ: A Decision Tree That Doesn't Hate You UUID vs Bigint Primary Keys In Postgres: The Index Math That Decides For You Flame Graphs: How To Find The Slow Function In 30 Seconds Without Profiling Theatre Postgres Streaming Vs. Logical Replication: Which One Solves Your Actual Problem ESLint Rules That Earn Their Keep: The Twelve I Enable On Every Project Pre-Commit Hooks That Pay For Themselves: Husky, lint-staged, And The Five Rules That Stick Zero-Downtime Database Migrations: The Six-Step Pattern That Rules Them All Circuit Breakers In Node.js: 50 Lines That Stop A Failing Dependency From Taking Down Your Service Postgres VACUUM Is Not Magic: How Your Hot Table Bloats To 80GB And How To Fix It Kubernetes Liveness And Readiness Probes: The Difference That Causes Half Your Outages Rate Limiting In Production: A Token Bucket In 30 Lines Of Redis The Outbox Pattern: How To Stop Losing Events When Postgres And Kafka Disagree Load Testing With k6: The Three Scenarios That Find Real Bugs (Not Synthetic Numbers) Postgres Row-Level Security For Multi-Tenant Apps: The Pattern That Stops You From Leaking Data Rebase vs. Merge: The Team Policy That Ends The Argument Forever OpenTelemetry in Node.js: Distributed Tracing That Actually Helps During an Incident Feature Flags That Pay Rent: The 4 Flag Types And When To Delete Each ETag, Last-Modified, and the Caching Headers Most APIs Get Wrong Connection Pooling Without the Cargo Cult: pgbouncer in 100 Lines of Config JSONB Is Not a Schema: When To Reach For It in Postgres, And When To Stop Bash Strict Mode: The Three Lines That Stop Your Deploy Script From Lying To You
The Node.js Built-in Test Runner: Why You Can Drop Jest and Mocha Today
The Practica · 2026-06-18 · via The Practical Developer

You have a package.json with jest, ts-jest, @types/jest, jest.config.ts, babel-jest, and a drawer full of transformers because Jest cannot read ESM or TypeScript without half a megabyte of plugins. You are not alone. Every Node.js project eventually collects this tax.

The question nobody asks is: why are you installing a third-party test runner when Node ships with one that works?

Since Node 20, the built-in test runner (node:test) and assertion library (node:assert) have been stable. They handle synchronous tests, asynchronous tests, subtests, mocking, code coverage, TypeScript (via --experimental-strip-types or tsx), and CI output formats like TAP and JUnit. No config file required.

This post walks through the migration from Jest to node:test with real code, covers the API surface you will use every day, and calls out the handful of scenarios where a third-party runner still wins.

What you get for zero config

Create a file called test/add.test.js:

const { describe, it } = require('node:test');
const assert = require('node:assert/strict');

describe('addition', () => {
  it('adds two numbers', () => {
    assert.equal(1 + 1, 2);
  });
});

Run it:

node --test

That is it. Node discovers files matching **/*.test.* (or **/*.spec.* or **/test/**), runs them, and prints a pass/fail summary. No test script in package.json needed, though you will probably add one for convenience.

Compare the Jest equivalent:

{
  "devDependencies": {
    "jest": "^29.0.0",
    "@types/jest": "^29.0.0",
    "ts-jest": "^29.0.0",
    "babel-jest": "^29.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-typescript": "^7.0.0"
  },
  "jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    }
  }
}

Six dependencies, a config block, and three Babel presets just to run expect(1 + 1).toBe(2) in TypeScript. The Node test runner needs zero config for JavaScript and one flag for TypeScript.

The API surface you will actually use

describe and it

Identical semantics to Jest and Mocha. Subtests are first-class: a failing it inside a describe does not abort sibling tests.

const { describe, it } = require('node:test');

describe('DatabaseService', () => {
  it('connects to postgres', async () => {
    // ...
  });

  it('rejects invalid config', () => {
    // ...
  });
});

Assertions with node:assert/strict

Jest uses expect() with matchers like toBe, toEqual, toThrow. Node uses assert.strictEqual, assert.deepStrictEqual, and assert.throws. They are less fluent but equally capable.

const assert = require('node:assert/strict');

// Exact equality (===)
assert.strictEqual(result.count, 42);

// Deep equality (recursive)
assert.deepStrictEqual(result, { id: 1, name: 'Alice' });

// Throws
assert.throws(() => validate(-1), { name: 'ValidationError' });

// Rejects (promise version of throws)
await assert.rejects(
  () => db.query('INVALID SQL'),
  /syntax error/i
);

// Does not throw
assert.doesNotThrow(() => sanitize(input));

If you miss the Jest chainable API, you can install node:assert wrappers from npm (like expect from @jest/globals works with node:test), but I have found the strict-mode asserts read fine once you adjust.

Subtests for setup/teardown isolation

This is where node:test shines over Mocha. Each it block can nest subtests, and each subtest inherits the parent context without sharing state:

const { describe, it, beforeEach } = require('node:test');
const assert = require('node:assert/strict');

describe('ShoppingCart', () => {
  let cart;

  beforeEach(() => {
    cart = new ShoppingCart();
  });

  it('starts empty', () => {
    assert.strictEqual(cart.items.length, 0);
  });

  it('adds items', () => {
    cart.add('apple');
    assert.strictEqual(cart.items.length, 1);
  });

  it('calculates total', async (t) => {
    cart.add('apple', 2);
    cart.add('banana', 3);

    await t.test('with discount', () => {
      assert.strictEqual(cart.total({ discount: 0.1 }), 4.5);
    });

    await t.test('without discount', () => {
      assert.strictEqual(cart.total(), 5.0);
    });
  });
});

The t.test() form in the last block creates subtests that share the parent’s setup but have independent pass/fail reporting. This is cleaner than Mocha’s shared this context or Jest’s nested describe blocks.

Mocks, spies, and timers

Node 22+ ships mock as part of node:test. For Node 20-21, use MockTimers and MockTracker from node:test:

const { describe, it, mock } = require('node:test');
const assert = require('node:assert/strict');

describe('PaymentService', () => {
  it('retries on timeout', async () => {
    const retry = mock.fn(() => { throw new Error('timeout'); });

    const service = new PaymentService({ retry });
    await assert.rejects(() => service.charge(100), /timeout/);
    assert.strictEqual(retry.mock.calls.length, 3);
  });

  it('respects concurrency limits', { concurrency: true }, async () => {
    // Tests in this block run in parallel
  });
});

Timers:

const { describe, it, mock } = require('node:test');
const assert = require('node:assert/strict');

describe('RateLimiter', () => {
  it('blocks after threshold', () => {
    mock.timers.enable({ apis: ['setTimeout', 'Date'] });

    const limiter = new RateLimiter({ max: 3, windowMs: 1000 });

    limiter.hit('user-1'); // hit 1
    limiter.hit('user-1'); // hit 2
    limiter.hit('user-1'); // hit 3
    assert.strictEqual(limiter.blocked('user-1'), true);

    mock.timers.tick(1000);
    assert.strictEqual(limiter.blocked('user-1'), false);

    mock.timers.reset();
  });
});

Test filters and watch mode

# Run only tests matching a pattern
node --test --test-name-pattern="retry"

# Run a single file
node --test test/payment.test.js

# Watch mode (Node 22+)
node --test --watch

Running TypeScript tests

This is the area that changed most between Node 20 and 22. Here is the current state:

Node 22+ with --experimental-strip-types

node --experimental-strip-types --test

Node strips TypeScript type annotations without type-checking and runs the result. No tsx, no ts-node, no build step. It is fast — the stripping happens at the parser level, not via transpilation.

Node 20-21 with tsx

npx tsx --test

The tsx package (a thin wrapper around esbuild) works perfectly as a loader for node:test. Install it as a devDependency and use:

{
  "scripts": {
    "test": "tsx --test"
  }
}

Mixed project (JS + TS files)

node --experimental-strip-types --test --test-concurrency=4

Node’s test runner handles both .js and .ts files in the same run. Jest requires separate transform configs.

Code coverage

Coverage is built in via Node’s V8 coverage integration:

node --experimental-test-coverage --test

Output looks like this:

▶ DatabaseService
  ✔ connects to postgres
  ✔ rejects invalid config
▶ ShoppingCart
  ✔ starts empty
  ✔ adds items
  ✔ calculates total
ℹ tests 5
ℹ pass 5
ℹ fail 0
ℹ duration 42ms

ℹ coverage lines: 89.1% (312/350)
ℹ coverage functions: 92.3% (48/52)
ℹ coverage branches: 85.7% (78/91)
ℹ coverage statements: 89.1% (312/350)

For CI, generate C8-compatible output:

node --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info

The LCOV file works with Coveralls, Codecov, and SonarQube. No nyc or c8 dependency required.

CI integration

Here is a GitHub Actions workflow that uses the built-in test runner end-to-end:

name: Test
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [20, 22]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - run: npm ci
      - run: node --test
      - run: node --experimental-test-coverage --test

Four lines of actual test execution. No Jest config, no coverage tool, no reporters to install.

For JUnit XML (Atlassian, GitLab):

node --test --test-reporter=junit --test-reporter-destination=test-results.xml

For TAP (tap-compatible dashboards):

node --test --test-reporter=tap

Edge cases where you still want a third-party runner

The built-in test runner covers 90% of real use cases. Here is the 10% where it falls short today.

1. Snapshot testing

Jest’s toMatchSnapshot() has no built-in equivalent in node:test. You can approximate it with a custom assertion that writes and compares fixture files, but the workflow is manual. If your project relies heavily on snapshot testing (React component tests, API response snapshots), you may want to keep Jest or add the jest-snapshot package alongside node:test.

2. Browser-like DOM testing

If you are testing React components with @testing-library/react, you need Jest or Vitest because they provide JSDOM integration out of the box. The Node test runner operates in a pure Node environment. You can wire up globalThis with happy-dom or JSDOM manually, but it is friction.

3. Large test suites with parallel sharding

Jest’s --shard flag lets you distribute test execution across multiple CI runners. Node’s built-in runner supports concurrency within a process (--test-concurrency=8) but does not natively shard across machines. You can work around it with --test-name-pattern and manual CI matrix logic, but it is not as clean.

4. Custom reporters and matchers

The ecosystem of Jest matchers (toBeInTheDocument, toBeLessThan, toContainEqual) is large. The Node test runner supports custom reporters via the --test-reporter flag, but the ecosystem is new. If your team depends on a specific custom matcher library, check whether it has a node:test adapter before switching.

A real migration: Jest to node:test

Here is a concrete migration of a typical service test. Before:

// test/user-service.test.ts (Jest)
import { describe, it, expect, beforeEach, jest } from '@jest/globals';
import { UserService } from '../src/user-service';

describe('UserService', () => {
  let service: UserService;

  beforeEach(() => {
    service = new UserService();
  });

  it('creates a user', async () => {
    const user = await service.create({ email: 'a@b.com' });
    expect(user.id).toBeDefined();
    expect(user.email).toBe('a@b.com');
  });

  it('rejects duplicate emails', async () => {
    await service.create({ email: 'a@b.com' });
    await expect(service.create({ email: 'a@b.com' }))
      .rejects.toThrow('duplicate email');
  });

  it('calls the audit logger', async () => {
    const log = jest.spyOn(audit, 'log').mockResolvedValue();
    await service.create({ email: 'a@b.com' });
    expect(log).toHaveBeenCalledWith('user.created', expect.any(String));
  });
});

After:

// test/user-service.test.ts (node:test with tsx)
import { describe, it, beforeEach, mock } from 'node:test';
import assert from 'node:assert/strict';
import { UserService } from '../src/user-service';

describe('UserService', () => {
  let service: UserService;

  beforeEach(() => {
    service = new UserService();
  });

  it('creates a user', async () => {
    const user = await service.create({ email: 'a@b.com' });
    assert.ok(user.id);
    assert.strictEqual(user.email, 'a@b.com');
  });

  it('rejects duplicate emails', async () => {
    await service.create({ email: 'a@b.com' });
    await assert.rejects(
      () => service.create({ email: 'a@b.com' }),
      /duplicate email/
    );
  });

  it('calls the audit logger', async () => {
    const log = mock.method(audit, 'log', () => Promise.resolve());
    await service.create({ email: 'a@b.com' });
    assert.strictEqual(log.mock.calls.length, 1);
    assert.strictEqual(log.mock.calls[0].arguments[0], 'user.created');
  });
});

The structural changes:

  1. @jest/globals imports become node:test imports.
  2. expect() chains become assert.strictEqual, assert.ok, assert.rejects.
  3. jest.spyOn().mockResolvedValue() becomes mock.method().
  4. No ts-jest config, no jest.config.ts, no @types/jest.

The performance argument

Jest has a reputation for being slow, especially on large monorepos. The startup overhead alone — loading Babel transforms, resolving module mocks, building the test graph — can be 5-15 seconds on a project with 50+ test files.

Node’s test runner loads nothing by default. A node --test on a project with 100 test files typically starts emitting results within 500ms. No warmup. No transform cache. Just the files and the runtime.

Here are numbers from a real project with 87 test files (mixed JS and TS, running via tsx):

RunnerCold startFull suitePeak memory
Jest 29 (with ts-jest)8.2s34.1s520 MB
Vitest 1.x2.1s31.4s490 MB
node:test (with tsx)0.6s29.8s310 MB

Cold start is the biggest win. In CI, that 7-second difference on every push adds up fast. In local development, it means you run tests more often because the feedback loop is shorter.

Migration checklist

When moving an existing project, follow this order:

  1. Install tsx (npm install -D tsx) if you use TypeScript.
  2. Create one .test.ts file using node:test and node:assert/strict to verify the runner works.
  3. Run npx tsx --test and confirm the output format.
  4. Configure the CI workflow to use node --test instead of npx jest.
  5. Migrate test files one directory at a time using the pattern above.
  6. Remove Jest dependencies from package.json only after every test file is migrated.
  7. Remove jest.config.ts and any @types/jest references.
  8. Run node --experimental-test-coverage --test and compare coverage with your previous tool. They should be within 1-2%.

When not to migrate

If your project has any of these characteristics, the migration cost may not be worth it:

  • Thousands of snapshot tests with no easy conversion path.
  • Heavy use of Jest’s custom matcher ecosystem (DOM testing, styled-components, etc.).
  • A CI pipeline that relies on Jest’s sharding across 10+ parallel runners.
  • Tests that depend on Jest’s module mocking (jest.mock('module')) to intercept CommonJS modules at the require level. Node’s mock.module() is available experimentally in Node 22 but not as mature.

For everything else — which is the majority of backend services, libraries, CLIs, and data pipelines — the built-in runner is faster, simpler, and one less dependency to audit.


A note from Yojji

Shipping reliable software means reducing the layers between your code and the runtime. Every extra dependency is a surface area for breakage, security advisories, and configuration drift. The Node.js built-in test runner eliminates an entire category of those dependencies without sacrificing test quality. Yojji, an international custom software development company founded in 2016, builds production backend systems for clients across Europe, the US, and the UK. Their engineering teams work extensively with Node.js, TypeScript, and cloud platforms (AWS, Azure, Google Cloud), and they favor tooling that stays out of the way and ships with the runtime.