Using aurelia-http-client-mock


#1

I am using wallabjs/webpack for my tests.
I have followed the instructions at https://gist.github.com/fragsalat/0a33295f0340e7e047ef5a820d8d2245, but can’t get it to work with wallaby.

The error I get from the wallaby console is:

 Error invoking HttpClient. Check the inner error for details.
​​​​​[Info]​​​​​     ------------------------------------------------
​​​​​[Info]​​​​​     Inner Error:
​​​​​[Info]​​​​​     Message: HttpClient requires a Fetch API implementation, but the current environment doesn't support it. You may need to load a polyfill such as https://github.com/github/fetch
​​​​​[Info]​​​​​     Inner Error Stack:
​​​​​[Info]​​​​​     Error: HttpClient requires a Fetch API implementation, but the current environment doesn't support it. You may need to load a polyfill such as https://github.com/github/fetch

In fact I am only interested in initializing the class that has a dependency on HttpClient - I am not interested in actually testing the behaviour of http itself.

The class under test:

import { HttpClient } from "aurelia-fetch-client";
import { autoinject } from "aurelia-framework";
import { IBaseFetchService, IListItem } from "./../core/interfaces";
import { IContract } from "./../models/contract/IContract";
import { AuthService } from "./auth-service";
import { BaseFetchService } from "./base-fetch-service";
import { BusyService } from "./busy-service";

@autoinject
export class ContractService extends BaseFetchService<IContract> implements IBaseFetchService<IContract, IListItem> {
  constructor(http: HttpClient, busy: BusyService, auth: AuthService) {
    super("api/contract", http, busy, auth);
  }
etc.....
}

The spec:

import { Container } from "aurelia-framework";
import { HttpClientMock } from "aurelia-http-client-mock";
import { IContract } from "./../../src/models/contract/IContract";
import { ContractService } from "./../../src/services/contract-service";

describe("ContractHeader", () => {
  let sut: ContractService = null;
  let container: Container;
  let http: HttpClientMock;

  beforeEach(() => {
    container = new Container();
    http = new HttpClientMock();
    http.registerGlobally();

    sut = container.get(ContractService);
  });

  it("should define a contract header", () => {
    // arrange
    const actual: IContract = sut.initEntity();
    expect(actual.id).toBe(null);
  });
});

I also get the same error running au jest from the command line.

Many thanks


#2

Oh - I just figured it out!

I don’t need to use aurelia-http-client-mock.

Just register HttpClient with the container.

describe("ContractHeader", () => {
  let sut: ContractService = null;
  let container: Container;

  beforeEach(() => {
    container = new Container();
    container.registerInstance(HttpClient);
    sut = container.get(ContractService);
  });

  it("should define a contract header", () => {
    // arrange
    const actual: IContract = sut.initEntity();
    expect(actual.id).toBe(null);
  });
});