Debugging 在Visual Studio代码调试器中使用Jest时如何配置源映射

Debugging 在Visual Studio代码调试器中使用Jest时如何配置源映射,debugging,react-native,babeljs,visual-studio-code,jestjs,Debugging,React Native,Babeljs,Visual Studio Code,Jestjs,我正在使用react native编写一个应用程序,我希望能够使用jest框架测试我的代码,并使用visual studio代码编辑器调试器设置断点。我目前面临的问题是,无论我如何运行调试器,无论是通过生成新实例还是附加实例,我似乎都无法从babel获得源映射。我在.babelrc文件中尝试了不同的配置,但似乎都不起作用 VScode版本-1.6.0(最新) 我的目录结构与此类似 -package.json -node_modules -.babelrc -dist -app -myModul

我正在使用react native编写一个应用程序,我希望能够使用jest框架测试我的代码,并使用visual studio代码编辑器调试器设置断点。我目前面临的问题是,无论我如何运行调试器,无论是通过生成新实例还是附加实例,我似乎都无法从babel获得源映射。我在.babelrc文件中尝试了不同的配置,但似乎都不起作用

VScode版本-1.6.0(最新)

我的目录结构与此类似

-package.json
-node_modules
-.babelrc
-dist
-app
 -myModule.js
 -test
   -myModule.spec.js
那么在我的.babelrc中,我有以下内容

{
    "presets" : ["react-native"],
    "sourceMaps" : true,
    "outDir" : "./dist"
}
我已尝试将
sourceMaps
属性同时设置为
true
inline
,但这两个属性都不能用于当前的
launch.json
配置

这是我的
launch.json
,用于运行Jest测试仪

{

            "name" : "Launch via jest",
            "type": "node",
            "request": "launch",
            "program" : "${workspaceRoot}/node_modules/jest/bin/jest.js",
            "cwd": "${workspaceRoot}",
            "args": [
                "--runInBand"
            ],
            "runtimeArgs": [
                "--harmony"
            ],
            "sourceMaps": true,
            "outDir" : "${workspaceRoot}/dist"
}
--harmony
--runInBand
都是使调试器正常工作所必需的,因为Jest将生成与端口冲突的子进程

我的package.json中还有其他Jest配置

"jest": {
    "preset": "jest-react-native"
  }
现在,每当我运行调试器时,它都会运行,并在babel输出的断点处停止,而不是在原始源代码处停止,而原始源代码没有太大帮助。我还应该提到,测试本身是由巴贝尔编译的,我不确定它是否重要


欢迎使用任何指针或不同的配置。

babelrc选项是
sourceMap
单数<代码>{“sourceMap”:“inline”}适合我。

您的jest配置是什么?在添加以下配置值之前,我一直看到与覆盖率相关的奇怪传输输出:


“testRegex”:“src/*\\.test\\.js$”

我能够在源文件中获得VS代码正确的源映射和断点位置,而不是通过以下方式传输到Jest+Babel中:

  • 确保Babel生成sourceMaps,这是通过在我的
    package.json
    Babel
    部分将sourceMap设置为内联(您可能需要在
    .babelrc.json
    中执行)

  • 在我的VS Codelaunch.json文件中,我将调试器从node替换为pwa node。这允许我指定
    resolveSourceMapLocations
    属性,并向VS代码指示源文件位于我的工作区目录中。我认为这是必要的,因为Jest在自己的缓存中构建文件

  • 我的完整
    午餐.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": [
            {
                "type": "pwa-node",
                "request": "launch",
                "name": "vscode-jest-tests",
                "program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
                "args": [
                    "--no-cache",
                    "--config",
                    "${workspaceRoot}/jest.config.json",
                    "--runInBand",
                    "--detectOpenHandles"
                ],
                "internalConsoleOptions": "neverOpen",
                "outFiles": [
                    "${workspaceRoot}/dist/**/*"
                ],
                "env": {
                    "NODE_ENV": "test"
                },
                "runtimeArgs": [
                    "--nolazy"
                ],
                "console": "integratedTerminal",
                "sourceMaps": true,
                "smartStep": true, 
                "skipFiles": [
                    "<node_internals>/**",
                    "node_modules/**"
                ],
                "resolveSourceMapLocations": [
                    "${workspaceFolder}/**",
                    "!**/node_modules/**"
                ],
            }
        ]
    }
    
    {
    //使用IntelliSense了解可能的属性。
    //悬停以查看现有属性的描述。
    //有关更多信息,请访问:https://go.microsoft.com/fwlink/?linkid=830387
    “版本”:“0.2.0”,
    “配置”:[
    {
    “类型”:“pwa节点”,
    “请求”:“启动”,
    “名称”:“vscode玩笑测试”,
    “程序”:“${workspaceRoot}/node_modules/jest/bin/jest.js”,
    “args”:[
    “--无缓存”,
    “--config”,
    “${workspaceRoot}/jest.config.json”,
    “--runInBand”,
    “--detectOpenHandles”
    ],
    “internalConsoleOptions”:“neverOpen”,
    “外部文件”:[
    “${workspaceRoot}/dist/***”
    ],
    “环境”:{
    “节点环境”:“测试”
    },
    “runtimeArgs”:[
    “诺拉齐”
    ],
    “控制台”:“集成终端”,
    “源地图”:正确,
    “smartStep”:没错,
    “滑雪板”:[
    "/**",
    “节点\u模块/**”
    ],
    “resolveSourceMapLocations”:[
    “${workspaceFolder}/**”,
    “!**/node\u模块/**”
    ],
    }
    ]
    }
    

    我使用的是VS代码1.53,Jest 25.5.4

    您同时找到解决方案了吗?对我也有效。对我有效谢谢!