Best practices for writing unit and integration tests for the G2 visualization library.
Install
mkdir -p .claude/skills/g2-unit-testing-skills && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/8256" && unzip -o skill.zip -d .claude/skills/g2-unit-testing-skills && rm skill.zipInstalls to .claude/skills/g2-unit-testing-skills
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Guidelines and best practices for writing unit tests in the G2 visualization library, covering directory structure, testing patterns, and implementation guidelines. Use when need to generate test.Key capabilities
- →Organize unit tests by module
- →Implement integration tests for charts
- →Export chart configurations as G2Spec
- →Run specific test files or patterns
How it works
The skill provides a directory structure and testing patterns for G2, using Jest for unit tests and screenshot-based integration tests for chart rendering.
Inputs & outputs
When to use g2-unit-testing-skills
- →Write G2 data transformation tests
- →Generate unit tests for G2 components
- →Create visual integration tests for charts
- →Test chart API modules
About this skill
Document Information
Document Title: G2 Unit Testing Skill Specification Version: 1.0 Author: G2 Development Team Purpose: Guide for writing effective unit tests in the G2 visualization library
Overview
This skill specification provides comprehensive guidelines for writing unit tests in the G2 visualization library. It covers directory structure, testing patterns, best practices, and implementation guidelines to ensure consistent and maintainable test code.
Prerequisites
Before writing unit tests for G2, ensure you have:
- Understanding of TypeScript and Jest testing framework
- Familiarity with G2's codebase structure
- Knowledge of the specific module you are testing
Test Directory Structure
G2 organizes tests into two main categories:
Unit Tests (__tests__/unit/)
Unit tests are for testing pure data modules or functions. They are located in __tests__/unit/ and organized by module:
api/- Chart API related testsdata/- Data transformation testsscale/- Scale related testsencode/- Encoding related testscomponent/- Component related testsutils/- Utility function tests- And more...
Integration Tests (__tests__/plots/)
Integration tests test the rendering results of the entire visualization chart by comparing screenshots. Located in __tests__/plots/, these are organized by focus areas:
static/- Static drawing related casesanimation/- Animation related casesapi/- Chart API related casesinteraction/- Interaction related casestooltip/- Tooltip related caseslegend/- Legend related casesbugfix/- Bug fix related cases
Unit Test Implementation Guidelines
Test Structure
Follow the standard Jest testing structure:
import { YourFunction } from '../../../src/your-module';
describe('YourFunction', () => {
it('should perform expected behavior', () => {
// Test implementation
expect(result).toBe(expected);
});
});
Integration Test Guidelines
Chart Configuration Tests
Integration tests typically export chart configurations:
import { G2Spec } from '../../../src';
export function alphabetInterval(): G2Spec {
return {
type: 'interval',
transform: [{ type: 'sortX', by: 'y', reverse: true }],
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: {
y: { labelFormatter: '.0%' },
},
encode: {
x: 'letter',
y: 'frequency',
},
};
}
Export Convention
- Export each test case as a function that returns a
G2Spec - Name the export function descriptively
- Add the export to the appropriate
index.tsfile to make it visible in the preview environment
Testing Workflow
Running Tests
To run all tests locally:
npm run test
To run tests on demand (specific test files or directories):
# Run specific test file
npm run test -- path/to/your/test-file.test.ts
# Run tests matching a pattern
npm run test -- --testNamePattern="legend"
This ensures your changes pass all existing tests before submitting a pull request.
9.2 Adding New Tests
- Determine the appropriate directory for your test
- Follow existing patterns in the same directory
- Export your test case in the corresponding index.ts file
10. Troubleshooting
Common issues and solutions:
- If tests fail due to missing dependencies, ensure all imports are correct
- For async test issues, verify proper use of async/await or Promises
- If tests are slow, check for unnecessary computations or network requests
11. References
- Review existing tests in the codebase for examples
- Follow the existing patterns and conventions
- When in doubt, refer to similar tests in the same module
When not to use it
- →When testing code outside the G2 visualization library
Prerequisites
Limitations
- →Requires familiarity with G2 codebase structure
How it compares
It enforces a specific G2-compatible structure and export convention for chart tests compared to generic unit testing.
Compared to similar skills
g2-unit-testing-skills side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| g2-unit-testing-skills (this skill) | 0 | 7mo | Review | Intermediate |
| dependency-upgrade | 26 | 5mo | Review | Intermediate |
| angular-best-practices | 21 | 3mo | No flags | Advanced |
| validate-typescript | 5 | 9mo | Review | Beginner |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by antvis
View all by antvis →You might also like
dependency-upgrade
wshobson
Manage major dependency version upgrades with compatibility analysis, staged rollout, and comprehensive testing. Use when upgrading framework versions, updating major dependencies, or managing breaking changes in libraries.
angular-best-practices
sickn33
Angular performance optimization and best practices guide. Use when writing, reviewing, or refactoring Angular code for optimal performance, bundle size, and rendering efficiency.
validate-typescript
BerryKuipers
Run TypeScript compiler type-checking (tsc --noEmit) to validate type safety and catch type errors. Works with any TypeScript project. Returns structured output with error counts, categories (type/syntax/import errors), and affected files. Used for quality gates and pre-commit validation.
ts-testing
johnlindquist
Design, implement, and maintain high‑value TypeScript test suites using popular JS/TS testing libraries. Use this skill whenever the user is adding tests, debugging failing tests, or refactoring code that should be covered by tests.
react-best-practices
redpanda-data
Client-side React performance optimization patterns.
feature-flags
Use when feature flag tests fail, flags need updating, understanding @gate pragmas, debugging channel-specific test failures, or adding new flags to React.