Skip to content

Commit 5aaab74

Browse files
committed
feat: module separation of AST parser
1 parent 7870758 commit 5aaab74

5 files changed

Lines changed: 37 additions & 20 deletions

File tree

packages/vscode-wdio-worker/package.json

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

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

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

4-
import { parseTestCases } from './js.js'
5-
import { getCucumberParser } from './utils.js'
4+
import { getAstParser, getCucumberParser } from './utils.js'
65
import type { ReadSpecsOptions } from '@vscode-wdio/types/api'
76
import type { WorkerMetaContext } from '@vscode-wdio/types/worker'
87

@@ -11,6 +10,11 @@ async function parseFeatureFile(context: WorkerMetaContext, contents: string, no
1110
return p.call(context, contents, normalizeSpecPath)
1211
}
1312

13+
async function parseJsFile(context: WorkerMetaContext, contents: string, normalizeSpecPath: string) {
14+
const p = await getAstParser(context)
15+
return p.call(context, contents, normalizeSpecPath)
16+
}
17+
1418
export async function parse(this: WorkerMetaContext, options: ReadSpecsOptions) {
1519
return await Promise.all(
1620
options.specs.map(async (spec) => {
@@ -20,7 +24,7 @@ export async function parse(this: WorkerMetaContext, options: ReadSpecsOptions)
2024
try {
2125
const testCases = isCucumberFeatureFile(normalizeSpecPath)
2226
? await parseFeatureFile(this, contents, normalizeSpecPath) // Parse Cucumber feature file
23-
: parseTestCases.call(this, contents, normalizeSpecPath) // Parse JavaScript/TypeScript test file
27+
: await parseJsFile(this, contents, normalizeSpecPath) // Parse JavaScript/TypeScript test file
2428

2529
this.log.debug(`Successfully parsed: ${normalizeSpecPath}`)
2630
return {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import path from 'node:path'
22

33
import type { WorkerMetaContext } from '@vscode-wdio/types/worker'
44
import type { parseCucumberFeature } from './cucumber.js'
5+
import type { parseTestCases } from './js.js'
56

67
export type CucumberParser = typeof parseCucumberFeature
8+
export type AstParser = typeof parseTestCases
79

810
const CUCUMBER_PARSER_PATH = path.resolve(__dirname, 'parser/cucumber.cjs')
11+
const AST_PARSER_PATH = path.resolve(__dirname, 'parser/ast.cjs')
912

1013
let cucumberParser: CucumberParser | undefined
14+
let astParser: AstParser | undefined
1115

1216
export async function getCucumberParser(context: WorkerMetaContext): Promise<CucumberParser> {
1317
if (cucumberParser) {
@@ -18,3 +22,13 @@ export async function getCucumberParser(context: WorkerMetaContext): Promise<Cuc
1822
cucumberParser = (await import(CUCUMBER_PARSER_PATH)).parseCucumberFeature as CucumberParser
1923
return cucumberParser
2024
}
25+
26+
export async function getAstParser(context: WorkerMetaContext): Promise<AstParser> {
27+
if (astParser) {
28+
context.log.debug('Use cached Ast parser')
29+
return astParser
30+
}
31+
context.log.debug('Import Ast parser')
32+
astParser = (await import(AST_PARSER_PATH)).parseTestCases as AstParser
33+
return astParser
34+
}

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
66
// Import parse function from parsers
77

88
import { parse } from '../../src/parsers/index.js'
9-
import { parseTestCases } from '../../src/parsers/js.js'
10-
import { getCucumberParser } from '../../src/parsers/utils.js'
9+
import { getAstParser, getCucumberParser } from '../../src/parsers/utils.js'
1110
import type { ReadSpecsOptions } from '@vscode-wdio/types/api'
1211
import type { WorkerMetaContext, TestData, CucumberTestData } from '@vscode-wdio/types/worker'
1312

14-
// Import the types
1513
// Mock fs module
1614
vi.mock('node:fs/promises', () => ({
1715
readFile: vi.fn(),
@@ -20,14 +18,9 @@ vi.mock('node:fs/promises', () => ({
2018
// Mock the parsers
2119
vi.mock('../../src/parsers/utils.js', () => ({
2220
getCucumberParser: vi.fn(),
21+
getAstParser: vi.fn(),
2322
}))
2423

25-
vi.mock('../../src/parsers/js.js', () => ({
26-
parseTestCases: vi.fn(),
27-
}))
28-
29-
// Import mocked modules
30-
3124
describe('Parser Index', () => {
3225
// Create mock context
3326
const mockContext: WorkerMetaContext = {
@@ -59,6 +52,7 @@ describe('Parser Index', () => {
5952
],
6053
},
6154
]
55+
const mockAstParser = vi.fn(()=>jsMockTestData)
6256

6357
const cucumberMockTestData: CucumberTestData[] = [
6458
{
@@ -93,8 +87,9 @@ describe('Parser Index', () => {
9387
vi.resetAllMocks()
9488

9589
// Mock the parseTestCases and parseCucumberFeature functions
96-
vi.mocked(parseTestCases).mockReturnValue(jsMockTestData)
90+
// vi.mocked(parseTestCases).mockReturnValue(jsMockTestData)
9791
vi.mocked(getCucumberParser).mockResolvedValue(mockCucumberParser)
92+
vi.mocked(getAstParser).mockResolvedValue(mockAstParser)
9893
})
9994

10095
afterEach(() => {
@@ -120,7 +115,7 @@ describe('Parser Index', () => {
120115
expect(result[0].tests).toEqual(jsMockTestData)
121116

122117
// Verify the correct parser was called
123-
expect(parseTestCases).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.js'))
118+
expect(mockAstParser).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.js'))
124119
expect(mockCucumberParser).not.toHaveBeenCalled()
125120

126121
// Verify logging
@@ -148,7 +143,7 @@ describe('Parser Index', () => {
148143
expect(result[0].tests).toEqual(jsMockTestData)
149144

150145
// Verify the correct parser was called
151-
expect(parseTestCases).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.ts'))
146+
expect(mockAstParser).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.ts'))
152147
expect(mockCucumberParser).not.toHaveBeenCalled()
153148
})
154149

@@ -174,7 +169,7 @@ describe('Parser Index', () => {
174169
cucumberFeatureContent,
175170
path.normalize('/path/to/test.feature')
176171
)
177-
expect(parseTestCases).not.toHaveBeenCalled()
172+
expect(mockAstParser).not.toHaveBeenCalled()
178173
})
179174

180175
it('should handle uppercase feature file extensions', async () => {
@@ -199,7 +194,7 @@ describe('Parser Index', () => {
199194
cucumberFeatureContent,
200195
path.normalize('/path/to/test.FEATURE')
201196
)
202-
expect(parseTestCases).not.toHaveBeenCalled()
197+
expect(mockAstParser).not.toHaveBeenCalled()
203198
})
204199

205200
it('should parse multiple spec files', async () => {
@@ -232,7 +227,7 @@ describe('Parser Index', () => {
232227
expect(result[1].tests).toEqual(cucumberMockTestData)
233228

234229
// Verify the correct parsers were called
235-
expect(parseTestCases).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.js'))
230+
expect(mockAstParser).toHaveBeenCalledWith(jsTestContent, path.normalize('/path/to/test.js'))
236231
expect(mockCucumberParser).toHaveBeenCalledWith(
237232
cucumberFeatureContent,
238233
path.normalize('/path/to/test.feature')
@@ -269,7 +264,7 @@ describe('Parser Index', () => {
269264
expect(result[0].spec).toBe(path.normalize('C:\\path\\to\\test.js'))
270265

271266
// Verify the parser was called with the normalized path
272-
expect(parseTestCases).toHaveBeenCalledWith(jsTestContent, path.normalize('C:\\path\\to\\test.js'))
267+
expect(mockAstParser).toHaveBeenCalledWith(jsTestContent, path.normalize('C:\\path\\to\\test.js'))
273268
})
274269

275270
it('should handle empty specs array', async () => {
@@ -284,7 +279,7 @@ describe('Parser Index', () => {
284279
// Verify
285280
expect(result).toEqual([])
286281
expect(fs.readFile).not.toHaveBeenCalled()
287-
expect(parseTestCases).not.toHaveBeenCalled()
282+
expect(mockAstParser).not.toHaveBeenCalled()
288283
expect(mockCucumberParser).not.toHaveBeenCalled()
289284
})
290285
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { createTempConfigFile } from '@vscode-wdio/worker/windows'
2+
export { parseTestCases } from '@vscode-wdio/worker/parser/ast'

0 commit comments

Comments
 (0)