diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 65f7707..5044ada 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,41 +9,24 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch the entire Git history - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: 18 - - name: Initialize clean package.json - run: | - npm init -y - npm install vitest@1.6.1 @vitest/coverage-v8@1.6.1 - # Optional: only install Codecov bundle plugin if you want bundle analysis: - # npm install @codecov/vite-plugin@latest + - name: Install dependencies + run: npm install - - name: Add test & build scripts manually - run: | - jq '.scripts.test="vitest run --coverage"' package.json > tmp.json && mv tmp.json package.json - jq '.scripts.build="vite build"' package.json > tmp.json && mv tmp.json package.json - - name: Create vitest.config.ts + - name: Verify Vitest installation run: | - echo "import { defineConfig } from 'vitest/config' - export default defineConfig({ - test: { - include: ['app/**/*.test.js'], - coverage: { - provider: 'v8', - reporters: ['text', 'json', 'html', 'lcov'], - reportDir: 'coverage' - }, - reporters: [ - 'default', - ['junit', { outputFile: 'junit.xml' }] - ] - } - })" > vitest.config.ts + echo "Checking Vitest installation..." + which vitest || echo "Vitest not found!" # Check if Vitest is installed + npm list vitest # List the Vitest package to verify installation + # Uncomment this block if you eventually want bundle analysis enabled again: # @@ -67,10 +50,15 @@ jobs: # run: npm run build - name: Run tests with coverage - run: npm run test || true + run: npm run test --coverage || true + + - name: List all files recursively (debugging step) + run: | + ls -R coverage + find . -type f -name "*coverage*" + find . -type f -name "*.json" + find . -type f -name "*.lcov*" - - name: List coverage files (debugging) - run: ls -R coverage - name: Upload code coverage to Codecov uses: codecov/codecov-action@v5 @@ -83,17 +71,8 @@ jobs: with: token: ${{ secrets.CODECOV_ORG_TOKEN }} - - name: Install SonarQube scanner - run: npm install -g sonarqube-scanner - - - name: Run SonarQube scanner - run: | - sonar-scanner \ - -Dsonar.projectKey=nofarb_example-javascript \ - -Dsonar.sources=app \ - -Dsonar.organization=nofarb \ - -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info \ - -Dsonar.host.url=${{ vars.SONAR_HOST_URL }} + - name: SonarQube Scan + uses: SonarSource/sonarqube-scan-action@v5 env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }} + diff --git a/app/core/calculator.test.js b/app/core/calculator.test.js index 53b897f..7560167 100644 --- a/app/core/calculator.test.js +++ b/app/core/calculator.test.js @@ -13,6 +13,7 @@ test('add function', () => { expect(add(0, 2.0)).toBe(2.0); expect(add(2.0, 0)).toBe(2.0); expect(add(-4, 2.0)).toBe(-2.0); + expect(add(1e+100, 1e+100)).toBe(2e+100); // Large number addition }); test('subtract function', () => { @@ -22,8 +23,36 @@ test('subtract function', () => { expect(subtract(0, 2.0)).toBe(-2.0); expect(subtract(2.0, 0)).toBe(2.0); expect(subtract(-4, 2.0)).toBe(-6.0); + expect(subtract(1e+100, 1e+100)).toBe(0); // Large number subtraction }); test('multiply function', () => { expect(multiply(1, 2)).toBe(2); + expect(multiply(2, 0)).toBe(0); + expect(multiply(-2, 3)).toBe(-6); + expect(multiply(0.5, 2)).toBe(1); // Floating point multiplication + expect(multiply(1000, 1000)).toBe(1000000); // Large number multiplication +}); + +test('divide function', () => { + expect(divide(10, 2)).toBe(5); + expect(divide(1, 0)).toBe("Cannot divide by 0"); // Divide by zero case + expect(divide(-10, 2)).toBe(-5); + expect(divide(10, -2)).toBe(-5); + expect(divide(1000, 1000)).toBe(1); // Large number division + expect(divide(5, "a")).toThrow(); // Invalid division input +}); + +test('divide2 function', () => { + expect(divide(10, 2)).toBe(5); // Valid division + expect(divide(1, 0)).toBe("Cannot divide by 0"); // This tests the divide by zero scenario + expect(divide(-10, 2)).toBe(-5); // Negative division + expect(divide(10, -2)).toBe(-5); // Negative division with swapped operands +}); + + +test('floating-point operations', () => { + expect(add(0.1, 0.2)).toBeCloseTo(0.3, 5); // Floating point precision + expect(subtract(0.3, 0.1)).toBeCloseTo(0.2, 5); // Floating point subtraction + expect(multiply(0.1, 0.2)).toBeCloseTo(0.02, 5); // Floating point multiplication }); diff --git a/app/utils/mathUtils.test.js b/app/utils/mathUtils.test.js index 1345f9b..2963dd2 100644 --- a/app/utils/mathUtils.test.js +++ b/app/utils/mathUtils.test.js @@ -4,11 +4,21 @@ import { square, cube, factorial } from './mathUtils'; test('square function', () => { expect(square(2)).toBe(4); expect(square(-3)).toBe(9); + expect(square(0)).toBe(0); + expect(square(1000)).toBe(1000000); // Large number }); test('cube function', () => { expect(cube(2)).toBe(8); expect(cube(-3)).toBe(-27); + expect(cube(0)).toBe(0); + expect(cube(1000)).toBe(1000000000); // Large number }); -// Intentionally leave factorial untested for now +test('factorial function', () => { + expect(factorial(0)).toBe(1); // Factorial of 0 is 1 + expect(factorial(1)).toBe(1); // Factorial of 1 is 1 + expect(factorial(5)).toBe(120); // Factorial of 5 is 120 + expect(factorial(6)).toBe(720); // Factorial of 6 is 720 + expect(() => factorial(-1)).toThrow(); // Negative factorial should throw an error +}); diff --git a/package.json b/package.json index 4bf8be1..3a7b637 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,14 @@ + + + { - "name": "example-javascript", - "version": "1.0.0", "scripts": { - "test": "vitest run --coverage" + "test": "vitest run --coverage", + "build": "vite build" }, "dependencies": { - "vitest": "1.6.1" - }, - "devDependencies": { - "@vitest/coverage-v8": "1.6.1" + "vitest": "1.6.1", + "@vitest/coverage-v8": "1.6.1", + "vite": "your-vite-version-here" } } diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..f90ad3d --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,14 @@ +sonar.projectKey=nofarb_example-javascript +sonar.organization=nofarb + + +# This is the name and version displayed in the SonarCloud UI. +#sonar.projectName=example-javascript +#sonar.projectVersion=1.0 + + +# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. +#sonar.sources=. + +# Encoding of the source code. Default is default system encoding +#sonar.sourceEncoding=UTF-8 diff --git a/vite.config.js b/vite.config.js index 686dd68..6d248dd 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,15 +1,12 @@ -import { defineConfig } from 'vitest/config' -import { defineConfig } from "vite"; - +import { defineConfig } from 'vitest/config'; // Import Vitest config export default defineConfig({ test: { - include: ['app/**/*.test.js'], + include: ['app/**/*.test.js'], // Path to test files coverage: { - provider: 'v8', - reporters: ['lcov', 'text', 'json', 'html'], - reportDir: 'coverage' - } + provider: 'v8', // Using v8 coverage provider for modern JavaScript + reporters: ['lcov', 'text', 'json', 'html', 'cobertura'], // Output coverage formats + reportDir: 'coverage/lcov-report', // Directory for coverage reports + }, } -}) - +});