【问题标题】:Debug Typescript protractor tests with VSCode使用 VSCode 调试 Typescript 量角器测试
【发布时间】:2017-04-28 14:55:36
【问题描述】:

我在 typescript 中有带有 e2e 测试的 Angular 应用程序,我想在 VSCode 中运行调试。我去了read.me 看看如何运行调试,这很容易。但我的问题是打字稿测试中的断点并没有停止。 如我所见,我有未生成的源映射问题。

tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/node_modules/protractor/bin/protractor",
            "stopOnEntry": false,
            "sourceMaps": true,
            "cwd": "${workspaceRoot}",
            "args": [
                "${workspaceRoot}/protractor.conf.js"
            ]
        }
    ]
}

量角器.conf.js

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/docs/referenceConf.js

/*global jasmine */
var SpecReporter = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  useAllAngular2AppRoots: true,
  beforeLaunch: function() {
    require('ts-node').register({
      project: 'e2e'
    });
  },
  onPrepare: function() {
    jasmine.getEnv().addReporter(new SpecReporter());
  }
};

据我了解,ts-node 正在将 ts 编译为 js,并且可能不会生成源映射,或者它们存储在某个特定位置

我做错了什么?

【问题讨论】:

  • 我认为量角器会覆盖 ts-node 安装的 source-map-support。尚不确定解决方案是什么。
  • @CletusW 谢谢我会试试的

标签: angular typescript protractor visual-studio-code


【解决方案1】:

看起来 Protractor 本身就是 calling into source-map-support,它覆盖了 ts-node 发出的调用。

尝试在 protractor.conf.js 中启用skipSourceMapSupport option

【讨论】:

    【解决方案2】:

    这是一篇旧帖子,但希望这可以帮助其他偶然发现此问题的人。正如您所提到的,使用 ts-node,不会像使用 tsc 编译时那样将任何文件写入工作空间中的磁盘。断点命中的方法是在运行量角器时注册 ts-node。

    试试这个(注意添加到args 属性)。

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Launch",
                "type": "node",
                "request": "launch",
                "program": "${workspaceRoot}/node_modules/protractor/bin/protractor",
                "args": [
                    "${workspaceRoot}/protractor.conf.js",
                    "--require", "ts-node/register"
                ]
                "stopOnEntry": false,
                "sourceMaps": true,
                "cwd": "${workspaceRoot}",
            }
        ]
    }
    

    在我的 launch.json 中,我还包括以下内容,以确保我不会进入不属于我的代码(如 vscode 文档中的 skipping uninteresting code 中所述)。

    "skipFiles": [
       "<node_internals>/**",
       "${workspaceRoot}/node_modules/**",
    ]
    

    但是,我似乎对此有问题。它破坏了我的代码并跳过了 node_modules 文件夹,但我仍然看到它破坏了一些 node_internals 文件。我还没有找出原因,但至少我可以逐步执行代码。

    【讨论】:

      【解决方案3】:

      在我的情况下,将以下内容添加到 laumch.json 有效:

            "type": "pwa-node",
            ...
            "sourceMaps": true,
            "resolveSourceMapLocations": [
              "${workspaceFolder}/**",
              "!**/node_modules/**"
            ],
      

      skipSourceMapSuppor: true 不起作用。)

      编辑:这仅适用于类型pwa-node,不适用于node。这针对新的调试器。详情见这个答案:https://stackoverflow.com/a/63662561/407758

      【讨论】:

      • 仅供参考,当您尝试添加此内容时,vscode 会抛出警告“rseolveSourceMapLocations”不允许。
      • @TylerNielsen 我已经澄清它仅适用于 pwa-node 类型。 node 类型不支持它,这就是你收到警告的原因。
      猜你喜欢
      • 2021-04-09
      • 2019-08-28
      • 2017-02-11
      • 1970-01-01
      • 2015-02-26
      • 2020-11-08
      • 2017-05-29
      • 1970-01-01
      • 2018-01-21
      相关资源
      最近更新 更多