Skip to content

Commit 72031a3

Browse files
authored
Merge branch 'main' into feature/resource-management
2 parents be0ee0f + 1a95dcd commit 72031a3

File tree

11 files changed

+145
-30
lines changed

11 files changed

+145
-30
lines changed

.github/workflows/build.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [20.x]
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
cache: 'npm'
25+
26+
- run: yarn install --frozen-lockfile
27+
28+
- run: yarn run compile
29+
30+
- run: yarn test

.vscode/launch.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"--extensionDevelopmentPath=${workspaceFolder}",
1414
"${workspaceFolder}/samples/basic"
1515
],
16-
"outFiles": ["${workspaceFolder}/out/**/*.js"]
16+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
1717
},
1818
{
1919
"name": "Run Extension Multi-Root Workspace Sample",
@@ -23,7 +23,7 @@
2323
"--extensionDevelopmentPath=${workspaceFolder}",
2424
"${workspaceFolder}/samples/multi-root-workspace/sample.code-workspace"
2525
],
26-
"outFiles": ["${workspaceFolder}/out/**/*.js"]
26+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
2727
},
2828
{
2929
"name": "Run Extension Monorepo Sample",
@@ -33,7 +33,7 @@
3333
"--extensionDevelopmentPath=${workspaceFolder}",
3434
"${workspaceFolder}/samples/monorepo"
3535
],
36-
"outFiles": ["${workspaceFolder}/out/**/*.js"]
36+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
3737
},
3838
{
3939
"name": "Run Extension Monorepo Vitest Workspace",
@@ -43,7 +43,7 @@
4343
"--extensionDevelopmentPath=${workspaceFolder}",
4444
"${workspaceFolder}/samples/monorepo-vitest-workspace"
4545
],
46-
"outFiles": ["${workspaceFolder}/out/**/*.js"]
46+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
4747
},
4848
{
4949
"name": "Run Extension React Sample",
@@ -53,7 +53,7 @@
5353
"--extensionDevelopmentPath=${workspaceFolder}",
5454
"${workspaceFolder}/samples/monorepo/packages/react"
5555
],
56-
"outFiles": ["${workspaceFolder}/out/**/*.js"]
56+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
5757
},
5858
{
5959
"name": "Run Extension Vue Sample",
@@ -63,17 +63,17 @@
6363
"--extensionDevelopmentPath=${workspaceFolder}",
6464
"${workspaceFolder}/samples/vue"
6565
],
66-
"outFiles": ["${workspaceFolder}/out/**/*.js"]
66+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
6767
},
6868
{
6969
"name": "Extension Tests",
7070
"type": "extensionHost",
7171
"request": "launch",
7272
"args": [
7373
"--extensionDevelopmentPath=${workspaceFolder}",
74-
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
74+
"--extensionTestsPath=${workspaceFolder}/dist/test/suite/index"
7575
],
76-
"outFiles": ["${workspaceFolder}/out/test/**/*.js"]
76+
"outFiles": ["${workspaceFolder}/dist/test/**/*.js"]
7777
}
7878
]
7979
}

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [0.2.44](https://github.com/vitest-dev/vscode/compare/v0.2.43...v0.2.44) (2024-02-18)
6+
7+
8+
### Bug Fixes
9+
10+
* exclude/include configs and missing workspacePath ([#192](https://github.com/vitest-dev/vscode/issues/192)) ([bee4f6e](https://github.com/vitest-dev/vscode/commit/bee4f6e9bce9a9761a627f89379d2aed20735d11))
11+
512
### [0.2.43](https://github.com/vitest-dev/vscode/compare/v0.2.42...v0.2.43) (2023-10-18)
613

714
### [0.2.42](https://github.com/vitest-dev/vscode/compare/v0.2.41...v0.2.42) (2023-07-01)

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vitest-explorer",
33
"displayName": "Vitest",
4-
"version": "0.2.43",
4+
"version": "0.2.44",
55
"description": "Run and debug Vitest test cases",
66
"icon": "img/icon.png",
77
"preview": true,
@@ -89,6 +89,12 @@
8989
"scope": "resource",
9090
"default": false
9191
},
92+
"vitest.watchOnStartup": {
93+
"description": "Start watching tests on startup",
94+
"type": "boolean",
95+
"scope": "resource",
96+
"default": false
97+
},
9298
"vitest.commandLine": {
9399
"markdownDescription": "The command line to start vitest tests. **It should have with the ability to append extra arguments**. For example `npx vitest` or `yarn test --`\n\nThis is a workspace setting. Do not change it in the user setting directly, which will affect all the projects you open",
94100
"type": "string",

samples/basic/test/console.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, it } from "vitest";
2+
3+
describe("console", () => {
4+
it("basic", () => {
5+
console.log([
6+
"string",
7+
{ hello: "world" },
8+
1234,
9+
/regex/g,
10+
true,
11+
false,
12+
null,
13+
]);
14+
});
15+
16+
it("async", async () => {
17+
console.log("1st");
18+
await sleep(200);
19+
console.log("2nd");
20+
await sleep(200);
21+
console.log("3rd");
22+
});
23+
});
24+
25+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

src/config.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as vscode from 'vscode'
21
import semver from 'semver'
3-
import type { WorkspaceConfiguration, WorkspaceFolder } from 'vscode'
42
import type { ResolvedConfig } from 'vitest'
3+
import type { WorkspaceConfiguration, WorkspaceFolder } from 'vscode'
4+
import * as vscode from 'vscode'
5+
import { log } from './log'
56
import { isDefinitelyVitestEnv, mayBeVitestEnv } from './pure/isVitestEnv'
67
import { getVitestCommand, getVitestVersion, isNodeAvailable } from './pure/utils'
7-
import { log } from './log'
88
export const extensionId = 'zxch3n.vitest-explorer'
99

1010
// Copied from https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/defaults.ts
@@ -42,6 +42,7 @@ export function getConfig(workspaceFolder?: WorkspaceFolder | vscode.Uri | strin
4242
return {
4343
env: get<null | Record<string, string>>('nodeEnv', null),
4444
commandLine: get<string | undefined>('commandLine', undefined),
45+
watchOnStartup: get<boolean>('watchOnStartup', false),
4546
include: get<string[]>('include'),
4647
exclude: get<string[]>('exclude'),
4748
enable: get<boolean>('enable', false),

src/discover.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import path, { sep } from 'path'
2-
import * as vscode from 'vscode'
31
import minimatch from 'minimatch'
2+
import path, { sep } from 'path'
43
import type { ResolvedConfig } from 'vitest'
5-
import parse from './pure/parsers'
6-
import type { NamedBlock } from './pure/parsers/parser_nodes'
4+
import * as vscode from 'vscode'
75
import type { TestData } from './TestData'
86
import {
97
TestCase,
@@ -12,10 +10,13 @@ import {
1210
WEAKMAP_TEST_DATA,
1311
testItemIdMap,
1412
} from './TestData'
13+
import parse from './pure/parsers'
14+
import type { NamedBlock } from './pure/parsers/parser_nodes'
1515
import { shouldIncludeFile } from './vscodeUtils'
1616

1717
import { getCombinedConfig, vitestEnvironmentFolders } from './config'
1818
import { log } from './log'
19+
import { openTestTag } from './tags'
1920

2021
export class TestFileDiscoverer extends vscode.Disposable {
2122
private lastWatches = [] as vscode.FileSystemWatcher[]
@@ -139,6 +140,8 @@ export class TestFileDiscoverer extends vscode.Disposable {
139140

140141
const { file, data } = this.getOrCreateFile(ctrl, e.uri)
141142
discoverTestFromFileContent(ctrl, e.getText(), file, data)
143+
144+
return file
142145
}
143146

144147
discoverTestFromPath(
@@ -257,7 +260,7 @@ export function discoverTestFromFileContent(
257260
result = parse(fileItem.id, content)
258261
}
259262
catch (e) {
260-
log.error('parse error')
263+
log.error('parse error', e)
261264
return
262265
}
263266

@@ -320,4 +323,28 @@ export function discoverTestFromFileContent(
320323
]
321324
}
322325
}
326+
327+
const childTestItems = [fileItem]
328+
const allTestItems = new Array<vscode.TestItem>()
329+
330+
while (childTestItems.length) {
331+
const child = childTestItems.pop()
332+
if (!child)
333+
continue
334+
335+
allTestItems.push(child)
336+
childTestItems.push(...[...child.children].map(x => x[1]))
337+
}
338+
339+
const isFileOpen = vscode.workspace.textDocuments.some(
340+
x => x.uri.fsPath === fileItem.uri?.fsPath,
341+
)
342+
const existingTagsWithoutOpenTag = fileItem.tags.filter(
343+
x => x !== openTestTag,
344+
)
345+
const newTags = isFileOpen
346+
? [...existingTagsWithoutOpenTag, openTestTag]
347+
: existingTagsWithoutOpenTag
348+
for (const testItem of allTestItems)
349+
testItem.tags = newTags
323350
}

src/extension.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ import * as vscode from 'vscode'
33
import { effect } from '@vue/reactivity'
44

55
import type { ResolvedConfig } from 'vitest'
6+
import { StatusBarItem } from './StatusBarItem'
7+
import { TestFile, WEAKMAP_TEST_DATA } from './TestData'
68
import { Command } from './command'
79
import {
8-
detectVitestEnvironmentFolders, extensionId, getVitestWorkspaceConfigs,
10+
detectVitestEnvironmentFolders, extensionId, getConfig, getVitestWorkspaceConfigs,
911
vitestEnvironmentFolders,
1012
} from './config'
1113
import { TestFileDiscoverer } from './discover'
1214
import { log } from './log'
1315
import {
1416
debugHandler, gatherTestItemsFromWorkspace, runHandler, updateSnapshot,
1517
} from './runHandler'
16-
import { StatusBarItem } from './StatusBarItem'
17-
import { TestFile, WEAKMAP_TEST_DATA } from './TestData'
1818
import { TestWatcher } from './watch'
1919

2020
import type { VitestWorkspaceConfig } from './config'
2121
import { fetchVitestConfig } from './pure/watch/vitestConfig'
22+
import { openTestTag } from './tags'
2223

2324
export async function activate(context: vscode.ExtensionContext) {
2425
await detectVitestEnvironmentFolders()
@@ -67,6 +68,11 @@ export async function activate(context: vscode.ExtensionContext) {
6768
vscode.workspace.onDidOpenTextDocument((e) => {
6869
fileDiscoverer.discoverTestFromDoc(ctrl, e)
6970
}),
71+
vscode.workspace.onDidCloseTextDocument((e) => {
72+
const item = fileDiscoverer.discoverTestFromDoc(ctrl, e)
73+
if (item)
74+
item.tags = item.tags.filter(x => x !== openTestTag)
75+
}),
7076
vscode.workspace.onDidChangeTextDocument(e =>
7177
fileDiscoverer.discoverTestFromDoc(ctrl, e.document),
7278
),
@@ -116,6 +122,8 @@ function registerDiscovery(ctrl: vscode.TestController, context: vscode.Extensio
116122
fileDiscoverer.discoverTestFromDoc(ctrl, x.document),
117123
)
118124

125+
fileDiscoverer.discoverAllTestFilesInWorkspace(ctrl)
126+
119127
return fileDiscoverer
120128
}
121129

@@ -140,9 +148,14 @@ function registerWatchHandlers(
140148
fileDiscoverer: TestFileDiscoverer,
141149
context: vscode.ExtensionContext,
142150
) {
143-
const testWatchers = vitestConfigs.map((vitestConfig, index) =>
144-
TestWatcher.create(ctrl, fileDiscoverer, vitestConfig, vitestConfig.workspace, index),
145-
) ?? []
151+
const testWatchers = vitestConfigs.map((vitestConfig, index) => {
152+
const watcher = TestWatcher.create(ctrl, fileDiscoverer, vitestConfig, vitestConfig.workspace, index)
153+
154+
if (getConfig(vitestConfig.workspace).watchOnStartup)
155+
watcher.watch()
156+
157+
return watcher
158+
}) ?? []
146159

147160
statusBarItem = new StatusBarItem()
148161
effect(() => {

src/runHandler.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,7 @@ async function runTest(
306306
? WEAKMAP_TEST_DATA.get(items[0])!.getFullPattern()
307307
: '',
308308
{
309-
info: (msg: string) => {
310-
if (items.length === 1)
311-
run.appendOutput(msg, undefined, items[0])
312-
else
313-
run.appendOutput(msg)
314-
},
309+
info: log.info,
315310
error: log.error,
316311
},
317312
config.env || undefined,
@@ -339,4 +334,3 @@ async function runTest(
339334
}
340335
}
341336
}
342-

src/tags.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { TestTag } from 'vscode'
2+
3+
export const openTestTag = new TestTag('open')

src/watch.ts

+9
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ export function syncTestStatusToVsCode(
356356
for (const task of vitest) {
357357
const data = matchTask(task, set)
358358
if (task.type === 'test' || task.type === 'custom') {
359+
// for now, display logs after all tests are finished.
360+
// TODO: append logs during test execution using `onUserConsoleLog` rpc.
361+
if (finished) {
362+
for (const log of task.logs ?? []) {
363+
// LF to CRLF https://code.visualstudio.com/api/extension-guides/testing#test-output
364+
const output = log.content.replace(/(?<!\r)\n/g, "\r\n");
365+
run.appendOutput(output, undefined, data.item);
366+
}
367+
}
359368
if (task.result == null) {
360369
if (finished) {
361370
finishedTest && finishedTest.add(data.item)

0 commit comments

Comments
 (0)