Problem with unit testing and mocking API call

I’m trying to create a unit test case using Karma and Jasmine. I’m using jasmine-ajax to stub out the API request with a fake response. But the problem I have is that the unit test throws an error saying “TypeError: aurelia_pal__WEBPACK_IMPORTED_MODULE_1__.DOM.createCustomEvent is not a function”.

The unit test:

import { SystemInfo } from './../../src/common/models/system-info';
import { InfoPresentation } from "applications/info_presentation/info-presentation";

describe('Info presentation:', () => {
  let _infoPresentationViewModel: InfoPresentation;

  beforeEach(() => {
    _infoPresentationViewModel = new InfoPresentation();

  afterEach(() => {

  describe('when the info is retrieved', () => {
    let _expectedValue: Collections.Set<SystemInfo>;

    beforeEach(() => {
        status: 200,
        responseText: '[{"id":1, "infoText": "Updated"}]'

      _expectedValue = new Collections.Set<SystemInfo>();
      _expectedValue.add(Object.assign(new SystemInfo(), {
        id: 1,
        infoText: "Updated",

    it('then should be presented on the page', () => {

The view model:

import * as Collections from 'typescript-collections';
import { SystemInfo } from 'common/models/system-info';
import { HttpClient } from 'aurelia-http-client';

export class InfoPresentation {
  SystemInfos: Collections.Set<SystemInfo>;
  private _http: HttpClient;

  constructor() {
    this._http = new HttpClient().configure(config => {
      config.withHeader('Content-Type', 'application/json')

  attached() {
  public startRetrievingSystemInfos() {
    .then(result => {
    .catch(error => {

Any ideas what is the problem?

1 Like

Aurelia platform initialization code needs to run first in order to have the references in DOM, PLATFORM object populated.

For your scenario, probably do

import { initialize } from 'aurelia-pal-browser';


... your test code

Well now it no longer throws an error but the request never seems to be handled by jasmine-ajax because nothing will be logged.
Also I added the following line to my it function:


But this will only result in the following:

TypeError: jasmine.Ajax.requests.mostRecent(…) is undefined

However if I replace the code in startRetrievingSystemInfos with a regular XMLHttpRequest it will work as expected. Is aurelia-http-client not using XMLHttpRequest under the hood? Or am I still missing something?