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

refactor: use an xml parser for less brittle mocha result verification #30865

Merged
merged 4 commits into from
Feb 6, 2025
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"eslint-plugin-react-hooks": "4.2.0",
"eslint-plugin-vue": "7.18.0",
"execa": "4.0.0",
"fast-xml-parser": "^4.5.1",
"filesize": "10.1.1",
"fs-extra": "9.1.0",
"getenv": "^1.0.0",
Expand Down
24 changes: 7 additions & 17 deletions scripts/verify-mocha-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,25 @@ const fs = require('fs').promises
const la = require('lazy-ass')
const path = require('path')
const { readCircleEnv } = require('./circle-env')

// mocha regex
const MOCHA_REGEX = /<testsuites name="([^"]+)" time="([^"]+)" tests="([^"]+)" failures="([^"]+)"(?: skipped="([^"]+)"|)>/
// vitest regex
const VITEST_REGEX = /<testsuites name="([^"]+)" tests="([^"]+)" failures="([^"]+)" errors="([^"]+)" time="([^"]+)"(?: skipped="([^"]+)"|)>/
const { XMLParser } = require('fast-xml-parser')

const REPORTS_PATH = '/tmp/cypress/junit'

const expectedResultCount = Number(process.argv[process.argv.length - 1])

const parseMochaResult = (xml) => {
const [name, time, tests, failures, skipped] = MOCHA_REGEX.exec(xml).slice(1)
const parseResult = (xml) => {
const { testsuites } = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '',
}).parse(xml)

return {
name, time, tests: Number(tests), failures: Number(failures), skipped: Number(skipped || 0),
}
}
const parseVitestResult = (xml) => {
const [name, tests, failures, , time, skipped] = VITEST_REGEX.exec(xml).slice(1)
const { name, time, tests, failures, skipped } = testsuites

return {
name, time, tests: Number(tests), failures: Number(failures), skipped: Number(skipped || 0),
}
}

const parseResult = (xml) => {
return MOCHA_REGEX.test(xml) ? parseMochaResult(xml) : parseVitestResult(xml)
}

const total = { tests: 0, failures: 0, skipped: 0 }

console.log(`Looking for reports in ${REPORTS_PATH}`)
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16645,10 +16645,10 @@ [email protected]:
dependencies:
strnum "^1.0.5"

fast-xml-parser@^4.4.1:
version "4.5.0"
resolved "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.5.0.tgz#2882b7d01a6825dfdf909638f2de0256351def37"
integrity sha512-/PlTQCI96+fZMAOLMZK4CWG1ItCbfZ/0jx7UIJFChPNrx7tcEgerUgWbeieCM9MfHInUDyK8DWYZ+YrywDJuTg==
fast-xml-parser@^4.4.1, fast-xml-parser@^4.5.1:
version "4.5.1"
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.5.1.tgz#a7e665ff79b7919100a5202f23984b6150f9b31e"
integrity sha512-y655CeyUQ+jj7KBbYMc4FG01V8ZQqjN+gDYGJ50RtfsUB8iG9AmwmwoAgeKLJdmueKKMrH1RJ7yXHTSoczdv5w==
dependencies:
strnum "^1.0.5"

Expand Down
Loading