Skip to content

before hook does not run with async callback returning Promise<undefined>. #4379

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

Open
garg3133 opened this issue Feb 13, 2025 · 2 comments · May be fixed by #4380
Open

before hook does not run with async callback returning Promise<undefined>. #4379

garg3133 opened this issue Feb 13, 2025 · 2 comments · May be fixed by #4380

Comments

@garg3133
Copy link
Member

Description of the bug/issue

When using BDD-style syntax for tests, if we pass an async callback to the before hook that does not return anything, the before hook never runs.

  // does not work (returns nothing)
  before(async browser => {
    browser
      .navigateTo('https://www.ecosia.org/');
  });

  // works (returns the result from `navigateTo`)
  before(async browser =>
    browser
      .navigateTo('https://www.ecosia.org/');
  );

Steps to reproduce

use the above mentioned before hook in ecosia.js test and then run the test.

Sample test

Command to run

Verbose Output


Nightwatch Configuration

Nightwatch.js Version

tested with 3.9.0 and 3.11.0

Node Version

No response

Browser

No response

Operating System

No response

Additional Information

No response

@ac-mmi
Copy link

ac-mmi commented Feb 15, 2025

@garg3133 navigateTo() returns a Promise(which is async), so we need to await it first to make sure the navigation completes before running the test.
adding await in the before hook worked for me

before(async browser => {
  await browser.navigateTo('https://www.ecosia.org/');
});

@garg3133
Copy link
Member Author

@ac-mmi You're right! But this is something Nightwatch offers, that you don't need to always use await with commands, even in async callbacks, unless you want to get a result back from the command.

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

Successfully merging a pull request may close this issue.

2 participants