Skip to content

Commit b332704

Browse files
committed
refactor: ctx->vitest
1 parent 7008140 commit b332704

File tree

3 files changed

+54
-54
lines changed

3 files changed

+54
-54
lines changed

src/worker/coverage.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class ExtensionCoverageManager {
1515
constructor(
1616
private worker: ExtensionWorker,
1717
) {
18-
this._config = worker.ctx.config.coverage
19-
const projects = new Set([...worker.ctx.projects, worker.getRootTestProject()])
18+
this._config = worker.vitest.config.coverage
19+
const projects = new Set([...worker.vitest.projects, worker.getRootTestProject()])
2020
projects.forEach((project) => {
2121
Object.defineProperty(project.config, 'coverage', {
2222
get: () => {
@@ -27,7 +27,7 @@ export class ExtensionCoverageManager {
2727
},
2828
})
2929
})
30-
Object.defineProperty(worker.ctx, 'coverageProvider', {
30+
Object.defineProperty(worker.vitest, 'coverageProvider', {
3131
get: () => {
3232
if (this.enabled)
3333
return this._provider
@@ -56,7 +56,7 @@ export class ExtensionCoverageManager {
5656
}
5757

5858
public async enable() {
59-
const vitest = this.worker.ctx
59+
const vitest = this.worker.vitest
6060
this._enabled = true
6161

6262
const jsonReporter = this._config.reporter.find(([name]) => name === 'json')
@@ -69,7 +69,7 @@ export class ExtensionCoverageManager {
6969
this._config.reportOnFailure = true
7070
this._config.reportsDirectory = join(tmpdir(), `vitest-coverage-${randomUUID()}`)
7171

72-
this.worker.ctx.logger.log('Running coverage with configuration:', this.config)
72+
this.worker.vitest.logger.log('Running coverage with configuration:', this.config)
7373

7474
if (!this._provider) {
7575
// @ts-expect-error private method
@@ -82,7 +82,7 @@ export class ExtensionCoverageManager {
8282
}
8383

8484
private get coverageProvider() {
85-
return (this.worker.ctx as any).coverageProvider as CoverageProvider | null | undefined
85+
return (this.worker.vitest as any).coverageProvider as CoverageProvider | null | undefined
8686
}
8787

8888
public disable() {
@@ -92,7 +92,7 @@ export class ExtensionCoverageManager {
9292
async waitForReport() {
9393
if (!this.enabled)
9494
return null
95-
const ctx = this.worker.ctx
95+
const ctx = this.worker.vitest
9696
const coverage = ctx.config.coverage
9797
if (!coverage.enabled || !this.coverageProvider)
9898
return null

src/worker/watcher.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class ExtensionWorkerWatcher {
1414
constructor(extension: ExtensionWorker) {
1515
// eslint-disable-next-line ts/no-this-alias
1616
const state = this
17-
const ctx = extension.ctx
17+
const vitest = extension.vitest
1818
;(extension.getRootTestProject().provide as <T extends keyof ProvidedContext>(key: T, value: ProvidedContext[T]) => void)('__vscode', {
1919
get continuousFiles() {
2020
return state.files || []
@@ -28,9 +28,9 @@ export class ExtensionWorkerWatcher {
2828
})
2929

3030
// @ts-expect-error modifying a private property
31-
const originalScheduleRerun = ctx.scheduleRerun.bind(ctx)
31+
const originalScheduleRerun = vitest.scheduleRerun.bind(vitest)
3232
// @ts-expect-error modifying a private property
33-
ctx.scheduleRerun = async function (files: string[]) {
33+
vitest.scheduleRerun = async function (files: string[]) {
3434
// if trigger is not a test file, remove all non-continious files from this.changedTests
3535
const triggerFile = files[0]
3636
const isTestFileTrigger = this.changedTests.has(triggerFile)
@@ -55,10 +55,10 @@ export class ExtensionWorkerWatcher {
5555
}
5656

5757
extension.setGlobalTestNamePattern(ExtensionWorker.COLLECT_NAME_PATTERN)
58-
ctx.logger.log('Collecting tests due to file changes:', ...files.map(f => relative(ctx.config.root, f)))
58+
vitest.logger.log('Collecting tests due to file changes:', ...files.map(f => relative(vitest.config.root, f)))
5959

6060
if (astSpecs.length) {
61-
ctx.logger.log('Collecting using AST explorer...')
61+
vitest.logger.log('Collecting using AST explorer...')
6262
await extension.astCollect(astSpecs)
6363
this.changedTests.clear()
6464
return await originalScheduleRerun.call(this, [])
@@ -72,9 +72,9 @@ export class ExtensionWorkerWatcher {
7272
const namePattern = state.testNamePattern ? new RegExp(state.testNamePattern) : undefined
7373
extension.setGlobalTestNamePattern(namePattern)
7474
if (state.watchEveryFile) {
75-
ctx.logger.log(
75+
vitest.logger.log(
7676
'Rerunning all tests due to file changes:',
77-
...files.map(f => relative(ctx.config.root, f)),
77+
...files.map(f => relative(vitest.config.root, f)),
7878
namePattern ? `with pattern ${namePattern}` : '',
7979
)
8080
return await originalScheduleRerun.call(this, files)
@@ -91,9 +91,9 @@ export class ExtensionWorkerWatcher {
9191
}
9292

9393
if (this.changedTests.size) {
94-
ctx.logger.log(
94+
vitest.logger.log(
9595
'Rerunning tests due to file changes:',
96-
...[...this.changedTests].map(f => relative(ctx.config.root, f)),
96+
...[...this.changedTests].map(f => relative(vitest.config.root, f)),
9797
namePattern ? `with pattern ${namePattern}` : '',
9898
)
9999
}

src/worker/worker.ts

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
1717
public static COLLECT_NAME_PATTERN = '$a'
1818

1919
constructor(
20-
public readonly ctx: VitestCore,
20+
public readonly vitest: VitestCore,
2121
private readonly debug = false,
2222
public readonly alwaysAstCollect = false,
2323
private emitter: WorkerWSEventEmitter,
@@ -31,15 +31,15 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
3131
}
3232

3333
private get configOverride(): Partial<ResolvedConfig> {
34-
return (this.ctx as any).configOverride
34+
return (this.vitest as any).configOverride
3535
}
3636

3737
public setGlobalTestNamePattern(pattern?: string | RegExp): void {
3838
if (pattern == null || pattern === '') {
3939
this.configOverride.testNamePattern = undefined
4040
}
41-
else if ('setGlobalTestNamePattern' in this.ctx) {
42-
return this.ctx.setGlobalTestNamePattern(pattern)
41+
else if ('setGlobalTestNamePattern' in this.vitest) {
42+
return this.vitest.setGlobalTestNamePattern(pattern)
4343
}
4444
else {
4545
this.configOverride.testNamePattern = typeof pattern === 'string'
@@ -50,22 +50,22 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
5050

5151
public getRootTestProject(): WorkspaceProject {
5252
// vitest 3 uses getRootProject
53-
if ('getRootProject' in this.ctx) {
54-
return this.ctx.getRootProject()
53+
if ('getRootProject' in this.vitest) {
54+
return this.vitest.getRootProject()
5555
}
5656
// vitest 3.beta uses getRootTestProject
57-
if ('getRootTestProject' in this.ctx) {
58-
return ((this.ctx as any).getRootTestProject as () => WorkspaceProject)()
57+
if ('getRootTestProject' in this.vitest) {
58+
return ((this.vitest as any).getRootTestProject as () => WorkspaceProject)()
5959
}
60-
return (this.ctx as any).getCoreWorkspaceProject()
60+
return (this.vitest as any).getCoreWorkspaceProject()
6161
}
6262

6363
public async collectTests(files: [projectName: string, filepath: string][]) {
6464
const astCollect: [project: WorkspaceProject, filepath: string][] = []
6565
const otherTests: [project: WorkspaceProject, filepath: string][] = []
6666

6767
for (const [projectName, filepath] of files) {
68-
const project = this.ctx.projects.find(project => project.getName() === projectName)
68+
const project = this.vitest.projects.find(project => project.getName() === projectName)
6969
assert(project, `Project ${projectName} not found for file ${filepath}`)
7070
if (this.alwaysAstCollect || project.config.browser.enabled) {
7171
astCollect.push([project, filepath])
@@ -140,26 +140,26 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
140140

141141
public async runTests(specsOrPaths: SerializedTestSpecification[] | string[] | undefined, testNamePattern?: string) {
142142
// @ts-expect-error private method in Vitest <=2.1.5
143-
await this.ctx.initBrowserProviders?.()
143+
await this.vitest.initBrowserProviders?.()
144144

145145
const specs = await this.resolveTestSpecs(specsOrPaths)
146146

147147
await this.runTestFiles(specs, testNamePattern, !specsOrPaths)
148148

149149
// debugger never runs in watch mode
150150
if (this.debug) {
151-
await this.ctx.close()
151+
await this.vitest.close()
152152
this.emitter.close()
153153
}
154154
}
155155

156156
public cancelRun() {
157-
return this.ctx.cancelCurrentRun('keyboard-input')
157+
return this.vitest.cancelCurrentRun('keyboard-input')
158158
}
159159

160160
public async getFiles(): Promise<[project: string, file: string][]> {
161161
// reset cached test files list
162-
this.ctx.projects.forEach((project) => {
162+
this.vitest.projects.forEach((project) => {
163163
// testFilesList is private
164164
(project as any).testFilesList = null
165165
})
@@ -168,18 +168,18 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
168168
}
169169

170170
private async globTestSpecifications(filters?: string[]): Promise<TestSpecification[]> {
171-
if ('globTestSpecifications' in this.ctx) {
172-
return this.ctx.globTestSpecifications(filters)
171+
if ('globTestSpecifications' in this.vitest) {
172+
return this.vitest.globTestSpecifications(filters)
173173
}
174-
return await (this.ctx as any).globTestFiles(filters)
174+
return await (this.vitest as any).globTestFiles(filters)
175175
}
176176

177177
private invalidateTree(mod: any, seen = new Set()) {
178178
if (seen.has(mod)) {
179179
return
180180
}
181181
seen.add(mod)
182-
this.ctx.server.moduleGraph.invalidateModule(mod)
182+
this.vitest.server.moduleGraph.invalidateModule(mod)
183183
mod.clientImportedModules.forEach((mod: any) => {
184184
this.invalidateTree(mod)
185185
})
@@ -189,7 +189,7 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
189189
}
190190

191191
private async runTestFiles(specs: SerializedTestSpecification[], testNamePattern?: string | undefined, runAllFiles = false) {
192-
await (this.ctx as any).runningPromise
192+
await (this.vitest as any).runningPromise
193193
this.watcher.markRerun(false)
194194

195195
this.setTestNamePattern(testNamePattern)
@@ -210,10 +210,10 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
210210

211211
const specsToRun = specs.flatMap((spec) => {
212212
const file = typeof spec === 'string' ? spec : spec[1]
213-
const fileSpecs = this.ctx.getModuleSpecifications
214-
? this.ctx.getModuleSpecifications(file)
213+
const fileSpecs = this.vitest.getModuleSpecifications
214+
? this.vitest.getModuleSpecifications(file)
215215
// supported by the older version
216-
: this.ctx.getProjectsByTestFile(file)
216+
: this.vitest.getProjectsByTestFile(file)
217217
if (!fileSpecs.length) {
218218
return []
219219
}
@@ -223,16 +223,16 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
223223
this.report('onWatcherRerun', paths),
224224
// `_onUserTestsRerun` exists only in Vitest 3 and it's private
225225
// the extension needs to migrate to the new API
226-
...((this.ctx as any)._onUserTestsRerun || []).map((fn: any) => fn(specs)),
226+
...((this.vitest as any)._onUserTestsRerun || []).map((fn: any) => fn(specs)),
227227
])
228228

229-
await (this.ctx as any).runFiles(specsToRun, runAllFiles)
229+
await (this.vitest as any).runFiles(specsToRun, runAllFiles)
230230

231-
await this.report('onWatcherStart', this.ctx.state.getFiles(paths))
231+
await this.report('onWatcherStart', this.vitest.state.getFiles(paths))
232232
}
233233

234234
private handleFileChanged(file: string): string[] {
235-
const ctx = this.ctx as any
235+
const ctx = this.vitest as any
236236
// support Vitest 3
237237
if (ctx.watcher) {
238238
return ctx.watcher.handleFileChanged(file) ? [file] : []
@@ -241,11 +241,11 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
241241
}
242242

243243
private scheduleRerun(files: string[]): Promise<void> {
244-
return (this.ctx as any).scheduleRerun(files)
244+
return (this.vitest as any).scheduleRerun(files)
245245
}
246246

247247
private updateLastChanged(filepath: string) {
248-
this.ctx.projects.forEach(({ server, browser }) => {
248+
this.vitest.projects.forEach(({ server, browser }) => {
249249
const serverMods = server.moduleGraph.getModulesByFile(filepath)
250250
serverMods?.forEach(mod => server.moduleGraph.invalidateModule(mod))
251251
if (browser) {
@@ -266,7 +266,7 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
266266
}
267267
}
268268
catch (err) {
269-
this.ctx.logger.error('Error during analyzing changed files', err)
269+
this.vitest.logger.error('Error during analyzing changed files', err)
270270
}
271271
}
272272

@@ -278,28 +278,28 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
278278
this.updateLastChanged(file)
279279
let content: string | null = null
280280
const projects = []
281-
for (const project of this.ctx.projects) {
281+
for (const project of this.vitest.projects) {
282282
if (this.isTestFile(
283283
project,
284284
file,
285285
() => content ?? (content = readFileSync(file, 'utf-8')),
286286
)) {
287287
testFiles.push(file)
288288
;(project as any).testFilesList?.push(file)
289-
this.ctx.changedTests.add(file)
289+
this.vitest.changedTests.add(file)
290290
projects.push(project)
291291
}
292292
}
293293
// to support Vitest 1.4.0
294-
if (projects.length && (this.ctx as any).projectsTestFiles) {
295-
(this.ctx as any).projectsTestFiles.set(file, new Set(projects))
294+
if (projects.length && (this.vitest as any).projectsTestFiles) {
295+
(this.vitest as any).projectsTestFiles.set(file, new Set(projects))
296296
}
297297
}
298298

299299
testFiles.forEach(file => this.scheduleRerun([file]))
300300
}
301301
catch (err) {
302-
this.ctx.logger.error('Error during analyzing created files', err)
302+
this.vitest.logger.error('Error during analyzing created files', err)
303303
}
304304
}
305305

@@ -338,11 +338,11 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
338338
return
339339
}
340340
if (!modules) {
341-
this.ctx.server.moduleGraph.invalidateAll()
341+
this.vitest.server.moduleGraph.invalidateAll()
342342
return
343343
}
344344
modules.forEach((moduleId) => {
345-
const mod = this.ctx.server.moduleGraph.getModuleById(moduleId)
345+
const mod = this.vitest.server.moduleGraph.getModuleById(moduleId)
346346
if (mod) {
347347
this.invalidateTree(mod)
348348
}
@@ -364,14 +364,14 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
364364
dispose() {
365365
this.coverage.disable()
366366
this.watcher.stopTracking()
367-
return this.ctx.close()
367+
return this.vitest.close()
368368
}
369369

370370
close() {
371371
return this.dispose()
372372
}
373373

374374
report<T extends keyof Reporter>(name: T, ...args: ArgumentsType<Reporter[T]>) {
375-
return (this.ctx as any).report(name, ...args)
375+
return (this.vitest as any).report(name, ...args)
376376
}
377377
}

0 commit comments

Comments
 (0)