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

Error when a hook is invoked inside the wrong module #1576

Open
raycohen opened this issue Apr 4, 2021 · 1 comment
Open

Error when a hook is invoked inside the wrong module #1576

raycohen opened this issue Apr 4, 2021 · 1 comment
Labels
Component: Core For module, test, hooks, and runner. Type: Enhancement
Milestone

Comments

@raycohen
Copy link
Member

raycohen commented Apr 4, 2021

When a user forgets to provide the hooks param in a module callback, they may unintentionally install a hook on a different module than is visually indicated, eg:

module('outer', function (hooks) {
  test('one', ...);

  module('inner a', function () {
    hooks.beforeEach(...); // will run before all three tests even though visually nested in "inner a"
    test('two', ...);
  });

  module('inner b', function () {
    test('three', ...);
  });
});

There is a lint rule for this, but I think it would make sense for QUnit to throw an error when this occurs. Is there any reason a user would want to do this intentionally?

@Krinkle Krinkle added Component: Core For module, test, hooks, and runner. Type: Enhancement labels Apr 4, 2021
@Krinkle
Copy link
Member

Krinkle commented Apr 4, 2021

Yeah, I agree we could flag this automatically. Basically if hooks are called on during the top-level executeNow() phase of a module, we would throw an error if it is not for the current inner-most module. This would mean it would also throw if you for some reason intentionally named them differently and accessed it directly:

module('outer', hooks => {
  test('one', );

  module('inner a', gizmos => {
    gizmos.beforeEach();
    hooks.beforeEach(); // will throw Error
    test('two', );
  });
});

I'd welcome this change in a PR, to land for QUnit 3. Preferably to first as a warning to land in QUnit 2.x, to aid in migration (or fixing of mistakes) prior to the upgrade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core For module, test, hooks, and runner. Type: Enhancement
Development

No branches or pull requests

2 participants