From a6996b01d590ec916a2aba1700676f2424d7e909 Mon Sep 17 00:00:00 2001 From: Daniele Debernardi Date: Mon, 17 Mar 2025 17:48:52 +0100 Subject: [PATCH 1/2] Add code coverage with 100% threshold --- .github/workflows/ci.yml | 14 ++++++++++++++ CHANGELOG.md | 4 ++++ jest.config.js | 10 ++++++++++ src/lib/localStorage.spec.ts | 9 +++++++++ src/lib/object.spec.ts | 5 +++++ src/lib/string.ts | 2 +- 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 01014f3..10ebd52 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -88,3 +88,17 @@ jobs: - run: yarn lint - run: yarn build - run: yarn jest + + - name: Code Coverage Summary Report + uses: irongut/CodeCoverageSummary@v1.3.0 + with: + filename: coverage/coverage.cobertura.xml + format: markdown + output: both + + - name: Add Coverage PR Comment + uses: marocchino/sticky-pull-request-comment@v2 + if: github.event_name == 'pull_request' + with: + recreate: true + path: code-coverage-results.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 68a01a9..016722c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### dependabot: \#49 Bump cross-spawn from 7.0.3 to 7.0.6 +### Added + +- Code coverage with 100% threshold + ## [1.2.0] - 2025-01-13 ### Added diff --git a/jest.config.js b/jest.config.js index 5e031d0..15926d9 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,4 +4,14 @@ module.exports = { testEnvironment: "node", resetMocks: false, setupFiles: ["jest-localstorage-mock"], + collectCoverage: true, + coverageReporters: ["text", "html", "cobertura"], + coverageThreshold: { + global: { + branches: 100, + functions: 100, + lines: 100, + statements: 100, + }, + }, }; diff --git a/src/lib/localStorage.spec.ts b/src/lib/localStorage.spec.ts index 4df258a..575f65c 100644 --- a/src/lib/localStorage.spec.ts +++ b/src/lib/localStorage.spec.ts @@ -6,6 +6,15 @@ describe("localStorage tests", () => { jest.clearAllMocks(); }); + test("localStorage not supported", () => { + const { localStorage } = global; + delete (global as Partial).localStorage; + expect(() => { + getLocalStorageItem("test"); + }).toThrow("localStorage not supported"); + global.localStorage = localStorage; + }); + test("getLocalStorageItem not existing", () => { expect(getLocalStorageItem("test")).toBeUndefined(); }); diff --git a/src/lib/object.spec.ts b/src/lib/object.spec.ts index b6d4ae6..66699d4 100644 --- a/src/lib/object.spec.ts +++ b/src/lib/object.spec.ts @@ -26,6 +26,11 @@ describe("object tests", () => { }); test.each([ + // Invalid objects + [null as unknown as object, null], + [undefined as unknown as object, undefined], + + // Valid objects [{}, {}], [{ hello: "world" }, { hello: "world" }], [{ hello: null }, {}], diff --git a/src/lib/string.ts b/src/lib/string.ts index c5a1f96..c47392d 100644 --- a/src/lib/string.ts +++ b/src/lib/string.ts @@ -17,7 +17,7 @@ export function isNullOrEmpty(value?: string): boolean { * @returns true if the value parameter is null/undefined, Empty, or if value consists exclusively of white-space characters */ export function isNullOrWhitespace(value?: string): boolean { - return isNullOrEmpty(value) || (value ?? "").trim().length === 0; + return isNullOrEmpty(value) || (value as string).trim().length === 0; } /** From e35a14571186e2777e6a63755c1a4e3e0d943d99 Mon Sep 17 00:00:00 2001 From: Daniele Debernardi Date: Mon, 17 Mar 2025 20:06:40 +0100 Subject: [PATCH 2/2] Update ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 10ebd52..ec19e45 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -92,7 +92,7 @@ jobs: - name: Code Coverage Summary Report uses: irongut/CodeCoverageSummary@v1.3.0 with: - filename: coverage/coverage.cobertura.xml + filename: coverage/cobertura-coverage.xml format: markdown output: both