A comprehensive testing template for Node.js applications, utilizing the built-in Node.js test runner and a variety of testing techniques.
Use npm run <script-name>
to execute the following scripts:
Script Name | Description |
---|---|
start |
Runs the main application file (main.js ). |
dev |
Runs the application in watch mode, restarting on file changes. |
test |
Executes the test suite with experimental features like coverage and snapshots. |
test:watch |
Runs tests in watch mode, re-running on file changes. |
test:coverage |
Runs tests with coverage reporting. |
test:all |
Executes all tests with experimental features enabled. |
test:all:update |
Runs all tests and updates snapshots. |
Located in the test/
directory, these files demonstrate various testing scenarios and techniques:
Test File | Description |
---|---|
alias.test.js |
Using aliases in tests. |
async.test.js |
Testing asynchronous functions. |
concurrent.test.js |
Running tests concurrently. |
custom-reporter.js |
Implementing a custom test reporter. |
error.test.js |
Error handling and testing. |
external-dependency.test.js |
Mocking external dependencies. |
hooks.test.js |
Utilizing setup and teardown hooks. |
main.test.js |
Testing main application logic. |
mock.test.js |
Various mocking techniques. |
only-skip.test.js |
Focusing on specific tests or skipping others. |
parameterized.test.js |
Parameterized testing for multiple scenarios. |
performance.test.js |
Basic performance testing. |
promise.test.js |
Testing promise-based code. |
setup-teardown.test.js |
Setup and teardown for test suites. |
snapshot.test.js |
Snapshot testing for data structure changes. |
stream.test.js |
Testing Node.js streams for data flow. |
These files serve as references for implementing diverse testing strategies in your Node.js projects.
Ensures various mathematical functions work correctly:
- Basic operations: addition, subtraction, multiplication, division
- Advanced functions: power, square root, factorial, Fibonacci sequence
- Prime number checking, random number generation
- Trigonometric and hyperbolic functions
- Rounding and floor operations
Validates asynchronous operations:
asynchronouslyGetData
: Successfully returns dataasynchronouslyFailToGetData
: Throws an error as expected
Demonstrates running tests concurrently to speed up I/O-bound test suites:
- Two tests running with a 1-second delay each
Implements and uses a custom test reporter:
- Logs the completion of each test
Tests error throwing and handling mechanisms:
- Verifies specific errors are thrown under certain conditions
Mocks external dependencies for isolated testing:
- Mocks an external function and tests its behavior
Uses setup and teardown hooks:
- Utilizes
before
andafter
hooks to prepare and clean the test environment
Explores various mocking techniques:
- Mocking simple functions and module dependencies
- Spying on method calls
- Mocking and restoring timers and console methods
Focuses on specific tests or skips others:
- Always runs one test
- Skips another test
Writes efficient tests for similar scenarios:
- Tests addition with multiple input sets
Measures execution time of functions:
- Executes and times function performance
Tests promise-based asynchronous code:
- Validates both resolved and rejected promises
Manages test environment setup and cleanup:
- Sets up before tests and cleans up afterward
Detects unexpected changes in data structures:
- Compares complex objects to stored snapshots
- Handles cases with no or incorrect snapshots
Ensures correct data flow in Node.js streams:
- Tests reading from a
Readable
stream and writing to aWritable
stream
To execute all tests, run:
npm test
For additional options, use the available scripts listed above.