Introduction to Jest CLI
Jest CLI is a powerful command-line tool that helps developers run and manage tests in JavaScript applications. It provides numerous options for running tests, collecting coverage information, and managing test configurations.
Jest CLI Commands
Here are some of the most commonly used Jest CLI commands:
Running Tests
jest
This command runs all tests.
Running Tests in a Specific File
jest path/to/test/file.js
This command runs tests in the specified file.
Running Tests in Watch Mode
jest --watch
This command runs tests in watch mode, re-running them when any file changes.
Collecting Test Coverage
jest --coverage
This command collects and displays test coverage information.
Running Tests in a Specific Directory
jest path/to/tests/
This command runs all tests in the specified directory.
Using a Specific Configuration File
jest --config=jest.config.js
This command runs tests using the specified configuration file.
Example Application
Below is an example application with a simple add function and corresponding test cases.
Application Code (app.js)
// app.js
function add(a, b) {
return a + b;
}
module.exports = add;
Test Case (app.test.js)
// app.test.js
const add = require('./app');
test('adds 1 + 2 to equal 3', () => {
expect(add(1, 2)).toBe(3);
});
test('adds 5 + 7 to equal 12', () => {
expect(add(5, 7)).toBe(12);
});
Running the Tests
Use the following command to run the tests:
jest
After running the command, you should see the test results in your terminal.
Jest CLI is an essential tool for JavaScript developers to ensure their code is reliable and bug-free. With the commands and examples provided, you can start integrating Jest CLI into your development workflow today.
Hash: fc81f98e776b3b04bea93d942ff872889ab665853988a81a94ee010fde3c745b