角度迁移(从4到6)e2e --proxy-config无法正常工作

Mar*_*oLe 10 protractor e2e-testing angular angular-e2e angular6

我一直在将我的应用程序从4迁移到6,我无法为我的e2e测试执行我的代理脚本.脚本列表如下所示:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "start:tst1": "ng serve --proxy-config config/proxy/proxy.tst1.json",
    "start:tst5": "ng serve --proxy-config config/proxy/proxy.tst5.json",
    ...
    "test:watch": "ng test",
    "lint": "ng lint --type-check true",
    "e2e": "ng e2e",
    "e2e:tst1": "ng e2e --proxy-config config/proxy/proxy.tst1.json",
    "e2e:tst5": "ng e2e --proxy-config config/proxy/proxy.tst5.json",
  },
Run Code Online (Sandbox Code Playgroud)

我不明白的是,启动命令(ng serve)工作得非常好npm run start:tst5.但是,当我尝试执行e2e测试时,npm run e2e:tst5它会抛出错误:Unknown option: '--proxyConfig'.

我的angular.json中的配置如下所示:

angular.json

...
"lmsbo-bo-e2e": {
  "root": "e2e",
  "sourceRoot": "e2e",
  "projectType": "application",
  "architect": {
    "e2e": {
      "builder": "@angular-devkit/build-angular:protractor",
      "options": {
        "protractorConfig": "e2e/protractor.conf.js",
        "devServerTarget": "lmsbo-bo:serve"
      },
        "configurations": {
            "production": {
                "devServerTarget": "lmsbo-bo:serve:production"
            }
        }
    },
  ...
Run Code Online (Sandbox Code Playgroud)

编辑

我得到了e2e测试,其中包括以下内容angular.cli:

        "serve": {
            "builder": "@angular-devkit/build-angular:dev-server",
            "options": {
                "browserTarget": "lmsbo-bo:build",
                "proxyConfig": "config/proxy/proxy.tst5.json" <== **added this** line
            },
            "configurations": {
                "production": {
                    "browserTarget": "lmsbo-bo:build:production"
                }
            }
        },
Run Code Online (Sandbox Code Playgroud)

但这种解决方案无论如何都不令人满意.每次我想对另一个环境执行时,我都要改变这行代码.我宁愿要通过命令行通过编写像来管理这样的:ng serve --proxy-config config/proxy/proxy.tst5.json.

任何建议,将不胜感激!:)

Mar*_*oLe 5

尽管此功能不再受支持并且 github 上已经存在一个未解决的问题,但我发现了一种非常方便的方法来执行代理配置script

在您的 package.json 中添加以下几行(示例):

"e2e:local": "ng config projects.**yourAppName**.architect.serve.options.proxyConfig **yourProxyFile1** && ng e2e && ng config projects.**yourAppName**.architect.serve.options.proxyConfig ''",
"e2e:tst1": "ng config config projects.**yourAppName**.architect.serve.options.proxyConfig **yourProxyFile2** && ng e2e && ng config projects.**yourAppName**.architect.serve.options.proxyConfig ''"
Run Code Online (Sandbox Code Playgroud)

您所做的就是proxyConfig在您的angular.jsonviang命令中设置值并在 e2e 测试完成后重置它。确保ng命令正在运行(如果没有将angular/cli路径添加到操作系统的环境属性中并重新启动计算机)。


ray*_*wel 5

您可以通过如下更新 angular.json 来完成此操作(将您的项目名称替换为 my-project):

1) 在项目 -> my-project-e2e 中,更新 devServerTarget 从

"my-project:serve" 
Run Code Online (Sandbox Code Playgroud)

"my-project:serve:e2e"
Run Code Online (Sandbox Code Playgroud)

2)在项目->我的项目->架构师->配置中,添加

"e2e": {
          "browserTarget": "cli-advisor-portal:build:e2e",
          "proxyConfig": "proxy.local.config.json"
        }
Run Code Online (Sandbox Code Playgroud)