Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(29788): fix code coverage for vitest and cypress (e2e and compo…
Browse files Browse the repository at this point in the history
…nent)
vanch3d committed Jan 27, 2025
1 parent 2dadb32 commit 1141fa7
Showing 10 changed files with 2,111 additions and 146 deletions.
11 changes: 10 additions & 1 deletion hivemq-edge/src/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
node_modules
/docs
/dist
/coverage

# local env files
.env.local
.env.*.local
@@ -27,6 +27,15 @@ cypress/screenshots
# Exclude test-results
test-results

# Exclude code coverage
/.nyc_output/
/coverage
/coverage-components/
/coverage-e2e/
/coverage-vitest/
/coverage-cypress/


# Local dev fixes
/public/app/
/public/images/
7 changes: 6 additions & 1 deletion hivemq-edge/src/frontend/.prettierignore
Original file line number Diff line number Diff line change
@@ -3,7 +3,12 @@ pnpm-lock.*
.idea/
docs/
dist/
coverage/
/.nyc_output/
/coverage
/coverage-components/
/coverage-e2e/
/coverage-vitest/
/coverage-cypress/
public/
src/api/__generated__
.gradle/
15 changes: 13 additions & 2 deletions hivemq-edge/src/frontend/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
import { defineConfig } from 'cypress'
import installLogsPrinter from 'cypress-terminal-report/src/installLogsPrinter'

import codeCoverage from '@cypress/code-coverage/task'

export default defineConfig({
env: {
codeCoverage: {
exclude: ['cypress/**/*.*', '**/__generated__/*'],
},
},
retries: { runMode: 2, openMode: 0 },
e2e: {
video: true,
baseUrl: 'http://localhost:3000',
setupNodeEvents(on) {
setupNodeEvents(on, config) {
codeCoverage(on, config)
installLogsPrinter(on, {
printLogsToConsole: 'onFail',
includeSuccessfulHookLogs: false,
})
return config
},
},

component: {
video: true,

setupNodeEvents(on) {
setupNodeEvents(on, config) {
codeCoverage(on, config)
installLogsPrinter(on, {
printLogsToConsole: 'onFail',
includeSuccessfulHookLogs: false,
})
return config
},

devServer: {
8 changes: 6 additions & 2 deletions hivemq-edge/src/frontend/cypress/support/component.tsx
Original file line number Diff line number Diff line change
@@ -20,10 +20,14 @@ import 'cypress-axe'
import 'cypress-each'
import '@percy/cypress'
import 'cypress-real-events'
import '@cypress/code-coverage/support'

import './commands'

import { mount, MountOptions, MountReturn } from 'cypress/react18'
import { MemoryRouterProps, MemoryRouter } from 'react-router-dom'
import type { MountOptions, MountReturn } from 'cypress/react18'
import { mount } from 'cypress/react18'
import type { MemoryRouterProps } from 'react-router-dom'
import { MemoryRouter } from 'react-router-dom'
import { ChakraProvider, VisuallyHidden } from '@chakra-ui/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

1 change: 1 addition & 0 deletions hivemq-edge/src/frontend/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ import 'cypress-axe'
import 'cypress-each'
import '@percy/cypress'
import 'cypress-real-events'
import '@cypress/code-coverage/support'

import './commands'

6 changes: 6 additions & 0 deletions hivemq-edge/src/frontend/package.json
Original file line number Diff line number Diff line change
@@ -91,6 +91,7 @@
"monaco-editor": "^0.47.0",
"mqtt": "^5.10.1",
"mqtt-match": "^3.0.0",
"nyc": "^17.1.0",
"protobufjs": "^7.2.6",
"react": "^18.2.0",
"react-accessible-treeview": "^2.9.1",
@@ -111,6 +112,7 @@
},
"devDependencies": {
"@chakra-ui/cli": "^2.4.1",
"@cypress/code-coverage": "^3.13.10",
"@percy/cli": "^1.28.5",
"@percy/cypress": "^3.1.2",
"@tanstack/eslint-plugin-query": "^5.32.1",
@@ -150,11 +152,15 @@
"stylelint-config-standard-scss": "^9.0.0",
"typescript": "^5.0.4",
"vite": "^5.2.13",
"vite-plugin-istanbul": "^6.0.2",
"vitest": "^1.5.3"
},
"msw": {
"workerDirectory": "public"
},
"nyc": {
"report-dir": "./coverage-cypress"
},
"engines": {
"node": "18",
"pnpm": "8"
2,157 changes: 2,017 additions & 140 deletions hivemq-edge/src/frontend/pnpm-lock.yaml

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions hivemq-edge/src/frontend/tools/merge-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* istanbul ignore file -- @preserve */
/**
* This script merges the coverage reports from Cypress and Jest into a single one,
* inside the "coverage" folder
*/

// eslint-disable-next-line @typescript-eslint/no-var-requires,no-undef
const { execSync } = require('child_process')
// eslint-disable-next-line @typescript-eslint/no-var-requires,no-undef
const fs = require('fs-extra')

const REPORTS_FOLDER = 'reports'
const FINAL_OUTPUT_FOLDER = 'combined-coverage'

// eslint-disable-next-line @typescript-eslint/no-var-requires,no-undef
const { program } = require('commander')

program
.option('-e --e2e-cov-dir <dir>', 'Directory for e2e coverage', 'e2e/coverage')
.option('-c --ct-cov-dir <dir>', 'Directory for cypress-ct coverage', 'coverage')
.option('-u --unit-cov-dir <dir>', 'Directory for unit test coverage', 'dist/coverage')

program.parse()
const options = program.opts()

console.log('Running merge with options:', options)

const run = (commands) => {
commands.forEach((command) => execSync(command, { stdio: 'inherit' }))
}

// Create the reports folder and move the reports from cypress and jest inside it
fs.emptyDirSync(REPORTS_FOLDER)
fs.copyFileSync(options.ctCovDir + '/coverage-final.json', `${REPORTS_FOLDER}/from-cypress-ct.json`)
fs.copyFileSync(options.unitCovDir + '/coverage-final.json', `${REPORTS_FOLDER}/from-jest.json`)

fs.emptyDirSync('.nyc_output')
fs.emptyDirSync(FINAL_OUTPUT_FOLDER)

// Run "nyc merge" inside the reports folder, merging the two coverage files into one,
// then generate the final report on the coverage folder
run([
// "nyc merge" will create a "coverage.json" file on the root, we move it to .nyc_output
`npx nyc merge ${REPORTS_FOLDER} && mv coverage.json .nyc_output/out.json`,
`npx nyc report --reporter lcov --report-dir ${FINAL_OUTPUT_FOLDER}`,
])
5 changes: 5 additions & 0 deletions hivemq-edge/src/frontend/vite.config.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import { sentryVitePlugin } from '@sentry/vite-plugin'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import * as path from 'path'
import istanbul from 'vite-plugin-istanbul'

// https://vitejs.dev/config/
export default defineConfig({
@@ -18,6 +19,10 @@ export default defineConfig({
org: 'hivemq',
project: 'edge',
}),
istanbul({
requireEnv: false,
cypress: true,
}),
],

server: {
1 change: 1 addition & 0 deletions hivemq-edge/src/frontend/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ export default defineConfig({
],
provider: 'istanbul', // or 'v8'
reporter: ['text', 'json', 'html', 'lcov'],
reportsDirectory: './coverage-vitest/',
},
},
})

0 comments on commit 1141fa7

Please sign in to comment.