diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0aa8ac1..43f63da 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,20 @@ jobs: run: npm test - name: Build run: npm run build + - name: Mutation targets + if: github.event_name == 'pull_request' + id: targets + env: + BASE: origin/${{ github.base_ref }} + run: echo "files=$(scripts/mutation-targets.sh "$BASE")" >> "$GITHUB_OUTPUT" + - name: Mutation test the changed logic + if: github.event_name == 'pull_request' && steps.targets.outputs.files != '' + timeout-minutes: 30 + env: + FILES: ${{ steps.targets.outputs.files }} + run: | + npm i --no-save @stryker-mutator/core@10.1.1 @stryker-mutator/vitest-runner@10.1.1 + npx stryker run --mutate "$FILES" commitlint: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index c357118..f7a4be7 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ node_modules/ .DS_Store # brainstorm artifacts (not project code) .superpowers/ + +.stryker-tmp/ +reports/ diff --git a/scripts/mutation-targets.sh b/scripts/mutation-targets.sh new file mode 100755 index 0000000..ee4d4c3 --- /dev/null +++ b/scripts/mutation-targets.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# Comma-separated logic files a PR must leave without surviving mutants: every changed +# file in src/lib plus the src/lib file behind every changed test. Pages, components, +# data and the client wiring stay out. +set -euo pipefail +base="$1" +# A deleted test counts too: removing it can leave its source with surviving mutants. +git diff --name-only --diff-filter=AMRD "$base"...HEAD -- src tests | + while read -r f; do + case "$f" in + tests/*.test.ts) f="src/lib/$(basename "$f" .test.ts).ts" ;; + esac + [ -f "$f" ] && echo "$f" + done | + grep -E '^src/lib/.*\.ts$' | + sort -u | paste -sd, - || true diff --git a/stryker.config.json b/stryker.config.json new file mode 100644 index 0000000..77c0ca3 --- /dev/null +++ b/stryker.config.json @@ -0,0 +1,16 @@ +{ + "$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json", + "testRunner": "vitest", + "plugins": ["@stryker-mutator/vitest-runner", "./stryker/noise-ignorer.mjs"], + "ignorers": ["noise"], + "reporters": ["clear-text"], + "clearTextReporter": { + "logTests": false + }, + "thresholds": { + "high": 100, + "low": 100, + "break": 100 + }, + "tsconfigFile": "" +} diff --git a/stryker/noise-ignorer.mjs b/stryker/noise-ignorer.mjs new file mode 100644 index 0000000..e5b7702 --- /dev/null +++ b/stryker/noise-ignorer.mjs @@ -0,0 +1,41 @@ +import { declareValuePlugin, PluginKind } from "@stryker-mutator/api/plugin"; + +const STYLE_ATTRIBUTES = new Set(["className", "style"]); + +function isErrorMessage(path) { + const parent = path.parentPath; + return Boolean( + parent?.isNewExpression() && + parent.node.callee.type === "Identifier" && + /Error$/.test(parent.node.callee.name) && + parent.node.arguments.includes(path.node), + ); +} + +function isAssertionLabel(path) { + const parent = path.parentPath; + if (!parent?.isCallExpression() || !parent.node.arguments.includes(path.node)) return false; + const callee = parent.node.callee; + const name = callee.type === "Identifier" ? callee.name : callee.property?.name; + return typeof name === "string" && /^(assert|require)[A-Z]/.test(name); +} + +function isStyleAttribute(path) { + const attr = path.findParent((p) => p.isJSXAttribute()); + return Boolean(attr && STYLE_ATTRIBUTES.has(attr.node.name.name)); +} + +export const strykerPlugins = [ + declareValuePlugin(PluginKind.Ignore, "noise", { + shouldIgnore(path) { + if ((path.isStringLiteral() || path.isTemplateLiteral()) && isErrorMessage(path)) + return "Error message text is not part of the contract"; + if (path.isStringLiteral() && isAssertionLabel(path)) + return "Label only used in the assertion error message"; + if (path.isJSXAttribute() && STYLE_ATTRIBUTES.has(path.node.name.name)) + return "Styling is checked visually, not by unit tests"; + if (isStyleAttribute(path)) return "Styling is checked visually, not by unit tests"; + return undefined; + }, + }), +]; diff --git a/tests/projects.test.ts b/tests/projects.test.ts index 8886746..965b8c8 100644 --- a/tests/projects.test.ts +++ b/tests/projects.test.ts @@ -12,11 +12,17 @@ const p = (name: string, category: string): Project => ({ }); describe("groupByCategory", () => { - it("orders groups by the declared order and preserves item order", () => { - const projects = [p("a", "web apps"), p("b", "flipper zero"), p("c", "web apps")]; + it("orders groups by the declared order and projects by name, ignoring case", () => { + const projects = [ + p("c", "web apps"), + p("x", "flipper zero"), + p("B", "web apps"), + p("a", "web apps"), + p("b", "web apps"), + ]; const groups = groupByCategory(projects, ["web apps", "flipper zero"]); expect(groups.map((g) => g.category)).toEqual(["web apps", "flipper zero"]); - expect(groups[0].projects.map((x) => x.name)).toEqual(["a", "c"]); + expect(groups[0].projects.map((x) => x.name)).toEqual(["a", "B", "b", "c"]); }); it("appends categories missing from the order list, sorted alphabetically", () => { diff --git a/tests/theme.test.ts b/tests/theme.test.ts index 5262777..3c7cfd2 100644 --- a/tests/theme.test.ts +++ b/tests/theme.test.ts @@ -23,7 +23,7 @@ describe("nextPref", () => { describe("isThemePref", () => { it("accepts valid values and rejects others", () => { - expect(isThemePref("auto")).toBe(true); + expect(["light", "dark", "auto"].every(isThemePref)).toBe(true); expect(isThemePref("blue")).toBe(false); expect(isThemePref(null)).toBe(false); });