How to load custom test helpers in Ember 3.2 tests?

498 views Asked by At

I seem to be missing some info about how to register a test helper in Ember 3.2, looking for some guidance:

I'm trying to use a test helper (see here) which I've placed in my tests/helpers directory, and am trying to reference it in a test:

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

module('Integration | Component | my-component', function(hooks) {
  setupRenderingTest(hooks);
  setupMirage(hooks);

  test('it renders nothing by default', async function(assert) {
    const company = server.create('company');
    this.pushMirageRecordsToStore(); // this doesn't work
    await render(hbs`{{my-component}}`);

    assert.equal(this.element.textContent.trim(), '');

  });

When this.pushMirageRecordsToStore() is called the error is:

TypeError: this.pushMirageRecordsToStore is not a function

Which leads me to believe the registerAsyncHelper helper in that link is not being called.

My question is how do I make the Ember test framework call registerAsyncHelper in that test helper so I can do this.pushMirageRecordsToStore()?

1

There are 1 answers

5
lorcan On

If you are registering a test helper and calling it pushMirageRecordsToStore then it should be available as a global so you should be able to access it in your tests as pushMirageRecordsToStore();, not this.pushMirageRecordsToStore();.

There's good documentation on registering test helpers in the ember docs here.

p.s. just to confirm, the helper you shared in the link was called pushMirageDbIntoStore but here you call pushMirageRecordsToStore. The name will need to be identical :-)