I’m using ember-network to polyfill my fetch requests. Testing this can be a bit tricky since many browsers already have native support. Having a local reference will miss out on the mocked version.

I decided to generate an initializer to replace native fetch with the polyfill so it’s easier to test.

initializers/fetch.js

import fetchPolyfill from 'ember-network/fetch';

export function initialize() {
  window.fetch = fetchPolyfill;
}

export default {
  name: 'fetch',
  initialize
};

fetch-test.js

import fetch from 'ember-network/fetch';
const expectedFetch = fetch;

describe('Unit | Initializer | fetch', function() {
  let application;

  beforeEach(function() {
    Ember.run(function() {
      application = Ember.Application.create();
      application.deferReadiness();
    });
  });

  afterEach(function() {
    destroyApp(application);
  });

  it('replaces native fetch with a polyfill', function() {
    initialize(application);

    expect(fetch.toString()).to.equal(expectedFetch.toString());
  });
});

Works like a charm :)