SCANNPM-131 Migrate unit tests to Node test runner#386
Conversation
1eef4cd to
26d9c48
Compare
zglicz
left a comment
There was a problem hiding this comment.
Please rework the business logic, so that not all of the methods need to accept the mocks/DI code. This will make it much, much cleaner.
Changes in this sessionRefactor: Centralized Dependency InjectionAddressed the PR feedback to clean up the dependency injection approach. Instead of passing New files created:
Pattern change: // Before: deps parameter in every function
export async function fetchJRE(properties: Props, deps: JavaDeps = {}) { ... }
// After: clean function signatures, deps accessed via getDeps()
export async function fetchJRE(properties: Props): Promise<string> {
const { fs, http } = getDeps();
// ...
}Test pattern: beforeEach(() => {
setDeps({ http: createMockHttpDeps({ fetch: mockFetch }) });
});
afterEach(() => {
resetDeps();
});Removed sinon dependencyReplaced all
Fixed TypeScript errors
Stats
|
- Replace Jest with Node's native test runner and tsx - Remove jest, ts-jest, @types/jest, and jest-sonar-reporter dependencies - Use dependency injection pattern for testability instead of module mocking - Add src/deps.ts for shared dependency interfaces - Update all test files to use node:test and node:assert - Use node: prefix for all Node.js built-in imports - Update CLAUDE.md with current conventions Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> # Conflicts: # package-lock.json # package.json
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The integrity hash changes with every build, causing CI failures. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add nyc and @istanbuljs/nyc-config-typescript dependencies - Create .nycrc configuration for TypeScript coverage - Update test script to run with nyc coverage - Generates lcov report for SonarCloud analysis Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Remove unused spawn import from scanner-cli.ts - Remove unused spawn import from scanner-engine.ts - Use `new Error()` instead of `Error()` in scanner-cli.ts - Remove unused beforeEach import from file.test.ts Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add test for dumpToFile feature in scanner-engine - Add test for npm_config_sonar_scanner_ env var conversion - Add test for CLI debug flag setting verbose mode Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Replace fs-extra with native Node.js fs module throughout the codebase: - fsExtra.remove() -> fs.promises.rm() with recursive/force options - fsExtra.ensureDir() -> fs.promises.mkdir() with recursive option - fsExtra.exists() -> custom function using fs.promises.access() Move dependency injection interfaces from centralized deps.ts to local interfaces in each module (FileDeps, ScannerCliFsDeps, etc.), improving code locality and maintainability. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Update test files to import interfaces from their respective source modules instead of the deleted deps.ts file. Remove export from interfaces that are only used internally. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add `type` modifier to imports that are only used for type checking. This improves tree-shaking and makes it clear which imports are only used at compile time. Also update CLAUDE.md with this convention. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move default values for dependency injection from inside function bodies to parameter destructuring. This makes it clearer which defaults are used when callers don't provide specific dependencies. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add path traversal validation when extracting tar.gz archives to prevent malicious archives from writing files outside the target directory. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Remove redundant ignoreDependencies and entry pattern as suggested by knip. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Restored 5 tests that were accidentally omitted during the migration from Jest to Node's native test runner: - should download SonarScanner CLI if it does not exist on Unix without arch - should display SonarScanner CLI output - should only forward non-scanner env vars to Scanner CLI - should pass proxy options to scanner - should pass https proxy options to scanner Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Added path traversal validation for AdmZip extraction to complement the existing tar.gz protection. All archive entries are now validated to ensure they extract within the target directory. Added test for tar.gz path traversal rejection. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Update zip extraction path validation to use path.normalize with string concatenation as recommended by SonarCloud S6096 rule. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Apply same fix pattern to tar.gz extraction for consistency. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add node-reporter-sonarqube to generate test execution reports - Update test script to output reports in SonarQube generic format - Configure ci-analysis.js to send test execution data via sonar.testExecutionReportPaths Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The package is used in npm script, not imported in code. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Remove deprecated shebang and husky.sh sourcing lines. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The test execution report needs to be shared with the analyze job. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- ChildProcessMock.ts: Not used after Jest migration - mock-jre.tar.gz: Only referenced as string in test data, not read from disk Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move FakeProjectMock class directly into properties.test.ts since it's the only file using it. Removes the now-empty mocks folder. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Create src/deps.ts with centralized Dependencies interface and getDeps()/setDeps()/resetDeps() functions for dependency injection - Migrate all source files to use getDeps() instead of function parameters - Create test/unit/test-helpers.ts with reusable mock factories - Update all unit tests to use setDeps() in beforeEach and resetDeps() in afterEach instead of passing deps as function arguments - Remove sinon dependency, replacing all stubs with native node:test mocks - Fix TypeScript errors by using proper Mock<T> typing from node:test Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
cefdf83 to
f77eef8
Compare
SonarQube reviewer guideImportant We are currently testing different models for AI Summary. Model A:Summary: Migrate test suite from Jest to Node.js native test runner and improve dependency injection architecture. Review Focus:
Start review at: Model B:Summary: Migrates the test framework from Jest to Node.js native test runner with comprehensive refactoring. Review Focus:
Start review at:
|



Summary
src/deps.tsfor shared dependency interfacesnode:testandnode:assertnode:prefix for all Node.js built-in importsTest plan
npm testnpm run build🤖 Generated with Claude Code