Skip to content

Commit 565b7a7

Browse files
authored
feat: add the ability to define env variables for use when debugging (#697)
* feat: add the ability to define debug env variables * fix: missing updated settings.json for test
1 parent 0e5d263 commit 565b7a7

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ These options are resolved relative to the [workspace file](https://code.visuals
9999
- `vitest.vitestPackagePath`: The path to a `package.json` file of a Vitest executable (it's usually inside `node_modules`) in case the extension cannot find it. It will be used to resolve Vitest API paths. This should be used as a last resort fix.
100100
- `vitest.nodeEnv`: Environment passed to the runner process in addition to
101101
`process.env`
102+
- `vitest.debugNodeEnv`: Environment passed to the runner process in addition to `process.env` and `vitest.nodeEnv` when debugging tests
102103
- `vitest.debugExclude`: Excludes files matching specified glob patterns from debugging. Default:
103104
`["<node_internals>/**"]`
104105
- `vitest.debugOutFiles`: If source maps are enabled, these glob patterns specify the generated JavaScript files. If a pattern starts with `!` the files are excluded. If not specified, the generated code is expected in the same directory as its source. Default: `["${workspaceFolder}/**/*.(m|c|)js", "!**/node_modules/**"]`

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@
207207
"type": "array",
208208
"scope": "resource"
209209
},
210+
"vitest.debugNodeEnv": {
211+
"markdownDescription": "The env passed to runner process in addition to `process.env` and `vitest.nodeEnv` when debugging tests",
212+
"type": [
213+
"object",
214+
"null"
215+
],
216+
"default": null,
217+
"scope": "window"
218+
},
210219
"vitest.nodeEnv": {
211220
"markdownDescription": "The env passed to runner process in addition to `process.env`",
212221
"type": [

packages/extension/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function getConfig(workspaceFolder?: WorkspaceFolder) {
7676

7777
return {
7878
env: get<null | Record<string, string>>('nodeEnv', null),
79+
debugEnv: get<null | Record<string, string>>('debugNodeEnv', null),
7980
debugExclude: get<string[]>('debugExclude'),
8081
debugOutFiles,
8182
filesWatcherInclude,

packages/extension/src/debug.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export async function debugTests(
4343

4444
const { runtimeArgs, runtimeExecutable } = await getRuntimeOptions(pkg)
4545
const env = config.env || {}
46+
const debugEnv = config.debugEnv || {}
4647
const logLevel = config.logLevel
4748

4849
log.info('[DEBUG]', 'Starting debugging session', runtimeExecutable, ...(runtimeArgs || []))
@@ -84,6 +85,7 @@ export async function debugTests(
8485
env: {
8586
...process.env,
8687
...env,
88+
...debugEnv,
8789
VITEST_VSCODE_LOG: env.VITEST_VSCODE_LOG ?? process.env.VITEST_VSCODE_LOG ?? logLevel,
8890
VITEST_VSCODE: 'true',
8991
VITEST_WS_ADDRESS: wsAddress,

samples/basic-v4/.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
"vitest.experimentalStaticAstCollect": true,
66
"[typescript]": {
77
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
8+
},
9+
"vitest.debugNodeEnv": {
10+
"TEST_CUSTOM_ENV": "hello new",
11+
"TEST_CUSTOM_ENV_2": "world"
812
}
913
}

samples/basic-v4/test/env.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { test, expect } from "vitest";
1+
import { expect, test } from "vitest";
22

33
test('process.env', () => {
44
expect(process.env.TEST).toBe('true');
55
expect(process.env.VITEST).toBe('true');
66
expect(process.env.NODE_ENV).toBe('test');
77
expect(process.env.VITEST_VSCODE).toBe('true');
8-
expect(process.env.TEST_CUSTOM_ENV).toBe('hello');
8+
9+
if(process.env.TEST_CUSTOM_ENV_2 === undefined) {
10+
expect(process.env.TEST_CUSTOM_ENV).toBe('hello');
11+
}
12+
else {
13+
expect(process.env.TEST_CUSTOM_ENV).toBe('hello new');
14+
expect(process.env.TEST_CUSTOM_ENV_2).toBe('world');
15+
}
916
});

0 commit comments

Comments
 (0)