Loopback-next: How to pass `--grep` filter to Mocha when running `npm test`

Created on 15 Apr 2019  路  3Comments  路  Source: strongloop/loopback-next

I have a problem whenever using the grep filter

Every time I run npm test --grep "HomePage" it displays a warning message Warning: Cannot find any files matching pattern "HomePage" and runs all the tests despite having a describe with that keyword. You can find it in that repo https://github.com/ThePinger/first-project in /__tests__ /acceptance/home-page.acceptance.ts. So does anyone have a solution to this problem ??

Acceptance criteria


馃巻 Hacktoberfest 2020

Greetings :wave: to all Hacktoberfest 2020 participants!

Here are few tips 馃憖 to make your start easier, see also #6456.

  • Before you start working on this issue, please leave a comment to let others know.
  • We don't have a full list of code snippets that are missing import statements, we rely on you to find the places needing improvement. Don't worry if you miss some, others will pick them up later. It's perfectly fine to work in small steps, changing only few pages in a single pull request. Baby steps FTW, every little helps!
  • If you are new to GitHub pull requests, then you can learn about the process in Submitting a pull request to LoopBack 4.
  • If this is your first contribution to LoopBack documentation, then read Documentation guide for LoopBack 4 developers please. Consider previewing loopback-next docs only for a faster feedback loop. Finally, please refer to Contributing to LoopBack documentation for a more advanced information on our Jekyll-based documentation infrastructure.
  • Feel free to ask for help in #loopback-contributors channel, you can join our Slack workspace here.
Docs Hacktoberfest good first issue

Most helpful comment

IIRC, npm is treating --grep as its own option and does not pass it to the underlying script. As a result, the following command is invoked to run Mocha tests - notice that --grep is not present and therefore Mocha treats HomePage as a name of a file to load:

> [email protected] test /private/tmp/first-project
> lb-mocha --allow-console-logs "dist/__tests__" "HomePage"

You need to add -- before any arguments you want to npm to pass directly to the invoked script.

$ npm test -- --grep "HomePage"

With that change, npm gives both --grep and HomePage arguments to lb-mocha:

> [email protected] test /private/tmp/first-project
> lb-mocha --allow-console-logs "dist/__tests__" "--grep" "HomePage"

It would be great to capture this information in DEVELOPING.md file we create in scaffolded projects, see the template in packages/cli/generators/project/templates/DEVELOPING.md.

Would you @ThePinger like to contribute this improvement yourself? See Contributing code in LoopBack 4 and Submitting a pull request to LoopBack 4 to get started.

All 3 comments

IIRC, npm is treating --grep as its own option and does not pass it to the underlying script. As a result, the following command is invoked to run Mocha tests - notice that --grep is not present and therefore Mocha treats HomePage as a name of a file to load:

> [email protected] test /private/tmp/first-project
> lb-mocha --allow-console-logs "dist/__tests__" "HomePage"

You need to add -- before any arguments you want to npm to pass directly to the invoked script.

$ npm test -- --grep "HomePage"

With that change, npm gives both --grep and HomePage arguments to lb-mocha:

> [email protected] test /private/tmp/first-project
> lb-mocha --allow-console-logs "dist/__tests__" "--grep" "HomePage"

It would be great to capture this information in DEVELOPING.md file we create in scaffolded projects, see the template in packages/cli/generators/project/templates/DEVELOPING.md.

Would you @ThePinger like to contribute this improvement yourself? See Contributing code in LoopBack 4 and Submitting a pull request to LoopBack 4 to get started.

@bajtos Well thanks a lot for the good elaboration that worked as expected, and of course I would like to contribute this improvement.

I believe this is covered in the new troubleshooting doc: https://loopback.io/doc/en/lb4/Debugging-tests-with-mocha.html#running-specific-tests

cc @bajtos closing this issue. Feel free to reopen it if necessary.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chavitodb88 picture chavitodb88  路  3Comments

rexliu0715 picture rexliu0715  路  3Comments

shadyanwar picture shadyanwar  路  3Comments

kesavkolla picture kesavkolla  路  3Comments

zero-bugs picture zero-bugs  路  3Comments