Skip to content

Commit 0987af0

Browse files
committed
Code refactor
1 parent 3ba5c3c commit 0987af0

5 files changed

Lines changed: 43 additions & 87 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import type { SuiteStats, TestStats } from '../types.js'
88
import type { TestReporter } from '../reporter.js'
99
import { generateStableUid } from './utils.js'
1010

11-
// const log = logger('@wdio/nightwatch-devtools:suiteManager')
12-
1311
export class SuiteManager {
1412
private currentSuiteByFile = new Map<string, SuiteStats>()
1513

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ export function getCallSourceFromStack(): {
179179
filePath = filePath.replace('file://', '')
180180
}
181181
// Remove line:col from filePath
182-
const cleanFilePath: string = filePath.split(':')[0]
182+
const cleanFilePath = filePath.split(':')[0]
183183
// Use full path with line number for callSource so Source tab can match it
184-
const callSource: string = `${cleanFilePath}:${userFrame.lineNumber || 0}`
184+
const callSource = `${cleanFilePath}:${userFrame.lineNumber || 0}`
185185
return { filePath: cleanFilePath, callSource }
186186
}
187187
return { filePath: undefined, callSource: 'unknown:0' }
@@ -199,7 +199,7 @@ export function findTestFileByName(
199199
return undefined
200200
}
201201
// Clean up filename - remove extensions and normalize
202-
const baseFilename: string = filename.replace(/\.[cm]?[jt]sx?$/, '')
202+
const baseFilename = filename.replace(/\.[cm]?[jt]sx?$/, '')
203203
// Recursively search directories
204204
function searchDir(dir: string, depth = 0): string | undefined {
205205
if (depth > 5) {

packages/nightwatch-devtools/src/index.ts

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,8 @@ class NightwatchDevToolsPlugin {
422422
this.suiteManager.finalizeSuiteState(featureSuite)
423423
}
424424

425-
if (scenarioState === TEST_STATE.PASSED) {
426-
this.#passCount++
427-
} else if (scenarioState === TEST_STATE.SKIPPED) {
428-
this.#skipCount++
429-
} else {
430-
this.#failCount++
431-
}
432-
const icon =
433-
scenarioState === TEST_STATE.PASSED
434-
? '✅'
435-
: scenarioState === TEST_STATE.SKIPPED
436-
? '⏭'
437-
: '❌'
425+
this.#incrementCount(scenarioState)
426+
const icon = this.#testIcon(scenarioState)
438427
const durationSec = (duration / 1000).toFixed(2)
439428
log.info(` ${icon} ${pickle?.name ?? 'Unknown'} (${durationSec}s)`)
440429

@@ -588,19 +577,8 @@ class NightwatchDevToolsPlugin {
588577
runningTest._duration = parseFloat(testcase.time || '0') * 1000
589578
this.testManager.updateTestState(runningTest, testState)
590579
this.testManager.markTestAsProcessed(testFile, runningTest.title)
591-
if (testState === TEST_STATE.PASSED) {
592-
this.#passCount++
593-
} else if (testState === TEST_STATE.SKIPPED) {
594-
this.#skipCount++
595-
} else {
596-
this.#failCount++
597-
}
598-
const prevIcon =
599-
testState === TEST_STATE.PASSED
600-
? '✅'
601-
: testState === TEST_STATE.SKIPPED
602-
? '⏭'
603-
: '❌'
580+
this.#incrementCount(testState)
581+
const prevIcon = this.#testIcon(testState)
604582
log.info(
605583
` ${prevIcon} ${runningTest.title} (${(runningTest._duration / 1000).toFixed(2)}s)`
606584
)
@@ -701,12 +679,8 @@ class NightwatchDevToolsPlugin {
701679
duration
702680
)
703681
this.testManager.markTestAsProcessed(testFile, runningTest.title)
704-
if (testState === TEST_STATE.PASSED) {
705-
this.#passCount++
706-
} else {
707-
this.#failCount++
708-
}
709-
const icon = testState === TEST_STATE.PASSED ? '✅' : '❌'
682+
this.#incrementCount(testState)
683+
const icon = this.#testIcon(testState)
710684
log.info(
711685
` ${icon} ${runningTest.title} (${(duration / 1000).toFixed(2)}s)`
712686
)
@@ -732,19 +706,8 @@ class NightwatchDevToolsPlugin {
732706
new Date(),
733707
dur
734708
)
735-
if (testState === TEST_STATE.PASSED) {
736-
this.#passCount++
737-
} else if (testState === TEST_STATE.SKIPPED) {
738-
this.#skipCount++
739-
} else {
740-
this.#failCount++
741-
}
742-
const icon =
743-
testState === TEST_STATE.PASSED
744-
? '✅'
745-
: testState === TEST_STATE.SKIPPED
746-
? '⏭'
747-
: '❌'
709+
this.#incrementCount(testState)
710+
const icon = this.#testIcon(testState)
748711
log.info(
749712
` ${icon} ${currentTestName} (${(dur / 1000).toFixed(2)}s)`
750713
)
@@ -855,6 +818,24 @@ class NightwatchDevToolsPlugin {
855818
}
856819
}
857820

821+
#incrementCount(state: TestStats['state']): void {
822+
if (state === TEST_STATE.PASSED) {
823+
this.#passCount++
824+
} else if (state === TEST_STATE.SKIPPED) {
825+
this.#skipCount++
826+
} else {
827+
this.#failCount++
828+
}
829+
}
830+
831+
#testIcon(state: TestStats['state']): string {
832+
return state === TEST_STATE.PASSED
833+
? '✅'
834+
: state === TEST_STATE.SKIPPED
835+
? '⏭'
836+
: '❌'
837+
}
838+
858839
registerEventHandlers(eventHub: any): void {
859840
this.#isCucumberRunner = eventHub.runner === 'cucumber'
860841
if (this.#isCucumberRunner) {

packages/nightwatch-devtools/src/reporter.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logger from '@wdio/logger'
2+
import { DEFAULTS } from './constants.js'
23
import {
34
extractTestMetadata,
45
generateStableUid,
@@ -129,36 +130,14 @@ export class TestReporter {
129130
* Called when a test passes
130131
*/
131132
onTestPass(testStats: TestStats) {
132-
// Search all suites for this test (not just current suite)
133-
for (const suite of this.#allSuites) {
134-
const testIndex = suite.tests.findIndex(
135-
(t) => (typeof t === 'string' ? t : t.uid) === testStats.uid
136-
)
137-
if (testIndex !== -1) {
138-
suite.tests[testIndex] = testStats
139-
break
140-
}
141-
}
142-
143-
this.#sendUpstream()
133+
this.onTestEnd(testStats)
144134
}
145135

146136
/**
147137
* Called when a test fails
148138
*/
149139
onTestFail(testStats: TestStats) {
150-
// Search all suites for this test (not just current suite)
151-
for (const suite of this.#allSuites) {
152-
const testIndex = suite.tests.findIndex(
153-
(t) => (typeof t === 'string' ? t : t.uid) === testStats.uid
154-
)
155-
if (testIndex !== -1) {
156-
suite.tests[testIndex] = testStats
157-
break
158-
}
159-
}
160-
161-
this.#sendUpstream()
140+
this.onTestEnd(testStats)
162141
}
163142

164143
/**
@@ -181,7 +160,7 @@ export class TestReporter {
181160
file: suiteStats.file,
182161
fullTitle: `${suiteStats.title} ${testName}`
183162
} as TestStats),
184-
cid: '0-0',
163+
cid: DEFAULTS.CID,
185164
title: testName,
186165
fullTitle: `${suiteStats.title} ${testName}`,
187166
parent: suiteStats.uid,
@@ -190,8 +169,8 @@ export class TestReporter {
190169
end: new Date(),
191170
type: 'test',
192171
file: suiteStats.file,
193-
retries: 0,
194-
_duration: 0,
172+
retries: DEFAULTS.RETRIES,
173+
_duration: DEFAULTS.DURATION,
195174
hooks: []
196175
}
197176

packages/nightwatch-devtools/src/session.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ export class SessionCapturer {
253253
})
254254
}
255255

256+
#serializeError(error: Error | undefined) {
257+
return error
258+
? { name: error.name, message: error.message, stack: error.stack }
259+
: undefined
260+
}
261+
256262
/**
257263
* Capture a command execution
258264
* @returns true if command was captured, false if it was skipped as a duplicate
@@ -267,13 +273,7 @@ export class SessionCapturer {
267273
timestamp?: number
268274
): Promise<boolean> {
269275
// Serialize error properly (Error objects don't JSON.stringify well)
270-
const serializedError = error
271-
? {
272-
name: error.name,
273-
message: error.message,
274-
stack: error.stack
275-
}
276-
: undefined
276+
const serializedError = this.#serializeError(error)
277277

278278
const commandId = this.#commandCounter++
279279
const commandLogEntry: CommandLog & { _id?: number } = {
@@ -435,9 +435,7 @@ export class SessionCapturer {
435435
// Allow the slot to be re-used by a new entry
436436
this.#sentCommandIds.delete(oldId)
437437

438-
const serializedError = error
439-
? { name: error.name, message: error.message, stack: error.stack }
440-
: undefined
438+
const serializedError = this.#serializeError(error)
441439
const commandId = this.#commandCounter++
442440
const entry: CommandLog & { _id?: number } = {
443441
_id: commandId,

0 commit comments

Comments
 (0)