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

"You created a second Pretender instance while there was already one running" #915

Closed denchen closed 7 years ago

denchen commented 8 years ago

I started getting this error today with ember-cli-mirage@0.2.2, and I'm fairly certain it has to do with Pretender 1.4.0 (that has a Notify on second Pretender fix) that was released today.

beforeEach failed on visiting /myroute/foo: You created a second Pretender instance while there
was already one running. Running two Pretender servers at once will lead to unexpected
results!Please call .shutdown() on your instances when you no longer need them to respond.

This was in my acceptance test. I have failures in my integration tests as well, which look like:

Promise rejected before it renders: Assertion Failed: You cannot use the same root element
(#ember-testing) multiple times in an Ember.Application

So what is the appropriate way to shutdown the Mirage server? I currently have this in my acceptance tests:

afterEach() {
  Ember.run(application, 'destroy');
  server.shutdown();
}

Do I need something similar in my integration tests?

denchen commented 8 years ago

I found a workaround for both my acceptance and integration tests.

In acceptance tests, my beforeEach() now looks like:

 beforeEach() {
   server.shutdown();
   application = startApp();
 },

I'm still not sure why server.shutdown() in afterEach() doesn't properly shutdown the server.

And for my integration/unit tests, I've modified the code from Manually starting up Mirage to this:

// See: http://www.ember-cli-mirage.com/docs/v0.2.x/manually-starting-mirage/
import mirageInitializer from '../../initializers/ember-cli-mirage';

export default function startMirage(container) {
  if (this.server) {
    console.warn('Shutting down Mirage server before re-initializing');
    this.server.shutdown();
  }
  mirageInitializer.initialize(container);
}

All my tests now pass. However, I'm not sure if the above modifications are what I should be doing or not.

netes commented 8 years ago

Same here.

netes commented 8 years ago

Update: It seems that updating module-for-acceptance.js to the newest version (from Ember CLI 2.8) and adding this snippet to integration tests which use Mirage:

    afterEach() {
      server.shutdown();
    },

does the job - everything is fine then.

dustinfarris commented 8 years ago

Mirage monky-patches the server.shutdown for acceptance tests, but not for integration tests. I suspect this will need to be done manually (as @netes has shown) and should be mentioned in the docs.

morhook commented 8 years ago

Is the https://github.com/samselikoff/ember-cli-mirage/pull/917 related to this issue?

dustinfarris commented 8 years ago

Yes, it is an internal fix for the same thing. Users will need to do this on their own for now.

morhook commented 8 years ago

Thanks @dustinfarris ! Sounds good!

Leooo commented 8 years ago

I tried everything but still can't make it work even with one single acceptance test. Anyone is in the same case? I'm using a custom moduleForAcceptance this may be the reason why..

This is definitely due to https://github.com/pretenderjs/pretender/pull/178 having been released without a major bump.

shellandbull commented 8 years ago

temporary workaround for us on #922

trek commented 8 years ago

Pretender 1.4.1 is published and it will only give you a console.warn slap on the wrist.

dustinfarris commented 8 years ago

Thanks @trek. I think we should still try to dissuade users from overlapping servers going forward though by updating the documentation here.

For example, users relying on startMirage in their integration tests need to know why they are going to start seeing this warning.

I don't think there is a way to automagically handle this like we do for acceptance tests because moduleForComponent comes directly from ember-qunit, probably best to just advise users to run shutdown after using it.

trek commented 8 years ago

Overlapping servers will also prevent some useful tools like https://percy.io/ from running in projects that use Mirage in tests.

mdarmetko commented 7 years ago

I'm using mostly default generated tests with ember 2.10 and was having this issue. Resolved it by adding if(server !== undefined) { server.shutdown(); } to beforeEach and afterEach in each acceptance test file. As in,

module('Acceptance: Metro', {
  beforeEach: function() {
    if(server !== undefined) { server.shutdown(); }
    application = startApp();
    originalConfirm = window.confirm;
    window.confirm = function() {
      confirmCalledWith = [].slice.call(arguments);
      return true;
    };
  },
  afterEach: function() {
    Ember.run(application, 'destroy');
    window.confirm = originalConfirm;
    confirmCalledWith = null;
    if(server !== undefined) { server.shutdown(); }
  }
});

Hope this helps someone else having the same problem.

DimkaVardina commented 6 years ago

You saved my day! Thank you!