Skip to content

Commit 7870758

Browse files
committed
feat: module separation of Cucumber parser
1 parent 83e9be3 commit 7870758

8 files changed

Lines changed: 50 additions & 14 deletions

File tree

packages/vscode-wdio-worker/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
},
1010
"./windows": {
1111
"import": "./dist/config.js"
12+
},
13+
"./parser/cucumber": {
14+
"import": "./dist/parsers/cucumber.js"
1215
}
1316
},
1417
"scripts": {

packages/vscode-wdio-worker/src/parsers/cucumber.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { CucumberTestData, StepType, WorkerMetaContext } from '@vscode-wdio
1212
* @returns Array of test case information
1313
*/
1414
export function parseCucumberFeature(this: WorkerMetaContext, fileContent: string, uri: string) {
15-
this.log.debug('Cucumber parser is used.')
15+
this.log.debug(`Start parsing the cucumber feature file: ${uri}`)
1616
try {
1717
// Initialize the parser components
1818
const builder = new AstBuilder(IdGenerator.uuid())

packages/vscode-wdio-worker/src/parsers/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import * as fs from 'node:fs/promises'
22
import path from 'node:path'
33

4-
import { parseCucumberFeature } from './cucumber.js'
54
import { parseTestCases } from './js.js'
5+
import { getCucumberParser } from './utils.js'
66
import type { ReadSpecsOptions } from '@vscode-wdio/types/api'
77
import type { WorkerMetaContext } from '@vscode-wdio/types/worker'
88

9+
async function parseFeatureFile(context: WorkerMetaContext, contents: string, normalizeSpecPath: string) {
10+
const p = await getCucumberParser(context)
11+
return p.call(context, contents, normalizeSpecPath)
12+
}
13+
914
export async function parse(this: WorkerMetaContext, options: ReadSpecsOptions) {
1015
return await Promise.all(
1116
options.specs.map(async (spec) => {
@@ -14,7 +19,7 @@ export async function parse(this: WorkerMetaContext, options: ReadSpecsOptions)
1419
const contents = await fs.readFile(normalizeSpecPath, { encoding: 'utf8' })
1520
try {
1621
const testCases = isCucumberFeatureFile(normalizeSpecPath)
17-
? parseCucumberFeature.call(this, contents, normalizeSpecPath) // Parse Cucumber feature file
22+
? await parseFeatureFile(this, contents, normalizeSpecPath) // Parse Cucumber feature file
1823
: parseTestCases.call(this, contents, normalizeSpecPath) // Parse JavaScript/TypeScript test file
1924

2025
this.log.debug(`Successfully parsed: ${normalizeSpecPath}`)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import path from 'node:path'
2+
3+
import type { WorkerMetaContext } from '@vscode-wdio/types/worker'
4+
import type { parseCucumberFeature } from './cucumber.js'
5+
6+
export type CucumberParser = typeof parseCucumberFeature
7+
8+
const CUCUMBER_PARSER_PATH = path.resolve(__dirname, 'parser/cucumber.cjs')
9+
10+
let cucumberParser: CucumberParser | undefined
11+
12+
export async function getCucumberParser(context: WorkerMetaContext): Promise<CucumberParser> {
13+
if (cucumberParser) {
14+
context.log.debug('Use cached Cucumber parser')
15+
return cucumberParser
16+
}
17+
context.log.debug('Import Cucumber parser')
18+
cucumberParser = (await import(CUCUMBER_PARSER_PATH)).parseCucumberFeature as CucumberParser
19+
return cucumberParser
20+
}

packages/vscode-wdio-worker/tests/parsers/cucumber.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ describe('Cucumber Parser', () => {
340340
parseCucumberFeature.call(mockContext, basicFeatureContent, 'basic-feature.feature')
341341

342342
// Verify debug log was called
343-
expect(mockContext.log.debug).toHaveBeenCalledWith('Cucumber parser is used.')
343+
expect(mockContext.log.debug).toHaveBeenCalledWith(
344+
'Start parsing the cucumber feature file: basic-feature.feature'
345+
)
344346
})
345347

346348
it('should include proper source ranges for each element', () => {

packages/vscode-wdio-worker/tests/parsers/index.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
55

66
// Import parse function from parsers
77

8-
import { parseCucumberFeature } from '../../src/parsers/cucumber.js'
98
import { parse } from '../../src/parsers/index.js'
109
import { parseTestCases } from '../../src/parsers/js.js'
10+
import { getCucumberParser } from '../../src/parsers/utils.js'
1111
import type { ReadSpecsOptions } from '@vscode-wdio/types/api'
1212
import type { WorkerMetaContext, TestData, CucumberTestData } from '@vscode-wdio/types/worker'
1313

@@ -18,8 +18,8 @@ vi.mock('node:fs/promises', () => ({
1818
}))
1919

2020
// Mock the parsers
21-
vi.mock('../../src/parsers/cucumber.js', () => ({
22-
parseCucumberFeature: vi.fn(),
21+
vi.mock('../../src/parsers/utils.js', () => ({
22+
getCucumberParser: vi.fn(),
2323
}))
2424

2525
vi.mock('../../src/parsers/js.js', () => ({
@@ -87,13 +87,14 @@ describe('Parser Index', () => {
8787
metadata: {},
8888
},
8989
]
90+
const mockCucumberParser = vi.fn(()=>cucumberMockTestData)
9091

9192
beforeEach(() => {
9293
vi.resetAllMocks()
9394

9495
// Mock the parseTestCases and parseCucumberFeature functions
9596
vi.mocked(parseTestCases).mockReturnValue(jsMockTestData)
96-
vi.mocked(parseCucumberFeature).mockReturnValue(cucumberMockTestData)
97+
vi.mocked(getCucumberParser).mockResolvedValue(mockCucumberParser)
9798
})
9899

99100
afterEach(() => {
@@ -120,7 +121,7 @@ describe('Parser Index', () => {
120121

121122
// Verify the correct parser was called
122123
expect(parseTestCases).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.js'))
123-
expect(parseCucumberFeature).not.toHaveBeenCalled()
124+
expect(mockCucumberParser).not.toHaveBeenCalled()
124125

125126
// Verify logging
126127
expect(mockContext.log.debug).toHaveBeenCalledWith(`Parse spec file: ${path.normalize('/path/to/test.js')}`)
@@ -148,7 +149,7 @@ describe('Parser Index', () => {
148149

149150
// Verify the correct parser was called
150151
expect(parseTestCases).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.ts'))
151-
expect(parseCucumberFeature).not.toHaveBeenCalled()
152+
expect(mockCucumberParser).not.toHaveBeenCalled()
152153
})
153154

154155
it('should parse Cucumber feature files correctly', async () => {
@@ -169,7 +170,7 @@ describe('Parser Index', () => {
169170
expect(result[0].tests).toEqual(cucumberMockTestData)
170171

171172
// Verify the correct parser was called
172-
expect(parseCucumberFeature).toHaveBeenCalledWith(
173+
expect(mockCucumberParser).toHaveBeenCalledWith(
173174
cucumberFeatureContent,
174175
path.normalize('/path/to/test.feature')
175176
)
@@ -194,7 +195,7 @@ describe('Parser Index', () => {
194195
expect(result[0].tests).toEqual(cucumberMockTestData)
195196

196197
// Verify the correct parser was called
197-
expect(parseCucumberFeature).toHaveBeenCalledWith(
198+
expect(mockCucumberParser).toHaveBeenCalledWith(
198199
cucumberFeatureContent,
199200
path.normalize('/path/to/test.FEATURE')
200201
)
@@ -232,7 +233,7 @@ describe('Parser Index', () => {
232233

233234
// Verify the correct parsers were called
234235
expect(parseTestCases).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.js'))
235-
expect(parseCucumberFeature).toHaveBeenCalledWith(
236+
expect(mockCucumberParser).toHaveBeenCalledWith(
236237
cucumberFeatureContent,
237238
path.normalize('/path/to/test.feature')
238239
)
@@ -284,7 +285,7 @@ describe('Parser Index', () => {
284285
expect(result).toEqual([])
285286
expect(fs.readFile).not.toHaveBeenCalled()
286287
expect(parseTestCases).not.toHaveBeenCalled()
287-
expect(parseCucumberFeature).not.toHaveBeenCalled()
288+
expect(mockCucumberParser).not.toHaveBeenCalled()
288289
})
289290
})
290291
})

packages/vscode-webdriverio/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"./parser/ast": {
2424
"requireSource": "./src/parser/ast.ts",
2525
"require": "./dist/parser/ast.cjs"
26+
},
27+
"./parser/cucumber": {
28+
"requireSource": "./src/parser/cucumber.ts",
29+
"require": "./dist/parser/cucumber.cjs"
2630
}
2731
},
2832
"scripts": {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { parseCucumberFeature } from '@vscode-wdio/worker/parser/cucumber'

0 commit comments

Comments
 (0)