Jest - JavaScript testing framework
Jest is a delightful JavaScript testing framework with a focus on simplicity.
Vortex comes with pre-configured Jest setup for testing JavaScript in custom Drupal modules.
For writing tests, mocking Drupal globals, and test templates, see the Jest development guide.
Usage
- Ahoy
- Docker Compose
ahoy test-js # Run all Jest tests.
docker compose exec cli bash -c "yarn test" # Run all Jest tests.
Running tests matching a pattern
ahoy test-js does not forward extra arguments, so run subsets through
ahoy cli:
- Ahoy
- Docker Compose
ahoy cli "yarn test ys_demo"
docker compose exec cli bash -c "yarn test ys_demo"
Running a specific test by name
- Ahoy
- Docker Compose
ahoy cli "yarn test -t 'should increment the value'"
docker compose exec cli bash -c "yarn test -t 'should increment the value'"
Configuration
See Jest configuration reference.
All global configuration takes place in the jest.config.js file.
By default, Jest will discover and run test files in web/modules/custom/*/js/
directories and their subdirectories. Test files must use the .test.js
extension and live in a tests/ subdirectory next to the source files they
test.
The configuration uses the jsdom test environment to provide browser globals
like document, window, and localStorage.
Test discovery
The jest.config.js file dynamically scans web/modules/custom/ for modules
that contain a js/ directory, and adds each as a root for test discovery. This
means adding a new custom module with JavaScript tests requires no configuration
changes.
Writing tests
Test files live in a tests/ subdirectory next to the source files they test:
web/modules/custom/my_module/
└── js/
├── my_module.js # Source file
└── tests/
└── my_module.test.js # Test file
Tests load the source file with require() after setting up the required
globals, and the .eslintrc.json override for *.test.js files enables the
jest environment and allows global-require to support that pattern.
For the test template, loading Drupal behaviors, and mocking globals, see the Jest development guide.
Ignoring fail in continuous integration pipeline
This tool runs in continuous integration pipeline by default and fails the build if there are any violations.
Set VORTEX_CI_JEST_IGNORE_FAILURE environment variable to 1 to ignore
failures. The tool will still run and report violations, if any.