Skip to content

Commit 5a50577

Browse files
miraoclaude
andcommitted
test: add unit tests for parsePlaywrightBrowsers regex
Extract parsePlaywrightBrowsers function and add unit tests to verify both old (Playwright < 1.58) and new (1.58+) output formats are parsed correctly, and that chromium-headless-shell is excluded. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ad6d584 commit 5a50577

File tree

2 files changed

+113
-15
lines changed

2 files changed

+113
-15
lines changed

lib/command/info.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ import Codecept from '../codecept.js'
55
import output from '../output.js'
66
import { execSync } from 'child_process'
77

8+
// Unified regex for both formats (excludes chromium-headless-shell):
9+
// - 1.58+: "Firefox 146.0.1 (playwright firefox v1509)"
10+
// - 1.57: "browser: firefox version 144.0.2"
11+
const playwrightBrowserRegex = /(?:([\d.]+)\s+\(playwright\s+(chromium|firefox|webkit)\s)|(?:browser:\s*(chromium|firefox|webkit)\s+version\s+([\d.]+))/gi
12+
13+
function parsePlaywrightBrowsers(output) {
14+
const versions = []
15+
const matches = [...output.matchAll(playwrightBrowserRegex)]
16+
matches.forEach(match => {
17+
const browser = match[2] || match[3]
18+
const version = match[1] || match[4]
19+
versions.push(`${browser}: ${version}`)
20+
})
21+
return versions.join(', ')
22+
}
23+
824
async function getPlaywrightBrowsers() {
925
try {
10-
// Unified regex for both formats (excludes chromium-headless-shell):
11-
// - 1.58+: "Firefox 146.0.1 (playwright firefox v1509)"
12-
// - 1.57: "browser: firefox version 144.0.2"
13-
const regex = /(?:([\d.]+)\s+\(playwright\s+(chromium|firefox|webkit)\s)|(?:browser:\s*(chromium|firefox|webkit)\s+version\s+([\d.]+))/gi
14-
let versions = []
15-
1626
const info = execSync('npx playwright install --dry-run').toString().trim()
17-
18-
const matches = [...info.matchAll(regex)]
19-
matches.forEach(match => {
20-
const browser = match[2] || match[3]
21-
const version = match[1] || match[4]
22-
versions.push(`${browser}: ${version}`)
23-
})
24-
25-
return versions.join(', ')
27+
return parsePlaywrightBrowsers(info)
2628
} catch (err) {
2729
return 'Playwright not installed'
2830
}
@@ -75,6 +77,8 @@ export default async function (path) {
7577
output.print('***************************************')
7678
}
7779

80+
export { parsePlaywrightBrowsers }
81+
7882
export const getMachineInfo = async () => {
7983
const info = {
8084
nodeInfo: await envinfo.helpers.getNodeInfo(),

test/unit/command/info_test.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { expect } from 'chai'
2+
import { parsePlaywrightBrowsers } from '../../../lib/command/info.js'
3+
4+
describe('info command', () => {
5+
describe('parsePlaywrightBrowsers', () => {
6+
describe('old format (Playwright < 1.58)', () => {
7+
const oldFormatOutput = `browser: chromium version 140.0.7339.186
8+
browser: chromium-headless-shell version 140.0.7339.186
9+
browser: firefox version 141.0
10+
browser: webkit version 26.0`
11+
12+
it('should parse chromium version', () => {
13+
const result = parsePlaywrightBrowsers(oldFormatOutput)
14+
expect(result).to.include('chromium: 140.0.7339.186')
15+
})
16+
17+
it('should parse firefox version', () => {
18+
const result = parsePlaywrightBrowsers(oldFormatOutput)
19+
expect(result).to.include('firefox: 141.0')
20+
})
21+
22+
it('should parse webkit version', () => {
23+
const result = parsePlaywrightBrowsers(oldFormatOutput)
24+
expect(result).to.include('webkit: 26.0')
25+
})
26+
27+
it('should exclude chromium-headless-shell', () => {
28+
const result = parsePlaywrightBrowsers(oldFormatOutput)
29+
expect(result).to.not.include('chromium-headless-shell')
30+
})
31+
32+
it('should return all three browsers', () => {
33+
const result = parsePlaywrightBrowsers(oldFormatOutput)
34+
expect(result).to.equal('chromium: 140.0.7339.186, firefox: 141.0, webkit: 26.0')
35+
})
36+
})
37+
38+
describe('new format (Playwright 1.58+)', () => {
39+
const newFormatOutput = `Chrome for Testing 145.0.7632.6 (playwright chromium v1208)
40+
Chromium Headless Shell 145.0.7632.6 (playwright build v1208)
41+
Firefox 146.0.1 (playwright firefox v1509)
42+
Webkit 18.4 (playwright webkit v2140)`
43+
44+
it('should parse chromium version', () => {
45+
const result = parsePlaywrightBrowsers(newFormatOutput)
46+
expect(result).to.include('chromium: 145.0.7632.6')
47+
})
48+
49+
it('should parse firefox version', () => {
50+
const result = parsePlaywrightBrowsers(newFormatOutput)
51+
expect(result).to.include('firefox: 146.0.1')
52+
})
53+
54+
it('should parse webkit version', () => {
55+
const result = parsePlaywrightBrowsers(newFormatOutput)
56+
expect(result).to.include('webkit: 18.4')
57+
})
58+
59+
it('should exclude Chromium Headless Shell', () => {
60+
const result = parsePlaywrightBrowsers(newFormatOutput)
61+
expect(result).to.not.include('Headless')
62+
})
63+
64+
it('should return all three browsers', () => {
65+
const result = parsePlaywrightBrowsers(newFormatOutput)
66+
expect(result).to.equal('chromium: 145.0.7632.6, firefox: 146.0.1, webkit: 18.4')
67+
})
68+
})
69+
70+
describe('mixed/edge cases', () => {
71+
it('should handle empty input', () => {
72+
const result = parsePlaywrightBrowsers('')
73+
expect(result).to.equal('')
74+
})
75+
76+
it('should handle input with no matching browsers', () => {
77+
const result = parsePlaywrightBrowsers('some random text without browser info')
78+
expect(result).to.equal('')
79+
})
80+
81+
it('should handle case insensitivity for old format', () => {
82+
const input = 'browser: CHROMIUM version 100.0.0'
83+
const result = parsePlaywrightBrowsers(input)
84+
expect(result).to.equal('CHROMIUM: 100.0.0')
85+
})
86+
87+
it('should handle case insensitivity for new format', () => {
88+
const input = 'Chrome 100.0.0 (playwright CHROMIUM v1234)'
89+
const result = parsePlaywrightBrowsers(input)
90+
expect(result).to.equal('CHROMIUM: 100.0.0')
91+
})
92+
})
93+
})
94+
})

0 commit comments

Comments
 (0)