Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add Vitest #465

Merged
merged 4 commits into from
Nov 25, 2024
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
35 changes: 35 additions & 0 deletions .github/workflows/test-frontend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Test frontend
on:
push:
branches:
- main
- stable
pull_request:
workflow_dispatch:

jobs:
run-vitest:
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v4

- uses: pnpm/action-setup@v4
with:
version: 9
run_install: false

- uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
cache-dependency-path: pnpm-lock.yaml

- name: Install and Build
run: |
(cd frontend && pnpm install);
make .install-logos
API_URL="https://prod.packit.dev/api" make transpile-prod

- name: Run Vitest
run: make test-frontend-coverage
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ check:
test_image: files/ansible/install-deps.yaml files/ansible/recipe-tests.yaml
$(CONTAINER_ENGINE) build --rm -t $(TEST_IMAGE) -f Dockerfile.tests .

check_in_container: test_image
test-frontend:
cd frontend && pnpm run test

test-frontend-coverage:
cd frontend && pnpm run coverage

check-in-container: test_image
$(CONTAINER_ENGINE) run --rm \
--security-opt label=disable \
$(TEST_IMAGE) make check
10 changes: 8 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
"@tanstack/react-router": "^1.58.15",
"@tanstack/router-devtools": "^1.58.15",
"@tanstack/router-plugin": "^1.58.12",
"@testing-library/react": "^16.0.1",
"@types/node": "^22.7.4",
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.2",
"happy-dom": "^15.7.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sharp": "^0.33.5",
Expand All @@ -40,7 +42,9 @@
"start": "vite",
"build": "vite build",
"build:analyze": "vite-bundle-visualizer --open",
"preview": "vite preview"
"preview": "vite preview",
"test": "vitest",
"coverage": "vitest run --coverage"
},
"browserslist": {
"production": [
Expand All @@ -57,7 +61,9 @@
"devDependencies": {
"@biomejs/biome": "1.8.3",
"babel-plugin-named-exports-order": "^0.0.2",
"prop-types": "^15.8.1"
"prop-types": "^15.8.1",
"vitest": "^2.1.5",
"@vitest/coverage-v8": "^2.1.5"
},
"engines": {
"node": ">=20",
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/components/forgeUrls.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Contributors to the Packit project.
// SPDX-License-Identifier: MIT
import { test, vi } from "vitest";
import { getHostName } from "./forgeUrls";

test("hostname logs error for invalid URL", ({ expect }) => {
const consoleMock = vi
.spyOn(console, "error")
.mockImplementation(() => undefined);
expect(getHostName("invalid url")).toBe("");

expect(consoleMock.mock.lastCall?.[0].toString()).toContain("Invalid URL");
expect(consoleMock.mock.lastCall?.[0]).toBeInstanceOf(TypeError);
});
22 changes: 6 additions & 16 deletions frontend/src/components/forgeUrls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
// SPDX-License-Identifier: MIT

// getHostName - returns the hostname if possible, otherwise an empty string
function getHostName(url: string | URL) {
export function getHostName(url: string | URL) {
let hostname = "";
try {
hostname = new URL(url).hostname;
} catch (error) {
console.log("Invalid URL");
console.error(error);
}
return hostname;
}

// getPRLink - returns the PR link if possible otherwise an empty string
function getPRLink(gitRepo: string, prID: number) {
export function getPRLink(gitRepo: string, prID: number) {
const forge = getHostName(gitRepo);
switch (forge) {
case "github.com":
Expand All @@ -28,7 +27,7 @@ function getPRLink(gitRepo: string, prID: number) {
}

// getBranchLink - returns the branch link if possible otherwise an empty string
function getBranchLink(gitRepo: string, branchName: string) {
export function getBranchLink(gitRepo: string, branchName: string) {
const forge = getHostName(gitRepo);
switch (forge) {
case "github.com":
Expand All @@ -42,7 +41,7 @@ function getBranchLink(gitRepo: string, branchName: string) {
}

// getIssueLink - returns the issue link if possible otherwise an empty string
function getIssueLink(gitRepo: string, issueID: number) {
export function getIssueLink(gitRepo: string, issueID: number) {
const forge = getHostName(gitRepo);
switch (forge) {
case "github.com":
Expand All @@ -56,7 +55,7 @@ function getIssueLink(gitRepo: string, issueID: number) {
}

// getReleaseLink - returns the link to release if possible otherwise an empty string
function getReleaseLink(gitRepo: string, release: string) {
export function getReleaseLink(gitRepo: string, release: string) {
const forge = getHostName(gitRepo);
switch (forge) {
case "github.com":
Expand All @@ -69,7 +68,7 @@ function getReleaseLink(gitRepo: string, release: string) {
}

// getCommitLink - returns a link to the commit
function getCommitLink(gitRepo: string, commit_hash: string) {
export function getCommitLink(gitRepo: string, commit_hash: string) {
const forge = getHostName(gitRepo);
switch (forge) {
case "github.com":
Expand All @@ -81,12 +80,3 @@ function getCommitLink(gitRepo: string, commit_hash: string) {
return `${gitRepo}/-/commit/${commit_hash}`;
}
}

export {
getHostName,
getPRLink,
getBranchLink,
getIssueLink,
getReleaseLink,
getCommitLink,
};
19 changes: 19 additions & 0 deletions frontend/src/components/statusLabels/BaseStatusLabel.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Contributors to the Packit project.
// SPDX-License-Identifier: MIT

import { IceCreamIcon } from "@patternfly/react-icons";
import { render } from "@testing-library/react";
import { test } from "vitest";
import { BaseStatusLabel } from "./BaseStatusLabel";

test("default label", async ({ expect }) => {
const { baseElement } = render(
<BaseStatusLabel
color="blue"
tooltipText="tooltipText"
icon={<IceCreamIcon />}
label="Label fsdf"
/>,
);
expect(baseElement).toMatchSnapshot("default label");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`default label > default label 1`] = `
<body>
<div>
<div
style="display: contents;"
>
<span
style="padding: 2px;"
>
<span
class="pf-v6-c-label pf-m-blue pf-m-filled"
>
<span
class="pf-v6-c-label__content"
>
<span
class="pf-v6-c-label__icon"
>
<svg
aria-hidden="true"
class="pf-v6-svg"
fill="currentColor"
height="1em"
role="img"
viewBox="0 0 448 512"
width="1em"
>
<path
d="M368 160h-.94a144 144 0 1 0-286.12 0H80a48 48 0 0 0 0 96h288a48 48 0 0 0 0-96zM195.38 493.69a31.52 31.52 0 0 0 57.24 0L352 288H96z"
/>
</svg>
</span>
<span
class="pf-v6-c-label__text"
>
Label fsdf
<span
class="pf-v6-u-screen-reader"
>
tooltipText
</span>
</span>
</span>
</span>
</span>
</div>
</div>
</body>
`;
16 changes: 10 additions & 6 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Contributors to the Packit project.
// SPDX-License-Identifier: MIT

/// <reference types="vitest" />
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { sentryVitePlugin } from "@sentry/vite-plugin";
Expand All @@ -14,11 +14,7 @@ export default defineConfig(() => ({
rollupOptions: {
output: {
manualChunks: function manualChunks(id) {
if (
id.includes("victory-") ||
id.includes("d3") ||
id.includes("@patternfly/react-charts")
) {
if (id.includes("victory-") || id.includes("d3") || id.includes("@patternfly/react-charts")) {
return "charts";
} else if (id.includes("@patternfly")) {
return "patternfly";
Expand Down Expand Up @@ -46,9 +42,17 @@ export default defineConfig(() => ({
authToken: process.env.SENTRY_AUTH_TOKEN,
telemetry: false,
reactComponentAnnotation: { enabled: true },
disable: process.env.SENTRY_AUTH_TOKEN === undefined,
}),
],
server: {
open: true,
},
test: {
environment: "happy-dom",
reporters: process.env.GITHUB_ACTIONS ? ['dot', 'github-actions'] : ['dot'],
},
coverage: {
provider: "v8",
}
}));
Loading
Loading