在 VSCode 的调试终端中运行 npm test

问题描述 投票:0回答:2

我正在尝试在 launch.json 中创建一个配置,它将在

npm test
文件所在的文件夹中运行
.js
。 在终端中手动运行
npm test
效果很好,从我的
scripts
package.json
部分获取相关命令:

"scripts": {
    "start": "node --experimental-json-modules nodeserver.js",
    "test": "export MY_VAR=abc && node --experimental-json-modules nodeserver.js"
},

特别是,当直接在终端中运行

npm test
时,
test
脚本行中指定的环境变量生效,并且
--experimental-json-modules
标志将传递给
node

这是我的launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "command": "npm test",
            "name": "Run npm test",
            "request": "launch",
            "type": "node-terminal"
        }
    ]
}

这几乎是编辑器中建议的预定义选项之一的原样,并且与 this 非常相似。

但是当我在

nodeserver.js
文件上运行此配置时,我得到:

enter image description here

它似乎正在运行

node
,而没有我在配置中指定的标志? 我对这个
launch.json
计划的运作方式有什么误解?

编辑我玩得越多,配置似乎被完全忽略了,因此它使用默认的node.js配置...我从下拉列表中选择配置并按播放图标:

enter image description here

这应该有效吗?

除了在终端中运行

npm start
之外,使其工作的唯一“自动”方法是打开
package.json
并单击
scripts
标签出现的小调试按钮:

enter image description here

但我想弄清楚如何正确使用

launch.json
,以便我可以通过它传递环境变量等。

node.js debugging npm visual-studio-code
2个回答
0
投票

您可以尝试直接在 launch.json 中创建 npm 测试脚本,如上所述:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Run npm test",
        "request": "launch",
        "type": "node",
        "args": ["--experimental-json-modules", "${workspaceFolder}/nodeserver"],
        "env": {
           "MY_VAR": "abc"
        }

    }
]
}

0
投票

启动.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\\index.js"
        },
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Run Test",
            "skipFiles": 
            [
                "<node_internals>/**"
            ],

            // You can specify enviorment variables per config here
            // using key value pairs
            "env": 
            {
                "test_variable": "test value"
            },

            // You can also specify a .env file that contains them
            "envFile": "${workspaceFolder}/.env",

            // Here you specify the file you want launched
            "program": "${workspaceFolder}\\test.js",

            // add args to nodejs here
            "runtimeArgs": 
            [
                "--experimental-json-modules"
            ],
        }
    ]
}

供参考: https://code.visualstudio.com/docs/nodejs/nodejs-debugging

© www.soinside.com 2019 - 2024. All rights reserved.