在VSCode的调试终端中运行npm测试。

5

我正在尝试创建一个配置文件,使得launch.json能够在包含.js文件的文件夹中运行npm test。手动在终端中运行npm test可以正常工作,可以从package.json的scripts部分取出相关命令:

"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"
        }
    ]
}

这基本上是从编辑器建议的预定义选项之一中保持不变的内容,与这个非常相似。
但是,当我在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,以便可以通过它传递环境变量等。
2个回答

1

launch.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

该文档介绍了如何使用 Visual Studio Code 进行 Node.js 调试。在此过程中,您将学习如何设置和启动调试器、断点、观察变量和执行代码。此外,该文档还介绍了一些常见的调试场景和技巧,例如在 Docker 容器中进行调试和使用 Chrome DevTools 调试 Node.js 应用程序。


同样的情况也发生在这个 launch.json 文件上... 看起来配置(包括 --experimental-json-modules 参数)被完全忽略了,因此它使用默认的 node.js 启动配置,没有任何参数。也许我只是启动方式不正确... 你是怎么做的?我是按照原始帖子中的方法进行的。 - drmrbrewer
@drmrbrewer 我选择了该配置文件,然后按下了F5键。我刚刚测试了一下那个开关,它起作用了。https://i.imgur.com/CiRXX6X.png - John
是的。F5相当于我之前所做的...按下个人资料名称旁边的“播放”按钮...但我仍然遇到相同的错误,指定的运行时参数完全被忽略...不知道发生了什么。 - drmrbrewer

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"
        }

    }
]
}

这个 launch.json 文件也是一样的情况... 看起来配置被完全忽略了,所以它正在使用默认的 node.js 启动配置。 - drmrbrewer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接