Node.js includes a built-in test runner for writing tests without external dependencies. Here's how to use it.
Basic Usage
Subtests
describe and it
Hooks
Skipping and Focusing
Mocking
Timers
Assertions
Snapshot Testing
Coverage
Test Configuration
Running Tests
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.