@@ -4,14 +4,14 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
44
55## Project Overview
66
7- ` @sonar/scan ` is an NPM module that triggers SonarQube Server and SonarCloud analyses on JavaScript codebases without requiring specific tools or Java runtime installation. The scanner detects server capabilities and either uses JRE provisioning (preferred, SonarQube 10.6+ ) or falls back to native sonar-scanner-cli.
7+ ` @sonar/scan ` is an NPM module that triggers SonarQube Server and SonarCloud analyses on JavaScript codebases without requiring specific tools or Java runtime installation. The scanner detects server capabilities and either uses JRE provisioning (preferred) or falls back to native sonar-scanner-cli.
88
99## Common Commands
1010
1111| Command | Purpose |
1212| -------------------------- | ------------------------------------------------------------------ |
1313| ` npm run build ` | Compile TypeScript, run license check, generate build/package.json |
14- | ` npm test ` | Run Jest unit tests with coverage |
14+ | ` npm test ` | Run unit tests |
1515| ` npm run test-integration ` | Run integration tests from test/integration/ |
1616| ` npm run format ` | Format code with Prettier |
1717| ` npm run check-format ` | Check formatting without changes |
@@ -20,13 +20,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
2020Run a single test file:
2121
2222``` bash
23- npx jest test/unit/properties.test.ts
24- ```
25-
26- Run tests matching a pattern:
27-
28- ``` bash
29- npx jest --testNamePattern=" should build properties"
23+ npx tsx --test test/unit/properties.test.ts
3024```
3125
3226## Architecture
@@ -87,49 +81,73 @@ logWithPrefix(LogLevel.INFO, 'ComponentName', 'message');
8781
8882Prettier config: 100 char width, trailing commas, single quotes, LF line endings. Pre-commit hook auto-formats staged files.
8983
84+ ### Pre-commit Checks
85+
86+ Before committing, verify there are no TypeScript errors and no unused exports:
87+
88+ ``` bash
89+ npx tsc --noEmit -p tsconfig.json
90+ npx tsc --noEmit -p test/unit/tsconfig.json
91+ npx knip
92+ ```
93+
94+ Note: ` test/integration/tsconfig.json ` requires running ` npm install ` in ` test/integration/ ` first, as it depends on the build artifact.
95+
96+ ### External Tools
97+
98+ - Use ` gh ` CLI to interact with GitHub (issues, PRs, etc.)
99+ - Use ` acli ` tool to interact with Jira
100+
101+ ### Node.js Imports
102+
103+ Always use the ` node: ` prefix for Node.js built-in modules:
104+
105+ ``` typescript
106+ // Correct
107+ import path from ' node:path' ;
108+ import { spawn } from ' node:child_process' ;
109+ import { EventEmitter } from ' node:events' ;
110+
111+ // Incorrect
112+ import path from ' path' ;
113+ import { spawn } from ' child_process' ;
114+ ```
115+
116+ ### Type Imports
117+
118+ Use ` import type ` when importing types only. This helps with tree-shaking and makes it clear which imports are only used for type checking:
119+
120+ ``` typescript
121+ // When all imports are types, use import type
122+ import type { ScannerProperties , ScanOptions } from ' ./types' ;
123+ import type { ChildProcess , SpawnOptions } from ' node:child_process' ;
124+
125+ // When mixing types and values, use inline type modifier
126+ import { type ChildProcess , spawn } from ' node:child_process' ;
127+ import { type ScannerProperties , ScannerProperty } from ' ./types' ;
128+
129+ // Incorrect: importing types as regular imports
130+ import { ScannerProperties , ScanOptions } from ' ./types' ;
131+ ```
132+
90133## Testing
91134
92135### Unit Tests
93136
94137Location: ` test/unit/ `
95138
96- ```
97- test/unit/
98- ├── tsconfig.json # TypeScript config for unit tests
99- ├── setup.ts # Jest setup - mocks logging to suppress output
100- ├── mocks/ # Test mocks (ChildProcessMock, FakeProjectMock)
101- ├── fixtures/ # Test fixture projects
102- └── *.test.ts # Test files
103- ```
139+ Uses Node's native test runner with tsx. Mocking is done via dependency injection and ` mock.fn() ` / ` mock.method() ` from ` node:test ` .
104140
105141Run a single unit test:
106142
107143``` bash
108- npx jest test/unit/properties.test.ts
109- npx jest --testNamePattern=" should build properties"
144+ npx tsx --test test/unit/properties.test.ts
110145```
111146
112147### Integration Tests
113148
114149Integration tests run end-to-end scans against a real SonarQube instance. Uses Node's native test runner with tsx.
115150
116- ** Structure:**
117-
118- ```
119- test/integration/
120- ├── package.json # Separate npm project (ESM, tsx, node:test)
121- ├── tsconfig.json # TypeScript config for integration tests
122- ├── scanner.test.ts # Integration tests (API and CLI)
123- ├── orchestrator/ # SonarQube lifecycle management
124- │ ├── download.ts # Download SonarQube
125- │ ├── sonarqube.ts # Start/stop, API calls
126- │ ├── index.ts # Exports
127- │ └── stop.java # Java helper to stop SonarQube
128- └── fixtures/
129- └── fake_project_for_integration/
130- └── src/index.js # Test project with intentional code issue
131- ```
132-
133151** How to run:**
134152
135153``` bash
@@ -140,37 +158,4 @@ npm run build && npm run test-integration
140158cd test/integration && npm test
141159```
142160
143- ** Orchestrator functions (from ` test/integration/orchestrator/ ` ):**
144-
145- Manages a local SonarQube instance:
146-
147- - Downloads SonarQube Community Edition (cached in ` ~/.sonar/sonarqube/ ` )
148- - Starts/stops SonarQube on localhost:9000
149- - Generates authentication tokens
150- - Creates test projects
151- - Polls for analysis completion
152-
153- Key functions:
154-
155- - ` getLatestSonarQube() ` - Download/cache SonarQube
156- - ` startAndReady(sqPath, maxWaitMs) ` - Start and wait until operational
157- - ` stop(sqPath) ` - Stop SonarQube instance
158- - ` generateToken() ` - Generate GLOBAL_ANALYSIS_TOKEN
159- - ` createProject() ` - Create project with random key
160- - ` waitForAnalysisFinished(maxWaitMs) ` - Poll until analysis queue empty
161- - ` getIssues(projectKey) ` - Fetch detected issues
162-
163- ** What the integration test validates:**
164-
165- 1 . SonarQube provisioning and startup
166- 2 . Token generation and project creation
167- 3 . Scanner invocation via API (` scan() ` function)
168- 4 . Scanner invocation via CLI (` npx sonar ` )
169- 5 . Issue detection (fake project has intentional issue at line 21)
170- 6 . Result verification (asserts exactly 1 issue found)
171-
172- ** Configuration:**
173-
174- - Timeout: 500 seconds (SonarQube startup is slow)
175- - SonarQube credentials: admin: admin (hardcoded in orchestrator)
176- - Optional env vars: ` ARTIFACTORY_URL ` , ` ARTIFACTORY_ACCESS_TOKEN ` for SonarQube download
161+ The orchestrator (` test/integration/orchestrator/ ` ) manages a local SonarQube instance for testing.
0 commit comments