Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 20 additions & 41 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand All @@ -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
Expand All @@ -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 }}

29 changes: 29 additions & 0 deletions app/core/calculator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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', () => {
Expand All @@ -22,8 +23,36 @@
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

Check failure on line 43 in app/core/calculator.test.js

View workflow job for this annotation

GitHub Actions / test

app/core/calculator.test.js > divide function

AssertionError: expected NaN to be a function ❯ app/core/calculator.test.js:43:26

Check failure on line 43 in app/core/calculator.test.js

View workflow job for this annotation

GitHub Actions / test

app/core/calculator.test.js > divide function

AssertionError: expected NaN to be a function ❯ app/core/calculator.test.js:43:26
});

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
});
12 changes: 11 additions & 1 deletion app/utils/mathUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
14 changes: 14 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -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
17 changes: 7 additions & 10 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -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
},
}
})

});