Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ember 2.18 - Not possible to test instance-initializers #16216

Closed
Exelord opened this issue Feb 5, 2018 · 8 comments · Fixed by #16794
Closed

Ember 2.18 - Not possible to test instance-initializers #16216

Exelord opened this issue Feb 5, 2018 · 8 comments · Fixed by #16794

Comments

@Exelord
Copy link
Contributor

Exelord commented Feb 5, 2018

Hey,
as from 2.18 it is not possible to test instance initializers using current blueprint.
There are 2 errors:

  1. Creating Application
Error: Assertion Failed: You cannot make a new Ember.Application using a root element that is an ancestor of an existing Ember.Application
  1. Booting Application
    When we built app instance application.buildInstance() it automatically boots app and fires the tested instance-initializer. So because of that, we are not able to prepare the test eg by registering own containers before running the initializer.
@rwjblue
Copy link
Member

rwjblue commented Feb 5, 2018

Please provide a reproduction, this should absolutely be possible so maybe there is an error in the specific test in question...

@Exelord
Copy link
Contributor Author

Exelord commented Feb 5, 2018

1. Code

export function initialize(appInstance) {
  let loggerService = appInstance.lookup('service:logger');
  return loggerService.get('enabled');
}

export default {
  initialize
};

2. Test

import Application from '@ember/application';
import { run } from '@ember/runloop';
import { initialize } from 'dummy/instance-initializers/my-init';
import { module, test } from 'qunit';
import destroyApp from '../../helpers/destroy-app';

import EmberObject from '@ember/object';

module('Unit | Instance Initializer | my init', {
  beforeEach() {
    run(() => {
      this.application = Application.create();
      this.appInstance = this.application.buildInstance();
    });
  },
  afterEach() {
    run(this.appInstance, 'destroy');
    destroyApp(this.application);
  }
});

test('it should return true', function(assert) {
  this.appInstance.register('service:logger', EmberObject.extend({enabled: true}))
  assert.ok(initialize(this.appInstance));
});

3. Error

screen shot 2018-02-05 at 15 06 56

What happened

The buildInstace booted the app before the tests so it was not possible to mock service first.

Real issue

EmberExperts/ember-rollbar-client#24

@Serabe
Copy link
Member

Serabe commented Feb 8, 2018

Yes, you can. See https://github.com/Serabe/ember-issue-16216

You need to use a Service to register a service.

Closing this.

Thank you!

@Serabe Serabe closed this as completed Feb 8, 2018
@Exelord
Copy link
Contributor Author

Exelord commented Feb 10, 2018

@Serabe Could you try with the latest ember 2.18? on 2.16 it works perfectly.
After this change Im still getting an error
Assertion Failed: You cannot make a new Ember.Application using a root element that is an ancestor of an existing Ember.Application

And also, I forgot to mention: this happening for me on an addon project.

@Exelord
Copy link
Contributor Author

Exelord commented Feb 10, 2018

I think this is related with new rfc: https://github.com/emberjs/ember.js/blob/master/blueprints/instance-initializer-test/qunit-rfc-232-files/tests/unit/instance-initializers/__name__-test.js

@Serabe Serabe reopened this Feb 10, 2018
@andrewtimberlake
Copy link

Needs reproduction: I get the error Promise rejected during "it works": Assertion Failed: You cannot make a new Ember.Application using a root element that is an ancestor of an existing Ember.Application if I create a new Ember app using 3.1.4, ember g instance-initializer my-initializer and then running ember t

@stopdropandrew
Copy link
Contributor

If you remove the setupTest(hooks); it should work, no need to create two applications.

@ziali088
Copy link

ziali088 commented Jul 5, 2018

@stopdropandrew that worked and makes sense, thanks! (version 3.2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants