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

V2 addons: initializers are duplicated or ran in the wrong context? #1127

Closed
NullVoxPopuli opened this issue Feb 12, 2022 · 2 comments
Closed

Comments

@NullVoxPopuli
Copy link
Collaborator

Reproduction: https://github.com/NullVoxPopuli/ember-addon-v2-duplicate-modules

I first noticed this when I was messing with: NullVoxPopuli/ember-statechart-component#244 (which relies on an initializer to utilize setComponentManager) -- it presented itself via ember saying that it didn't know of a component manager to handle StateNode (from xstate) -- which now makes sense, because the initializer code is in a chunk that runs after initializers are needed.

Odd things that can be noticed:

  • the module can be imported twice (a log is at module-level in the initializer, and logs when the app/test boots, and again during import)
  • the initializer only runs once (when called manually in the test) -- I'd expect the initializer to run during app/test boot?
@ef4
Copy link
Contributor

ef4 commented Feb 12, 2022

The reason your initializer isn't running normally is that your test isn't using one of the setup functions from ember-qunit. You need at least import { setupTest } from 'ember-qunit'.

The reason your module is getting duplicated is that your app imports the initializer from the path

my-addon/instance-initializers/test-initializer.js

but your test suite imports the initializer from the path

my-addon/instance-initializers/test-initializer

And this seems to be enough to confuse ember-auto-import, so I filed embroider-build/ember-auto-import#503

@ef4 ef4 closed this as completed Feb 12, 2022
@NullVoxPopuli
Copy link
Collaborator Author

legit. thanks!

I confirmed that by adding setupTest and by changing my import to have .js at the end, the test in the example passes.
Which I should have included in the OP, but (for completeness) it's:

// initializer
console.log('running...');

let i = 0;

export function initialize() {
  i++;
  console.log(`invoked... ${i}`);
  window.i = i;
}

export default {
  initialize,
};

and the test (now passing)

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

import { initialize } from 'my-addon/instance-initializers/test-initializer.js';

module('The Test', function (hooks) {
  setupTest(hooks);

  test('did initializer laod?', function (assert) {
    assert.strictEqual(window.i, 1);

    initialize();

    assert.strictEqual(window.i, 2);
  });
});

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

No branches or pull requests

2 participants