1

I would like to use another environment variable. In my case test. The reason is that I want to inject a http interceptor for my integration tests. This interceptor should only return a mocked response with predefined data and is used for testing only.

Is there an official angular-way?

So simply I want this app.module.ts:

    import { environment } from '../environments/environment';
    // ...

    export function CustomHttpProvider (backend: XHRBackend, options: RequestOptions) {
      return new HttpService(backend, options);
    }

    export function FakeHttpProvider (backend: XHRBackend, options: RequestOptions) {
      return new FakeHttpService(backend, options);
    }

    const HttpProvider = environment.test ? FakeHttpProvider : CustomHttpProvider;

    @NgModule({
      imports: [
        BrowserModule,
        HttpModule,
        BrowserAnimationsModule,

        // ... my modules + routing here
      ],
      declarations: [AppComponent],
      providers: [
        {
          provide: Http,
          useFactory: HttpProvider,
          deps: [XHRBackend, RequestOptions]
        }
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule {}

1 Answer 1

1

Angular CLI has a command line parameter --environment that will let you do this if you set up some class to support that.

See this blog post

This answer provides lots of information related to your question.

As a side note. For angular 1.5 I have used this plugin.

Sign up to request clarification or add additional context in comments.

1 Comment

I missed to add the environment.test.ts file under environments in angular-cli.json. The link to the blog post shows more infos about that. Thanks for your answer ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.