Back to Blog
Node.jsTestingTest RunnerBuilt-in

Node.js Test Runner Guide

Master the built-in Node.js test runner for writing and running tests without external frameworks.

B
Bootspring Team
Engineering
October 10, 2019
7 min read

Node.js includes a built-in test runner for writing tests without external dependencies. Here's how to use it.

Basic Usage#

Loading code block...

Subtests#

Loading code block...

describe and it#

Loading code block...

Hooks#

Loading code block...

Skipping and Focusing#

Loading code block...

Mocking#

Loading code block...

Timers#

Loading code block...

Assertions#

Loading code block...

Snapshot Testing#

Loading code block...

Coverage#

Loading code block...

Test Configuration#

Loading code block...

Running Tests#

Loading code block...

Best Practices#

Structure: ✓ Use describe/it for organization ✓ Keep tests focused ✓ Use hooks for setup/cleanup ✓ Group related tests Mocking: ✓ Mock external dependencies ✓ Restore mocks after tests ✓ Use timers mock for time ✓ Spy on methods Assertions: ✓ Use strictEqual for primitives ✓ Use deepStrictEqual for objects ✓ Test error conditions ✓ Be specific in assertions Avoid: ✗ Testing implementation details ✗ Shared mutable state ✗ Flaky tests ✗ Too many assertions per test

Conclusion#

The Node.js test runner provides a complete testing solution without external dependencies. Use test or describe/it for organization, hooks for setup/cleanup, and the mock API for test doubles. Run with --test flag and add coverage with --experimental-test-coverage.

Share this article

Help spread the word about Bootspring

Related articles