Skip to content

Commit 3ba5c3c

Browse files
committed
Test fix
1 parent 4b4ec0e commit 3ba5c3c

6 files changed

Lines changed: 95 additions & 83 deletions

File tree

packages/backend/tests/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('backend index', () => {
2929
describe('API endpoints', () => {
3030
it('should handle test run and stop requests with validation', async () => {
3131
vi.mocked(utils.getDevtoolsApp).mockResolvedValue('/mock/app/path')
32-
const server = await start({ port: 0 })
32+
const { server } = await start({ port: 0 })
3333
const { testRunner } = await import('../src/runner.js')
3434
const runSpy = vi.spyOn(testRunner, 'run').mockResolvedValue()
3535
const stopSpy = vi.spyOn(testRunner, 'stop')
@@ -83,7 +83,7 @@ describe('backend index', () => {
8383

8484
it('should handle test run errors gracefully', async () => {
8585
vi.mocked(utils.getDevtoolsApp).mockResolvedValue('/mock/app/path')
86-
const server = await start({ port: 0 })
86+
const { server } = await start({ port: 0 })
8787
const { testRunner } = await import('../src/runner.js')
8888
vi.spyOn(testRunner, 'run').mockRejectedValue(
8989
new Error('Test execution failed')

packages/nightwatch-devtools/example/tests/login.test.js renamed to packages/nightwatch-devtools/example/tests/login.js

File renamed without changes.

packages/nightwatch-devtools/example/tests/sample.test.js renamed to packages/nightwatch-devtools/example/tests/sample.js

File renamed without changes.

packages/nightwatch-devtools/src/helpers/utils.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,54 @@ export function getRequestType(url: string, mimeType?: string): string {
330330
}
331331
return 'xhr'
332332
}
333+
334+
/**
335+
* Extract BDD step keywords (Given/When/Then/And/But) from a feature file
336+
* for the steps belonging to the named scenario. The order of keywords
337+
* in the file matches the order of pickle.steps, so we just walk line-by-line.
338+
*/
339+
export function parseStepKeywords(
340+
featureContent: string,
341+
scenarioName: string,
342+
stepCount: number
343+
): string[] {
344+
if (!featureContent || stepCount === 0) {
345+
return Array(stepCount).fill('')
346+
}
347+
348+
const lines = featureContent.split('\n')
349+
const stepRe = /^\s*(Given|When|Then|And|But)\s+/i
350+
351+
// Find the Scenario block that contains this scenario name
352+
const scenarioLineIdx = lines.findIndex(
353+
(l) => /^\s*Scenario:/i.test(l) && l.includes(scenarioName)
354+
)
355+
if (scenarioLineIdx === -1) {
356+
return Array(stepCount).fill('')
357+
}
358+
359+
const keywords: string[] = []
360+
for (
361+
let i = scenarioLineIdx + 1;
362+
i < lines.length && keywords.length < stepCount;
363+
i++
364+
) {
365+
// Stop at next Scenario or Feature header
366+
if (
367+
i > scenarioLineIdx &&
368+
(/^\s*Scenario:/i.test(lines[i]) || /^\s*Feature:/i.test(lines[i]))
369+
) {
370+
break
371+
}
372+
const m = stepRe.exec(lines[i])
373+
if (m) {
374+
keywords.push(m[1])
375+
}
376+
}
377+
378+
// Pad with empty strings if fewer keywords were found than steps
379+
while (keywords.length < stepCount) {
380+
keywords.push('')
381+
}
382+
return keywords
383+
}

packages/nightwatch-devtools/src/index.ts

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import {
2626
import {
2727
determineTestState,
2828
findTestFileFromStack,
29-
deterministicUid
29+
deterministicUid,
30+
extractTestMetadata,
31+
parseStepKeywords
3032
} from './helpers/utils.js'
3133
import { DEFAULTS, TIMING, TEST_STATE } from './constants.js'
3234

@@ -550,27 +552,12 @@ class NightwatchDevToolsPlugin {
550552
let suiteTitle = testFile
551553
let testNames: string[] = []
552554
if (fullPath) {
553-
// Inline implementation of extractTestMetadata
554-
let suite = null
555-
const names: string[] = []
556-
try {
557-
const source = fs.readFileSync(fullPath, 'utf-8')
558-
const suiteMatch = source.match(
559-
/(?:describe|suite|context)\s*\(\s*['\"]([^'\"]+)['\"]/
560-
)
561-
if (suiteMatch && suiteMatch[1]) {
562-
suite = suiteMatch[1]
563-
}
564-
const testRegex = /(?:it|test|specify)\s*\(\s*['\"]([^'\"]+)['\"]/g
565-
let match
566-
while ((match = testRegex.exec(source)) !== null) {
567-
names.push(match[1])
568-
}
569-
} catch {}
570-
if (suite) {
571-
suiteTitle = suite
555+
const { suiteTitle: parsedTitle, testNames: parsedNames } =
556+
extractTestMetadata(fullPath)
557+
if (parsedTitle) {
558+
suiteTitle = parsedTitle
572559
}
573-
testNames = names
560+
testNames = parsedNames
574561
}
575562

576563
// Get or create suite for this test file
@@ -930,57 +917,6 @@ class NightwatchDevToolsPlugin {
930917
}
931918
}
932919

933-
/**
934-
* Extract BDD step keywords (Given/When/Then/And/But) from a feature file
935-
* for the steps belonging to the named scenario. The order of keywords
936-
* in the file matches the order of pickle.steps, so we just walk line-by-line.
937-
*/
938-
function parseStepKeywords(
939-
featureContent: string,
940-
scenarioName: string,
941-
stepCount: number
942-
): string[] {
943-
if (!featureContent || stepCount === 0) {
944-
return Array(stepCount).fill('')
945-
}
946-
947-
const lines = featureContent.split('\n')
948-
const stepRe = /^\s*(Given|When|Then|And|But)\s+/i
949-
950-
// Find the Scenario block that contains this scenario name
951-
const scenarioLineIdx = lines.findIndex(
952-
(l) => /^\s*Scenario:/i.test(l) && l.includes(scenarioName)
953-
)
954-
if (scenarioLineIdx === -1) {
955-
return Array(stepCount).fill('')
956-
}
957-
958-
const keywords: string[] = []
959-
for (
960-
let i = scenarioLineIdx + 1;
961-
i < lines.length && keywords.length < stepCount;
962-
i++
963-
) {
964-
// Stop at next Scenario or Feature header
965-
if (
966-
i > scenarioLineIdx &&
967-
(/^\s*Scenario:/i.test(lines[i]) || /^\s*Feature:/i.test(lines[i]))
968-
) {
969-
break
970-
}
971-
const m = stepRe.exec(lines[i])
972-
if (m) {
973-
keywords.push(m[1])
974-
}
975-
}
976-
977-
// Pad with empty strings if fewer keywords were found than steps
978-
while (keywords.length < stepCount) {
979-
keywords.push('')
980-
}
981-
return keywords
982-
}
983-
984920
/**
985921
* The absolute path to the compiled Cucumber hooks file.
986922
* Kept for backwards compatibility — prefer using `withCucumber()` instead.

packages/service/tests/launcher.test.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ describe('DevToolsAppLauncher', () => {
4444

4545
describe('onPrepare', () => {
4646
it('should start devtools backend and update capabilities', async () => {
47-
vi.mocked(backend.start).mockResolvedValue({ server: mockServer } as any)
47+
vi.mocked(backend.start).mockResolvedValue({
48+
server: mockServer,
49+
port: 3000
50+
} as any)
4851
vi.mocked(remote).mockResolvedValue(mockBrowser as any)
4952

5053
const launcher = new DevToolsAppLauncher({ port: 3000 })
@@ -67,7 +70,8 @@ describe('DevToolsAppLauncher', () => {
6770
it('should use custom hostname', async () => {
6871
const customServer = { address: () => ({ port: 4000 }) }
6972
vi.mocked(backend.start).mockResolvedValue({
70-
server: customServer
73+
server: customServer,
74+
port: 4000
7175
} as any)
7276
vi.mocked(remote).mockResolvedValue(mockBrowser as any)
7377

@@ -122,7 +126,10 @@ describe('DevToolsAppLauncher', () => {
122126
address: () => null
123127
}
124128

125-
vi.mocked(backend.start).mockResolvedValue({ server: mockServer } as any)
129+
vi.mocked(backend.start).mockResolvedValue({
130+
server: mockServer,
131+
port: 0
132+
} as any)
126133

127134
const launcher = new DevToolsAppLauncher({ port: 3000 })
128135
const caps = [{ browserName: 'chrome' }] as any
@@ -138,7 +145,10 @@ describe('DevToolsAppLauncher', () => {
138145
address: () => ({ port: 3000 })
139146
}
140147

141-
vi.mocked(backend.start).mockResolvedValue({ server: mockServer } as any)
148+
vi.mocked(backend.start).mockResolvedValue({
149+
server: mockServer,
150+
port: 3000
151+
} as any)
142152

143153
const launcher = new DevToolsAppLauncher({ port: 3000 })
144154
const caps: any = {
@@ -152,7 +162,10 @@ describe('DevToolsAppLauncher', () => {
152162
})
153163

154164
it('should update multiple capabilities', async () => {
155-
vi.mocked(backend.start).mockResolvedValue({ server: mockServer } as any)
165+
vi.mocked(backend.start).mockResolvedValue({
166+
server: mockServer,
167+
port: 3000
168+
} as any)
156169
vi.mocked(remote).mockResolvedValue(mockBrowser as any)
157170

158171
const launcher = new DevToolsAppLauncher({ port: 3000 })
@@ -173,7 +186,10 @@ describe('DevToolsAppLauncher', () => {
173186
})
174187

175188
it('should pass devtoolsCapabilities to remote', async () => {
176-
vi.mocked(backend.start).mockResolvedValue({ server: mockServer } as any)
189+
vi.mocked(backend.start).mockResolvedValue({
190+
server: mockServer,
191+
port: 3000
192+
} as any)
177193
vi.mocked(remote).mockResolvedValue(mockBrowser as any)
178194

179195
const customCaps = {
@@ -208,7 +224,10 @@ describe('DevToolsAppLauncher', () => {
208224
: Promise.reject(new Error('Browser closed'))
209225
})
210226

211-
vi.mocked(backend.start).mockResolvedValue({ server: mockServer } as any)
227+
vi.mocked(backend.start).mockResolvedValue({
228+
server: mockServer,
229+
port: 3000
230+
} as any)
212231
vi.mocked(remote).mockResolvedValue(mockBrowser as any)
213232

214233
const launcher = new DevToolsAppLauncher({ port: 3000 })
@@ -235,7 +254,10 @@ describe('DevToolsAppLauncher', () => {
235254
new Error('Session already closed')
236255
)
237256

238-
vi.mocked(backend.start).mockResolvedValue({ server: mockServer } as any)
257+
vi.mocked(backend.start).mockResolvedValue({
258+
server: mockServer,
259+
port: 3000
260+
} as any)
239261
vi.mocked(remote).mockResolvedValue(mockBrowser as any)
240262

241263
const launcher = new DevToolsAppLauncher({ port: 3000 })
@@ -250,7 +272,10 @@ describe('DevToolsAppLauncher', () => {
250272
it('should handle full lifecycle', async () => {
251273
mockBrowser.getTitle.mockRejectedValue(new Error('Browser closed'))
252274

253-
vi.mocked(backend.start).mockResolvedValue({ server: mockServer } as any)
275+
vi.mocked(backend.start).mockResolvedValue({
276+
server: mockServer,
277+
port: 3000
278+
} as any)
254279
vi.mocked(remote).mockResolvedValue(mockBrowser as any)
255280

256281
const launcher = new DevToolsAppLauncher({

0 commit comments

Comments
 (0)