Skip to content

Commit

Permalink
Merge branch 'main' into feature/nx-example
Browse files Browse the repository at this point in the history
  • Loading branch information
ffMathy authored Feb 18, 2024
2 parents 22dce79 + 1a95dcd commit 97ce6ce
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 22 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Build

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- run: yarn install --frozen-lockfile

- run: yarn run compile

- run: yarn test
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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.

### [0.2.44](https://github.com/vitest-dev/vscode/compare/v0.2.43...v0.2.44) (2024-02-18)


### Bug Fixes

* exclude/include configs and missing workspacePath ([#192](https://github.com/vitest-dev/vscode/issues/192)) ([bee4f6e](https://github.com/vitest-dev/vscode/commit/bee4f6e9bce9a9761a627f89379d2aed20735d11))

### [0.2.43](https://github.com/vitest-dev/vscode/compare/v0.2.42...v0.2.43) (2023-10-18)

### [0.2.42](https://github.com/vitest-dev/vscode/compare/v0.2.41...v0.2.42) (2023-07-01)
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vitest-explorer",
"displayName": "Vitest",
"version": "0.2.43",
"version": "0.2.44",
"description": "Run and debug Vitest test cases",
"icon": "img/icon.png",
"preview": true,
Expand Down Expand Up @@ -89,6 +89,12 @@
"scope": "resource",
"default": false
},
"vitest.watchOnStartup": {
"description": "Start watching tests on startup",
"type": "boolean",
"scope": "resource",
"default": false
},
"vitest.commandLine": {
"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",
"type": "string",
Expand Down
25 changes: 25 additions & 0 deletions samples/basic/test/console.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, it } from "vitest";

describe("console", () => {
it("basic", () => {
console.log([
"string",
{ hello: "world" },
1234,
/regex/g,
true,
false,
null,
]);
});

it("async", async () => {
console.log("1st");
await sleep(200);
console.log("2nd");
await sleep(200);
console.log("3rd");
});
});

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
7 changes: 4 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as vscode from 'vscode'
import semver from 'semver'
import type { WorkspaceConfiguration, WorkspaceFolder } from 'vscode'
import type { ResolvedConfig } from 'vitest'
import type { WorkspaceConfiguration, WorkspaceFolder } from 'vscode'
import * as vscode from 'vscode'
import { log } from './log'
import { isDefinitelyVitestEnv, mayBeVitestEnv } from './pure/isVitestEnv'
import { getVitestCommand, getVitestVersion, isNodeAvailable } from './pure/utils'
import { log } from './log'
export const extensionId = 'zxch3n.vitest-explorer'

// Copied from https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/defaults.ts
Expand Down Expand Up @@ -42,6 +42,7 @@ export function getConfig(workspaceFolder?: WorkspaceFolder | vscode.Uri | strin
return {
env: get<null | Record<string, string>>('nodeEnv', null),
commandLine: get<string | undefined>('commandLine', undefined),
watchOnStartup: get<boolean>('watchOnStartup', false),
include: get<string[]>('include'),
exclude: get<string[]>('exclude'),
enable: get<boolean>('enable', false),
Expand Down
37 changes: 32 additions & 5 deletions src/discover.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import path, { sep } from 'path'
import * as vscode from 'vscode'
import minimatch from 'minimatch'
import path, { sep } from 'path'
import type { ResolvedConfig } from 'vitest'
import parse from './pure/parsers'
import type { NamedBlock } from './pure/parsers/parser_nodes'
import * as vscode from 'vscode'
import type { TestData } from './TestData'
import {
TestCase,
Expand All @@ -12,10 +10,13 @@ import {
WEAKMAP_TEST_DATA,
testItemIdMap,
} from './TestData'
import parse from './pure/parsers'
import type { NamedBlock } from './pure/parsers/parser_nodes'
import { shouldIncludeFile } from './vscodeUtils'

import { getCombinedConfig, vitestEnvironmentFolders } from './config'
import { log } from './log'
import { openTestTag } from './tags'

export class TestFileDiscoverer extends vscode.Disposable {
private lastWatches = [] as vscode.FileSystemWatcher[]
Expand Down Expand Up @@ -139,6 +140,8 @@ export class TestFileDiscoverer extends vscode.Disposable {

const { file, data } = this.getOrCreateFile(ctrl, e.uri)
discoverTestFromFileContent(ctrl, e.getText(), file, data)

return file
}

discoverTestFromPath(
Expand Down Expand Up @@ -257,7 +260,7 @@ export function discoverTestFromFileContent(
result = parse(fileItem.id, content)
}
catch (e) {
log.error('parse error')
log.error('parse error', e)
return
}

Expand Down Expand Up @@ -320,4 +323,28 @@ export function discoverTestFromFileContent(
]
}
}

const childTestItems = [fileItem]
const allTestItems = new Array<vscode.TestItem>()

while (childTestItems.length) {
const child = childTestItems.pop()
if (!child)
continue

allTestItems.push(child)
childTestItems.push(...[...child.children].map(x => x[1]))
}

const isFileOpen = vscode.workspace.textDocuments.some(
x => x.uri.fsPath === fileItem.uri?.fsPath,
)
const existingTagsWithoutOpenTag = fileItem.tags.filter(
x => x !== openTestTag,
)
const newTags = isFileOpen
? [...existingTagsWithoutOpenTag, openTestTag]
: existingTagsWithoutOpenTag
for (const testItem of allTestItems)
testItem.tags = newTags
}
25 changes: 19 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ import * as vscode from 'vscode'
import { effect } from '@vue/reactivity'

import type { ResolvedConfig } from 'vitest'
import { StatusBarItem } from './StatusBarItem'
import { TestFile, WEAKMAP_TEST_DATA } from './TestData'
import { Command } from './command'
import {
detectVitestEnvironmentFolders, extensionId, getVitestWorkspaceConfigs,
detectVitestEnvironmentFolders, extensionId, getConfig, getVitestWorkspaceConfigs,
vitestEnvironmentFolders,
} from './config'
import { TestFileDiscoverer } from './discover'
import { log } from './log'
import {
debugHandler, gatherTestItemsFromWorkspace, runHandler, updateSnapshot,
} from './runHandler'
import { StatusBarItem } from './StatusBarItem'
import { TestFile, WEAKMAP_TEST_DATA } from './TestData'
import { TestWatcher } from './watch'

import type { VitestWorkspaceConfig } from './config'
import { fetchVitestConfig } from './pure/watch/vitestConfig'
import { openTestTag } from './tags'

export async function activate(context: vscode.ExtensionContext) {
await detectVitestEnvironmentFolders()
Expand Down Expand Up @@ -67,6 +68,11 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.workspace.onDidOpenTextDocument((e) => {
fileDiscoverer.discoverTestFromDoc(ctrl, e)
}),
vscode.workspace.onDidCloseTextDocument((e) => {
const item = fileDiscoverer.discoverTestFromDoc(ctrl, e)
if (item)
item.tags = item.tags.filter(x => x !== openTestTag)
}),
vscode.workspace.onDidChangeTextDocument(e =>
fileDiscoverer.discoverTestFromDoc(ctrl, e.document),
),
Expand Down Expand Up @@ -116,6 +122,8 @@ function registerDiscovery(ctrl: vscode.TestController, context: vscode.Extensio
fileDiscoverer.discoverTestFromDoc(ctrl, x.document),
)

fileDiscoverer.discoverAllTestFilesInWorkspace(ctrl)

return fileDiscoverer
}

Expand All @@ -140,9 +148,14 @@ function registerWatchHandlers(
fileDiscoverer: TestFileDiscoverer,
context: vscode.ExtensionContext,
) {
const testWatchers = vitestConfigs.map((vitestConfig, index) =>
TestWatcher.create(ctrl, fileDiscoverer, vitestConfig, vitestConfig.workspace, index),
) ?? []
const testWatchers = vitestConfigs.map((vitestConfig, index) => {
const watcher = TestWatcher.create(ctrl, fileDiscoverer, vitestConfig, vitestConfig.workspace, index)

if (getConfig(vitestConfig.workspace).watchOnStartup)
watcher.watch()

return watcher
}) ?? []

statusBarItem = new StatusBarItem()
effect(() => {
Expand Down
8 changes: 1 addition & 7 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,7 @@ async function runTest(
? WEAKMAP_TEST_DATA.get(items[0])!.getFullPattern()
: '',
{
info: (msg: string) => {
if (items.length === 1)
run.appendOutput(msg, undefined, items[0])
else
run.appendOutput(msg)
},
info: log.info,
error: log.error,
},
config.env || undefined,
Expand Down Expand Up @@ -339,4 +334,3 @@ async function runTest(
}
}
}

3 changes: 3 additions & 0 deletions src/tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { TestTag } from 'vscode'

export const openTestTag = new TestTag('open')
9 changes: 9 additions & 0 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ export function syncTestStatusToVsCode(
for (const task of vitest) {
const data = matchTask(task, set)
if (task.type === 'test' || task.type === 'custom') {
// for now, display logs after all tests are finished.
// TODO: append logs during test execution using `onUserConsoleLog` rpc.
if (finished) {
for (const log of task.logs ?? []) {
// LF to CRLF https://code.visualstudio.com/api/extension-guides/testing#test-output
const output = log.content.replace(/(?<!\r)\n/g, "\r\n");
run.appendOutput(output, undefined, data.item);
}
}
if (task.result == null) {
if (finished) {
finishedTest && finishedTest.add(data.item)
Expand Down

0 comments on commit 97ce6ce

Please sign in to comment.