Skip to content

Commit 23f77e7

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 95b78e0 commit 23f77e7

File tree

2 files changed

+117
-15
lines changed

2 files changed

+117
-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 @@ const Codecept = require('../codecept')
55
const output = require('../output')
66
const { execSync } = require('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 @@ module.exports = async function (path) {
7577
output.print('***************************************')
7678
}
7779

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

test/unit/command/info_test.js

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

0 commit comments

Comments
 (0)