Angular CLI e2e& useFactory切换

时间:2017-07-06 00:02:45

标签: angular protractor angular-cli

我正在尝试为Angular 4提供程序工厂提供一个模式,以便我可以切换运行ng e2e时使用的实现。

我已尝试在angular-cli中尝试使用build environments做一些事情,但遇到问题ng serve -e e2e

$ ng serve -e e2e
Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at createSourceFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:15457:109)
    at parseSourceFileWorker (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:15389:26)
    at Object.parseSourceFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:15338:26)
    at Object.createSourceFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:15192:29)
    at VirtualFileStats.getSourceFile (/home/user/Documents/workspace/ngproj/node_modules/@ngtools/webpack/src/compiler_host.js:66:35)
    at WebpackCompilerHost.getSourceFile (/home/user/Documents/workspace/ngproj/node_modules/@ngtools/webpack/src/compiler_host.js:213:38)
    at findSourceFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:67909:29)
    at processImportedModules (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:68056:25)
    at findSourceFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:67937:17)
    at processImportedModules (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:68056:25)
    at findSourceFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:67937:17)
    at processImportedModules (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:68056:25)
    at findSourceFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:67937:17)
    at processSourceFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:67840:27)
    at processRootFile (/home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:67728:13)
    at /home/user/Documents/workspace/ngproj/node_modules/typescript/lib/typescript.js:67018:60

无视该特定问题,是否有人对提供商工厂和基于cli参数或特定环境的切换实施有良好的模式(例如检测应用程序正在通过ng e2e运行)

const authFactory = (http) => {
  if (environment.useRestApi) {
    console.log('Using HTTP REST Api')
    return new HttpAuthService(http);
  } else {
    console.log('Using In Memory REST Api')
    return new InMemoryAuthService();
  }
};

@NgModule({
  imports: [
    CommonModule,
    HttpModule
  ],
  declarations: [],
  providers: [
    {
      provide: AuthService,
      useFactory: authFactory,
      deps: [Http]
    }
  ]
})
export class AuthModule {
}

我对使用MockBackend / MockConnection不感兴趣(我已在我的.spec.ts文件中使用这些来测试HttpAuthService

堆栈跟踪更新

堆栈跟踪是由我的.angular-cli.json文件中的拼写引起的:

  "environments": {
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts",
    "e2e": "environments/environments.e2e.ts"
  }

这里引用的e2e文件在文件名中有一个额外的's'。总是如此,ng-cli / webpack / ts / node确实很好地告诉我这一点。

我仍然对替代方案持开放态度,所以我会将此作为更新与答案保留(希望未解决的问题可能会引起更多关注),但与此同时,使用环境对我有用

0 个答案:

没有答案