What are the different ways to run Jest tests?

Beginner

Answer

Jest can be run in several ways:

  1. Basic test run: npm test or jest
  2. Watch mode: jest --watch (watches for file changes)
  3. Watch all: jest --watchAll (watches all files)
  4. Coverage: jest --coverage (generates coverage report)
  5. Specific file: jest mytest.test.js
  6. Pattern matching: jest --testNamePattern="should add"
  7. Verbose output: jest --verbose

Common package.json scripts:

{
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage",
    "test:debug": "jest --verbose"
  }
}