简体   繁体   English

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

[英]Running npm test in a debug terminal in VSCode

I'm trying to create a configuration in my launch.json which will run npm test in the folder in which the .js file resides.我正在尝试在我的 launch.json 中创建一个配置,它将在.js文件所在的文件夹中运行npm test Running npm test manually in a terminal works fine, taking the relevant command from the scripts part of my package.json :在终端中手动运行npm test工作正常,从我的package.jsonscripts部分获取相关命令:

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

In particular, when running npm test directly in a terminal, the env var specified in the test script line takes effect and the --experimental-json-modules flag is passed to node .特别是,当直接在终端中运行npm test时, test脚本行中指定的 env var 生效并将--experimental-json-modules标志传递给node

This is my launch.json:这是我的发射。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 is pretty much as-is from one of the predefined options suggested in the editor, and is very similar to this .这与编辑器中建议的预定义选项之一非常相似,并且与this非常相似。

But when I run this configuration on the nodeserver.js file, I get:但是当我在nodeserver.js文件上运行此配置时,我得到:

在此处输入图像描述

It seems to be running node without the flag I specified in the configuration?似乎正在运行的node没有我在配置中指定的标志? What am I misunderstanding about how this launch.json scheme works?我对这个launch.json方案的工作原理有什么误解?

EDIT the more I've played around, the more it seems as if the configuration is just being completely ignored, so that it is using the default node.js configuration... I'm selecting the config from the drop-down and pressing the play icon:编辑我玩得越多,似乎配置就被完全忽略了,因此它使用默认的 node.js 配置...我从下拉列表中选择配置并按下播放图标:

在此处输入图像描述

Should that work?那应该工作吗?

Apart from running npm start in a terminal, the only "automatic" way of getting this to work is by opening the package.json and clicking on the little Debug button which appears by the scripts tag:除了在终端中运行npm start之外,唯一的“自动”方法是打开package.json并单击scripts出现的小调试按钮:

在此处输入图像描述

But I'd like to figure out how to use launch.json properly so that I can pass environments variables etc via that instead.但我想弄清楚如何正确使用launch.json ,以便我可以通过它传递环境变量等。

You can try to create the npm test script directly in your launch.json as above:您可以尝试直接在您的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发射.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"
            ],
        }
    ]
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM