Ember cli mirage: using fixtures in acceptance test

391 views Asked by At

I am trying to use fixtures with my acceptance tests however its causing tests to fail due to duplicate data since fixtures are re-inserted in the mirage DB every test. Is there a way to get around this or remove the fixtures every test

setupApplicationTest(hooks);
setupMirage(hooks);

hooks.beforeEach(function() {
    this.server.loadFixtures();
});
1

There are 1 answers

0
mistahenry On

Where is this code that you're showing me above coming from?

In an acceptance test, Mirage automatically starts / stops the server from an initializer it bundles in its addon under addon/instance-initializers/ember-cli-mirage-autostart.js:

let server = startMirage(appInstance);
testContext.server = server;

// To ensure that the server is shut down when the application is
// destroyed, register and create a singleton object that shuts the server
// down in its willDestroy() hook.
appInstance.register('mirage:shutdown', MirageShutdown);

which calls:

willDestroy() {
   let testContext = this.get('testContext');
   testContext.server.shutdown();
   delete testContext.server;
}

Ember starts and stops the app between every test which means that each acceptance test automatically starts with an empty database.

If you are outside the context of an acceptance test, you need to do the start stopping yourself.

// tests/integration/components/your-test.js
import { startMirage } from 'yourapp/initializers/ember-cli-mirage';

moduleForComponent('your-component', 'Integration | Component | your component', {
  integration: true,
  beforeEach() {
    this.server = startMirage();
  },
  afterEach() {
    this.server.shutdown();
  }
});

Calling shutdown after each test is vital for clearing the data