This document describes the testing infrastructure, test organization, and procedures for ensuring code quality in anime.js. It covers how to run tests, write new test cases, and use the testing utilities provided by the framework. For information about the general development workflow and setup, see Development Setup. For guidelines on submitting code changes, see Pull Request Guidelines.
The anime.js project uses a comprehensive test suite to verify functionality across browser and Node.js environments. This page documents:
The anime.js testing system is built on the Mocha test framework and organized into domain-specific test suites that cover all major subsystems of the library.
Sources: tests/run.js1-33 tests/suites/draggables.test.js1-10 CONTRIBUTING.md29-31
The test suites are organized by functional domain, with each suite testing a specific subsystem or feature area:
| Test Suite | Purpose | Key Areas Tested |
|---|---|---|
engine.test.js | Core animation engine | Timing, update loop, execution |
animations.test.js | Animation creation and control | animate(), parameter handling |
timelines.test.js | Timeline orchestration | Sequencing, synchronization, labels |
draggables.test.js | Draggable interactions | Mouse/touch events, callbacks, physics |
svg.test.js | SVG animations | Line drawing, motion paths, attributes |
text.test.js | Text manipulation | splitText(), effects, revert |
waapi.test.js | Web Animations API | Native browser animations |
animatables.test.js | Programmable interfaces | createAnimatable() |
tweens.test.js | Tween generation | Value interpolation, decomposition |
keyframes.test.js | Keyframe animations | Multi-point animations |
timings.test.js | Timing calculations | Duration, delay, offsets |
eases.test.js | Easing functions | Springs, beziers, built-in easings |
callbacks.test.js | Lifecycle callbacks | onBegin, onUpdate, onComplete |
controls.test.js | Playback controls | play(), pause(), seek(), cancel() |
stagger.test.js | Stagger effects | Delay patterns, cascading animations |
colors.test.js | Color interpolation | Hex, RGB, HSL color spaces |
units.test.js | Unit handling | px, %, em, vh, vw conversions |
values.test.js | Value parsing | Number extraction, complex values |
utils.test.js | Utility functions | DOM helpers, getters, setters |
scope.test.js | Scope management | createScope(), context isolation |
targets.test.js | Target resolution | Selectors, arrays, NodeLists |
parameters.test.js | Parameter handling | Configuration validation |
promises.test.js | Promise integration | then(), async/await patterns |
directions.test.js | Playback direction | Forward, reverse, alternate |
function-based-values.test.js | Dynamic values | Function-based parameters |
seconds.test.js | Time units | Second-based timing |
build.test.js | Build verification | Module exports, types |
scroll.test.js | Scroll animations | Scroll-linked animations |
leaks.test.js | Memory leak detection | Resource cleanup verification |
Sources: tests/run.js1-30
Browser tests run in a real browser environment and test DOM manipulation, rendering, and browser-specific APIs:
This command opens a browser window that loads tests/index.html which runs all test suites and displays results in the Mocha reporter interface. The browser environment is necessary for testing:
Sources: CONTRIBUTING.md31
Node.js tests verify library functionality in a server-side environment without browser dependencies:
This executes tests/run.js1-33 which imports all test suites and runs them using Mocha with leak detection enabled via mocha.checkLeaks() at tests/run.js31 Node tests are faster than browser tests and suitable for continuous integration pipelines.
Sources: tests/run.js31-32
When adding new functionality, follow this testing workflow as documented in CONTRIBUTING.md23-32:
npm run dev-types to build library files while watching for changestests/suites/npm run test-browser to verify all tests passTests in anime.js follow the Mocha suite() and test() pattern. Here is the structure from tests/suites/draggables.test.js36-50:
Tests for animations are inherently asynchronous. The standard pattern uses a resolve callback:
Key components:
resolve parameteronSettle, onComplete perform assertionsresolve() must be called to complete the testSources: tests/suites/draggables.test.js37-49
Import testing utilities from the library and test helpers:
The expect function provides Chai-style assertions:
expect(value).to.equal(expected)expect(value).to.be.above(threshold)expect(value).to.be.below(threshold)Sources: tests/suites/draggables.test.js1-9
Use createTimer() to test time-based animations:
This pattern:
onBeginonUpdateonCompletecreateTimer() for precise timing controlSources: tests/suites/draggables.test.js103-116
For interactive features like draggables, create synthetic events:
These helpers enable testing of:
Sources: tests/suites/draggables.test.js11-34
Test descriptions should clearly state what is being tested and expected behavior:
Sources: tests/suites/draggables.test.js37-155
Always clean up resources after tests to prevent memory leaks and side effects:
Call revert() on animations, draggables, and other instances. Remove dynamically created DOM elements.
Sources: tests/suites/draggables.test.js77-178
Write tests for edge cases and error conditions:
This test verifies graceful handling when DOM elements are unexpectedly removed.
Sources: tests/suites/draggables.test.js70-79
Verify that callbacks fire the correct number of times:
Track callback invocations with counters and assert expected counts.
Sources: tests/suites/draggables.test.js81-112
The test runner enables leak detection at tests/run.js31:
This feature detects global variable leaks by comparing the global scope before and after tests run. Any unexpected global variables cause test failures.
Sources: tests/run.js31
The dedicated tests/suites/leaks.test.js suite verifies proper cleanup and resource management:
Sources: tests/run.js29
To avoid memory leaks in tests:
.revert() on all animation instancesExample from tests/suites/draggables.test.js51-68:
Sources: tests/suites/draggables.test.js51-68
All test suites are loaded in tests/run.js1-30:
Sources: tests/run.js1-30
To add a new test suite:
tests/suites/ following the naming pattern feature.test.jssuite() and test() functionsBefore creating a pull request, verify the following as specified in CONTRIBUTING.md23-32:
.js filesnpm run dev-types to build anime.esm.js and index.d.tstests/suites/npm run test-browser - all tests passmocha.checkLeaks()The project uses JSDoc for type definitions. All functions must have proper JSDoc annotations. Global types are defined in src/types.js while local types should be defined in their respective files.
Sources: CONTRIBUTING.md18-21
While the project doesn't enforce specific coverage percentages, all new features and bug fixes must include tests. The test should:
Sources: CONTRIBUTING.md23-32
For time-based animations, use these patterns:
onComplete, onSettle, etc.createTimer() for precise timingsetTimeout() for simple delaysAlways call resolve() to complete async tests.
When testing features in Shadow DOM contexts, ensure events bubble correctly as demonstrated in tests/suites/draggables.test.js155-194:
Sources: tests/suites/draggables.test.js155-194
Standard cleanup pattern for all tests:
This ensures resources are freed even if assertions fail.
The testing system integrates with the development workflow documented in Development Setup:
npm run dev or npm run dev-types watches source filesnpm run test:browser or npm run test:node verifies changesSources: CONTRIBUTING.md23-32
Refresh this wiki