Add Unit Tests for eraserTest Function in @excalidraw/excalidraw/eraser #10274
+1,490
−1,848
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request introduces a comprehensive suite of unit tests for the eraserTest function, which is part of the Eraser module in the Excalidraw project. The goal is to improve test coverage and ensure the correctness of the eraser’s behavior when interacting with various element types on the canvas.
The eraserTest function determines whether a segment of the eraser path intersects or affects an element, deciding whether it should be removed. It is a core part of the EraserTrail feature, responsible for interactive element deletion.
Changes Introduced
Added a new test file: packages/excalidraw/tests/eraser.test.tsx
Implemented 7 new test cases (CT1–CT7) covering different interaction scenarios between the eraser and elements:
- Rectangles, lines (open and closed), arrows, and polygons.
- Both positive (element should be erased) and negative (element should remain) cases.
Created mock modules to isolate behavior:
- @excalidraw/element mock for simplified geometry and element checks (isArrowElement, isLineElement, shouldTestInside, etc.).
- @excalidraw/math mock for geometric operations like lineSegmentsDistance and polygonIncludesPointNonZero.
Included TODO comments for future improvements such as testing grouped elements, linked text, zoom variations, and complex FreeDraw cases.
Test Results
All newly implemented tests passed successfully.
The number of tested functions nearly tripled, and over half of the eraser module’s logic is now covered by automated tests. Although branch coverage percentage decreased due to a higher number of conditional paths being tracked, the total tested logic increased substantially.
Next Steps (TO-DO)
Add tests for:
References
Function under test: eraserTest
Test implementation: eraser.test.tsx
Commit: 3230a82