@@ -8,10 +8,33 @@ import { createRunProfile } from './utils.js'
88import { TEST_ID_SEPARATOR } from '../constants.js'
99import { log } from '../utils/logger.js'
1010
11- import type { WdioConfigTestItem , WorkspaceTestItem } from './types.js'
11+ import type { TestItemMetadata } from './types.js'
1212import type { ServerManager } from '../api/manager.js'
1313import type { ExtensionConfigManager } from '../config/index.js'
1414
15+ class MetadataRepository {
16+ private static testMetadataRepository = new WeakMap < vscode . TestItem , TestItemMetadata > ( )
17+ public static getMetadata ( testItem : vscode . TestItem ) {
18+ const metadata = this . testMetadataRepository . get ( testItem )
19+ if ( ! metadata ) {
20+ throw new Error ( "The metadata for TestItem is not set. This is extension's bug." )
21+ }
22+ return metadata
23+ }
24+
25+ public static getRepository ( testItem : vscode . TestItem ) {
26+ const metadata = this . getMetadata ( testItem )
27+ if ( ! metadata . repository ) {
28+ throw new Error ( "The metadata for TestItem is not set. This is extension's bug." )
29+ }
30+ return metadata . repository
31+ }
32+
33+ public static setMetadata ( testItem : vscode . TestItem , metadata : TestItemMetadata ) {
34+ this . testMetadataRepository . set ( testItem , metadata )
35+ }
36+ }
37+
1538/**
1639 * workspace -- managed by this class
1740 * - configuration file (e.g. wdio.conf.js) -- managed by this class
@@ -22,18 +45,19 @@ import type { ExtensionConfigManager } from '../config/index.js'
2245
2346const LOADING_TEST_ITEM_ID = '_resolving'
2447
25- export class RepositoryManager implements vscode . Disposable {
48+ export class RepositoryManager extends MetadataRepository implements vscode . Disposable {
2649 private readonly _repos = new Set < TestRepository > ( )
2750 private _loadingTestItem : vscode . TestItem
28- private _workspaceTestItems : WorkspaceTestItem [ ] = [ ]
29- private _wdioConfigTestItems : WdioConfigTestItem [ ] = [ ]
51+ private _workspaceTestItems : vscode . TestItem [ ] = [ ]
52+ private _wdioConfigTestItems : vscode . TestItem [ ] = [ ]
3053 private isCreatedDefaultProfile = false
3154
3255 constructor (
3356 public readonly controller : vscode . TestController ,
3457 public readonly configManager : ExtensionConfigManager ,
3558 private readonly serverManager : ServerManager
3659 ) {
60+ super ( )
3761 this . _loadingTestItem = this . controller . createTestItem ( LOADING_TEST_ITEM_ID , 'Resolving WebdriverIO Tests...' )
3862 this . _loadingTestItem . sortText = '.0' // show at first line
3963 this . _loadingTestItem . busy = true
@@ -82,7 +106,8 @@ export class RepositoryManager implements vscode.Disposable {
82106 for ( const workspaceTestItem of affectedWorkspaceItems ) {
83107 const configTestItem = await this . createWdioConfigTestItem ( workspaceTestItem , wdioConfigPath )
84108
85- await configTestItem . metadata . repository . discoverAllTests ( )
109+ const repo = RepositoryManager . getRepository ( configTestItem )
110+ await repo . discoverAllTests ( )
86111 if ( ! this . configManager . isMultiWorkspace ) {
87112 this . controller . items . add ( configTestItem )
88113 }
@@ -96,14 +121,12 @@ export class RepositoryManager implements vscode.Disposable {
96121 log . debug ( `Remove the config file from ${ affectedWorkspaceItems . length } workspace(s)` )
97122 const configUri = convertPathToUri ( wdioConfigPath )
98123 for ( const workspaceItem of affectedWorkspaceItems ) {
99- const config = workspaceItem . children . get (
100- this . generateConfigTestItemId ( workspaceItem , configUri )
101- ) as WdioConfigTestItem
124+ const config = workspaceItem . children . get ( this . generateConfigTestItemId ( workspaceItem , configUri ) )
102125 if ( ! config ) {
103126 continue
104127 }
105128 log . debug ( `Remove the TestItem: ${ config . id } ` )
106- const targetRepo = config . metadata . repository
129+ const targetRepo = RepositoryManager . getRepository ( config )
107130 targetRepo . dispose ( )
108131 this . _repos . delete ( targetRepo )
109132
@@ -140,22 +163,24 @@ export class RepositoryManager implements vscode.Disposable {
140163 `workspace:${ workspaceFolder . uri . fsPath } ` ,
141164 workspaceFolder . name ,
142165 workspaceFolder . uri
143- ) as WorkspaceTestItem
144- workspaceItem [ 'metadata' ] = {
166+ )
167+ RepositoryManager . setMetadata ( workspaceItem , {
168+ uri : workspaceFolder . uri ,
145169 isWorkspace : true ,
146170 isConfigFile : false ,
147171 isSpecFile : false ,
148- }
172+ isTestcase : false ,
173+ } )
149174 return workspaceItem
150175 }
151176
152- private async createWdioConfigTestItem ( workspaceTestItem : WorkspaceTestItem , wdioConfigPath : string ) {
153- const configUri = convertPathToUri ( wdioConfigPath )
177+ private async createWdioConfigTestItem ( workspaceTestItem : vscode . TestItem , wdioConfigPath : string ) {
178+ const uri = convertPathToUri ( wdioConfigPath )
154179 const configItem = this . controller . createTestItem (
155- this . generateConfigTestItemId ( workspaceTestItem , configUri ) ,
180+ this . generateConfigTestItemId ( workspaceTestItem , uri ) ,
156181 basename ( wdioConfigPath ) ,
157- configUri
158- ) as WdioConfigTestItem
182+ uri
183+ )
159184
160185 workspaceTestItem . children . add ( configItem )
161186 this . _wdioConfigTestItems . push ( configItem )
@@ -165,17 +190,20 @@ export class RepositoryManager implements vscode.Disposable {
165190 this . _repos . add ( repo )
166191
167192 configItem . description = relative ( workspaceTestItem . uri ! . fsPath , dirname ( wdioConfigPath ) )
168- configItem [ 'metadata' ] = {
193+
194+ RepositoryManager . setMetadata ( configItem , {
195+ uri,
169196 isWorkspace : false ,
170197 isConfigFile : true ,
171198 isSpecFile : false ,
199+ isTestcase : false ,
172200 repository : repo ,
173201 runProfiles : createRunProfile . call ( this , configItem , ! this . isCreatedDefaultProfile ) ,
174- }
202+ } )
175203 return configItem
176204 }
177205
178- private generateConfigTestItemId ( workspaceTestItem : WorkspaceTestItem , configUri : vscode . Uri ) {
206+ private generateConfigTestItemId ( workspaceTestItem : vscode . TestItem , configUri : vscode . Uri ) {
179207 return [ workspaceTestItem . id , `config:${ configUri . fsPath } ` ] . join ( TEST_ID_SEPARATOR )
180208 }
181209
0 commit comments