lundi 19 juin 2017

Cannot import Angular MockBackend with production build

I have implemented a mock backend in my Angular application. This mock backend is used in the end to end tests.

I use a factory to create either an actual http backend service or the mock backend service. Basically it checks if this is a production build and if so it will return a http backend service and otherwise a mock backend service.

Simplified this factory function looks like this:

import {
    RequestMethod,
    Http,
    XHRBackend,
    BaseRequestOptions,
    RequestOptions,
    Response,
    ResponseOptions
} from "@angular/http";

import { MockBackend, MockConnection } from "@angular/http/testing";

function backendFactory(mockBackend: MockBackend,
        options: BaseRequestOptions,
        realBackend: XHRBackend) {
    if(!useMockBackend) {
        console.log("Configuring real HTTP backend...");
        return new Http(realBackend, options);
    }

    console.log("Configuring mock HTTP backend...");

    // Listen to incoming requests and send mock data

    return new Http(mockBackend, options);
}

When I run this code in a production build I get the following error: Uncaught Error: Cannot find module "@angular/http/testing/src/mock_backend"

I'm guessing Angular will remove some modules in a production build to decrease the web/app size. However I was not able to find more information about this process. Does anyone know if this can be disabled?

Environment:

  • Angular 4.0.0
  • Ionic 2.2.2
  • Lubuntu 14.04
  • Node 6.9.0

Aucun commentaire:

Enregistrer un commentaire