Mathyn
Mathyn

Reputation: 2600

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:

Upvotes: 0

Views: 461

Answers (1)

CharanRoot
CharanRoot

Reputation: 6325

Try upgrade your angular version into 4.1.2. this issue fixed in the angular > 4.1.2.

more info https://github.com/angular/angular/issues/15521#issuecomment-290118657

Upvotes: 1

Related Questions