Skip to content

Commit

Permalink
fix: throw an error if Node.js is too low (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 5, 2024
1 parent b1c3e33 commit 5209cc8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

- Visual Studio Code version >= 1.77.0
- Vitest version >= v1.4.0
- Node.js version >= 18.0.0 (follows Vitest)
- Coverage requires Visual Studio Code >= 1.88.0
- Debugger requires Vitest >= 1.5.0

## Usage

Expand Down
16 changes: 13 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { gte } from 'semver'
import { dirname, normalize, relative } from 'pathe'
import * as vscode from 'vscode'
import { log } from './log'
import { workerPath } from './constants'
import { minimumNodeVersion, workerPath } from './constants'
import { getConfig } from './config'
import type { VitestEvents, VitestRPC } from './api/rpc'
import { createVitestRpc } from './api/rpc'
import type { WorkerRunnerOptions } from './worker/types'
import type { VitestPackage } from './api/pkg'
import { findNode, showVitestError } from './utils'
import { findNode, getNodeJsVersion, showVitestError } from './utils'
import type { VitestProcess } from './process'

export class VitestReporter {
Expand Down Expand Up @@ -259,7 +259,17 @@ async function createChildVitestProcess(pkg: VitestPackage) {
]
: undefined
const env = getConfig().env || {}
const execPath = getConfig().nodeExecutable || await findNode(vscode.workspace.workspaceFile?.fsPath || vscode.workspace.workspaceFolders![0].uri.fsPath)
const execPath = await findNode(vscode.workspace.workspaceFile?.fsPath || pkg.cwd)
const execVersion = await getNodeJsVersion(execPath)
if (!execVersion) {
log.error('[API]', `Failed to get Node.js version from ${execPath}`)
throw new Error('Failed to get Node.js version')
}
if (!gte(execVersion, minimumNodeVersion)) {
const errorMsg = `Node.js version ${execVersion} is not supported. Minimum required version is ${minimumNodeVersion}`
log.error('[API]', errorMsg)
throw new Error(errorMsg)
}
log.info('[API]', `Running ${formapPkg(pkg)} with Node.js: ${execPath}`)
const vitest = fork(
workerPath,
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { resolve } from 'pathe'

export const minimumVersion = '1.4.0'
export const minimumDebugVersion = '1.5.0'
// follows minimum Vitest
export const minimumNodeVersion = '18.0.0'

export const distDir = __dirname
export const workerPath = resolve(__dirname, 'worker.js')
Expand Down
13 changes: 11 additions & 2 deletions src/testTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ export class TestTree extends vscode.Disposable {
rootItem.children.replace([this.loaderItem])
}
else {
const folderItems = workspaceFolders.map(x => this.getOrCreateWorkspaceFolderItem(x.uri))
this.controller.items.replace([this.loaderItem, ...folderItems])
const folderItems = workspaceFolders.map((x) => {
const item = this.getOrCreateWorkspaceFolderItem(x.uri)
item.children.replace([])
item.busy = true
return item
})
this.controller.items.replace(folderItems)
}
}

async discoverAllTestFiles(api: VitestFolderAPI, files: [project: string, file: string][]) {
const folderItem = this.folderItems.get(normalize(api.workspaceFolder.uri.fsPath))
if (folderItem)
folderItem.busy = false

for (const [project, file] of files)
this.getOrCreateFileTestItem(api, project, file)

Expand Down
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import which from 'which'
import { relative } from 'pathe'
import type { VitestPackage } from './api/pkg'
import { log } from './log'
import { getConfig } from './config'

export function noop() {}

Expand Down Expand Up @@ -91,6 +92,10 @@ let pathToNodeJS: string | undefined

// based on https://github.com/microsoft/playwright-vscode/blob/main/src/utils.ts#L144
export async function findNode(cwd: string): Promise<string> {
if (getConfig().nodeExecutable)
// if empty string, keep as undefined
pathToNodeJS = getConfig().nodeExecutable || undefined

if (pathToNodeJS)
return pathToNodeJS

Expand Down Expand Up @@ -174,3 +179,19 @@ async function findNodeViaShell(cwd: string): Promise<string | undefined> {
}
})
}

export function getNodeJsVersion(nodeJsPath: string) {
return new Promise<string>((resolve) => {
const childProcess = spawn(nodeJsPath, ['--version'], {
stdio: 'pipe',
})
let output = ''
childProcess.stdout.on('data', data => output += data.toString())
childProcess.on('error', () => resolve(''))
childProcess.on('exit', (exitCode) => {
if (exitCode !== 0)
return resolve('')
return resolve(output.trim())
})
})
}

0 comments on commit 5209cc8

Please sign in to comment.