Skip to content

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

Closed

Description

@Exelord

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.

Activity

  1. rwjblue commented on Feb 5, 2018

    @rwjblue
    Member

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

  2. Exelord commented on Feb 5, 2018

    @Exelord
    ContributorAuthor

    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

  3. Serabe commented on Feb 8, 2018

    @Serabe
    Member

    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!

  4. Exelord commented on Feb 10, 2018

    @Exelord
    ContributorAuthor

    @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.

  5. reopened this on Feb 10, 2018
  6. andrewtimberlake commented on May 15, 2018

    @andrewtimberlake

    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

  7. stopdropandrew commented on Jun 29, 2018

    @stopdropandrew
    Contributor

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

  8. ziali088 commented on Jul 5, 2018

    @ziali088

    @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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions