Skip to content

Commit a94c242

Browse files
committed
test: improve test descriptions and remove redundant edge cases
- Clarify test descriptions to be more descriptive and positive - Remove duplicate tests for undefined/null processor return values - Clean up test suite while maintaining comprehensive coverage
1 parent 16378f6 commit a94c242

2 files changed

Lines changed: 62 additions & 42 deletions

File tree

CLAUDE.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Development Commands
6+
7+
### Testing
8+
- `npm test` - Run all tests using Jest
9+
- `npm run test:coverage` - Run tests with coverage report
10+
- `npx jest --testNamePattern="test name"` - Run a specific test by name pattern
11+
12+
### Project Structure
13+
14+
This is a PostCSS plugin that processes CSS units through a configurable processor function. It's a single-purpose plugin with a focused architecture.
15+
16+
## Core Architecture
17+
18+
The plugin consists of a main entry point (`index.js`) with several key architectural components:
19+
20+
### Main Plugin Function (`module.exports`)
21+
- Returns a PostCSS plugin object with `postcssPlugin`, `Once`, `Declaration`, and `AtRule` hooks
22+
- Handles plugin configuration merging with defaults
23+
- Manages file exclusion logic and unit regex compilation
24+
25+
### Key Processing Components
26+
27+
1. **Unit Detection & Regex (`createUnitRegex`)**
28+
- Combines default CSS units with custom units from options
29+
- Creates a regex that excludes quoted strings and URLs but matches unit values
30+
- Pattern: `"[^"]+"|'[^']+'|url\([^)]+\)|var\([^)]+\)|(\d*\.?\d+)(${unitStr})`
31+
32+
2. **Property List Matching (`createPropListMatcher`)**
33+
- Supports wildcards (`*`), exact matches, and negation patterns (`!`)
34+
- Handles `startWith`, `endWith`, `contain`, and their negative variants
35+
- Returns a function that tests if a CSS property should be processed
36+
37+
3. **Unit Processing (`createUnitReplace`)**
38+
- Calls the user-provided processor function with value, unit, node, and root
39+
- Handles different processor return types: number, string, or object with value/unit
40+
- Applies precision rounding via `toFixed` helper
41+
42+
4. **Selector Blacklisting (`blacklistedSelector`)**
43+
- Supports string includes and regex pattern matching
44+
- Used to skip processing for specific CSS selectors
45+
46+
### Plugin Hooks
47+
48+
- **`Once`**: Sets up file exclusion logic and initializes the unit replacement function
49+
- **`Declaration`**: Processes CSS property values, handles replacement vs. cloning
50+
- **`AtRule`**: Processes media query parameters when `mediaQuery` option is enabled
51+
52+
### Configuration Options
53+
54+
The plugin accepts extensive configuration through the options object, with sensible defaults for all settings. Key options include processor function, unit precision, property filtering, selector blacklisting, and file exclusion patterns.
55+
56+
### Type Safety
57+
58+
TypeScript definitions are provided in `index.d.ts` with comprehensive interface documentation for all configuration options and the processor function signature.

index.test.js

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe('postcss-unit-processor', () => {
135135
});
136136

137137
// Test exclude option with function
138-
it('should not exclude files when file path is undefined', async () => {
138+
it('should process CSS when file path is undefined', async () => {
139139
const processor = (value, unit) => {
140140
if (unit === 'px') {
141141
return { value: value * 2, unit: 'px' };
@@ -148,7 +148,7 @@ describe('postcss-unit-processor', () => {
148148
});
149149

150150
// Test exclude option with string
151-
it('should not exclude files when file path does not match string', async () => {
151+
it('should process CSS when file path does not match exclude string', async () => {
152152
const processor = (value, unit) => {
153153
if (unit === 'px') {
154154
return { value: value * 2, unit: 'px' };
@@ -161,7 +161,7 @@ describe('postcss-unit-processor', () => {
161161
});
162162

163163
// Test exclude option with regex
164-
it('should not exclude files when file path does not match regex', async () => {
164+
it('should process CSS when file path does not match exclude regex', async () => {
165165
const processor = (value, unit) => {
166166
if (unit === 'px') {
167167
return { value: value * 2, unit: 'px' };
@@ -420,7 +420,7 @@ describe('postcss-unit-processor', () => {
420420
});
421421

422422
// Test blacklistedSelector function directly by manipulating parent selector
423-
it('should handle declaration with non-string parent selector', async () => {
423+
it('should handle selector blacklist check with edge cases', async () => {
424424
const processor = (value, unit) => {
425425
if (unit === 'px') {
426426
return { value: value * 2, unit: 'px' };
@@ -521,21 +521,6 @@ describe('postcss-unit-processor', () => {
521521
);
522522
});
523523

524-
// Test processor returning object with undefined value
525-
it('should handle processor returning object with undefined value', async () => {
526-
const processor = (value, unit) => {
527-
if (unit === 'px') {
528-
return { value: undefined, unit: 'px' };
529-
}
530-
return { value, unit };
531-
};
532-
await testProcess(
533-
'div { width: 100px; }',
534-
'div { width: 0; }',
535-
{ processor }
536-
);
537-
});
538-
539524
// Test processor returning object with falsy unit
540525
it('should handle processor returning object with falsy unit', async () => {
541526
const processor = (value, unit) => {
@@ -551,21 +536,6 @@ describe('postcss-unit-processor', () => {
551536
);
552537
});
553538

554-
// Test processor returning object with null unit
555-
it('should handle processor returning object with null unit', async () => {
556-
const processor = (value, unit) => {
557-
if (unit === 'px') {
558-
return { value: 50, unit: null };
559-
}
560-
return { value, unit };
561-
};
562-
await testProcess(
563-
'div { width: 100px; }',
564-
'div { width: 50px; }',
565-
{ processor }
566-
);
567-
});
568-
569539
// Test processor returning string number
570540
it('should handle processor returning string number', async () => {
571541
const processor = (value, unit) => {
@@ -650,12 +620,4 @@ describe('postcss-unit-processor', () => {
650620
expect(result.warnings()).toHaveLength(0);
651621
});
652622

653-
// Test unitProcessor with undefined options (default parameter)
654-
it('should handle unitProcessor called with undefined options', async () => {
655-
const input = 'div { width: 100px; }';
656-
const result = await postcss([unitProcessor(undefined)])
657-
.process(input, { from: undefined });
658-
expect(result.css).toEqual('div { width: 100px; }');
659-
expect(result.warnings()).toHaveLength(0);
660-
});
661623
});

0 commit comments

Comments
 (0)