miragejs / ember-cli-mirage

An Ember Addon to easily add Mirage JS to your Ember app.
http://ember-cli-mirage.com
MIT License
863 stars 439 forks source link

Mirage doesn't work in engines tests. #1737

Open luketheobscure opened 5 years ago

luketheobscure commented 5 years ago

Bug or question

When using a custom resolver (like what's recommended in the engines testing guide), ember-cli-mirage doesn't find the dummy config. The code where mirage registers the config does run, but it looks like it registers it with the wrong resolver.

I've got this working by manually registering the config before calling setupMirage:

// custom setupRendering test that sets the resolver
import setupRenderingTest from 'dummy/tests/helpers/setup-rendering-test';
import config from 'dummy/mirage/config';
import { setupMirage } from 'ember-cli-mirage/test-support';

module('Foo', function (hooks) {
  setupRenderingTest(hooks);
  hooks.beforeEach(function () {
    this.owner.register('mirage:base-config', config)
  });
  setupMirage(hooks);

  test('it renders', async function (assert) {
    await render(hbs`<MyDataLoadingComponent />`);

    assert.equal((this.element.textContent || '').trim(), 'Foo');
  });
});
samselikoff commented 5 years ago

At the least we should document this.

Can I see the full resolver setup? I've never used engines so I'm not familiar.

luketheobscure commented 5 years ago

Here's the entirety of our setup-rendering-test file:

import { setupRenderingTest } from 'ember-qunit';
import engineResolverFor from 'ember-engines/test-support/engine-resolver-for';
import Resolver from 'dummy/resolver';

export default function (hooks: NestedHooks): void {
  const engineResolver = engineResolverFor('crop-records-engine');

  const resolver = Resolver.extend({
    namespace: { modulePrefix: 'dummy' },
    resolve() {
      const resolved = this._super(...arguments);
      if (resolved) {
        return resolved;
      }

      return engineResolver.resolve(...arguments);
    },
  }).create();

  return setupRenderingTest(hooks, { resolver });
}