Skip to content

Commit

Permalink
feat: use Vitest public API to work with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 19, 2024
1 parent f3c3678 commit 989b940
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@
"scripts": {
"vscode:prepublish": "pnpm compile",
"release": "bumpp package.json --commit --push --tag && git update-ref refs/heads/release refs/heads/main && git push origin release",
"compile": "tsup ./src/extension.ts --external vscode --minify",
"watch": "tsup ./src/extension.ts --external vscode --watch --sourcemap",
"compile": "tsup",
"package": "vsce package --no-dependencies",
"watch": "tsup --watch --sourcemap",
"test": "vitest run --config vite.global-config.ts",
"typecheck": "tsc --noEmit",
"lint": "eslint --cache .",
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { resolve } from 'pathe'

export const distDir = resolve(__filename)
export const workerPath = resolve(__dirname, 'worker.mjs')
26 changes: 26 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Worker } from 'node:worker_threads'
import { dirname, resolve } from 'node:path'
import * as vscode from 'vscode'

import { effect } from '@vue/reactivity'
Expand Down Expand Up @@ -26,8 +28,32 @@ import { TestWatcher } from './watch'
import type { VitestWorkspaceConfig } from './config'
import { fetchVitestConfig } from './pure/watch/vitestConfig'
import { openTestTag } from './tags'
import { workerPath } from './constants'

export async function activate(context: vscode.ExtensionContext) {
// TODO: just to test, improve the logic!
const workspacePath = vscode.workspace.workspaceFolders?.[0].uri.fsPath
log.info('workspace path', workspacePath, workerPath)
if (workspacePath) {
const vitestPath = require.resolve('vitest', { paths: [workspacePath] }) // resolves to cjs
log.info('vitest path', vitestPath, resolve(dirname(vitestPath), './dist/node.js'))
const worker = new Worker(workerPath, {
workerData: {
root: workspacePath,
vitestPath: resolve(dirname(vitestPath), './dist/node.js'),
},
})
worker.stdout.on('data', (d) => {
log.info('worker error', d.toString())
})
worker.stderr.on('data', (d) => {
log.error('worker error', d.toString())
})
worker.on('message', (d) => {
log.info(JSON.stringify(d))
// worker.terminate()
})
}
await detectVitestEnvironmentFolders()
if (vitestEnvironmentFolders.length === 0) {
log.info('The extension is not activated because no Vitest environment was detected.')
Expand Down
21 changes: 21 additions & 0 deletions src/worker/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { parentPort, workerData } from 'node:worker_threads'

(async () => {
parentPort!.postMessage({ msg: 'hello from worker', p: workerData.vitestPath })
try {
const vitestMode = await import(workerData.vitestPath) as typeof import('vitest/node')
const vitest = await vitestMode.createVitest('test', {
watch: false,
root: workerData.root,
})
parentPort!.postMessage({ msg: 'vitest created' })
const files = await vitest.globTestFiles()
parentPort!.postMessage({
files: files.map(([_, spec]) => spec),
})
await vitest.close()
}
catch (err) {
parentPort!.postMessage({ error: err.message, stack: String(err.stack) })

Check failure on line 19 in src/worker/worker.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'err' is of type 'unknown'.

Check failure on line 19 in src/worker/worker.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'err' is of type 'unknown'.
}
})()
16 changes: 16 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'tsup'

export default defineConfig([
{
entry: ['./src/extension.ts'],
minify: true,
external: ['vscode'],
format: 'cjs',
},
{
entry: ['./src/worker/worker.ts'],
minify: true,
external: ['vitest/node'],
format: 'esm',
},
])

0 comments on commit 989b940

Please sign in to comment.