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

🚀 Feature: Better error for --file option with non-existent file #4047

Closed
outsideris opened this issue Oct 5, 2019 · 14 comments · Fixed by #5086
Closed

🚀 Feature: Better error for --file option with non-existent file #4047

outsideris opened this issue Oct 5, 2019 · 14 comments · Fixed by #5086
Labels
area: node.js command-line-or-Node.js-specific area: usability concerning user experience or interface good first issue new contributors should look here! status: accepting prs Mocha can use your help with this one! type: feature enhancement proposal

Comments

@outsideris
Copy link
Contributor

If specify non-existed file with --file option, mocha throw expection.

./bin/mocha --file non-existed
/Users/mocha/node_modules/yargs/yargs.js:1163
      else throw err
           ^

Error: Cannot find module '/Users/mocha/non-existed'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at /Users/mocha/lib/mocha.js:309:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/Users/mocha/lib/mocha.js:306:14)
    at Mocha.run (/Users/mocha/lib/mocha.js:784:10)
    at Object.exports.singleRun (/Users/mocha/lib/cli/run-helpers.js:108:16)
    at exports.runMocha (/Users/mocha/lib/cli/run-helpers.js:143:13)
    at Object.exports.handler.argv [as handler] (/Users/mocha/lib/cli/run.js:305:3)
    at Object.runCommand (/Users/mocha/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/Users/mocha/node_modules/yargs/yargs.js:1104:24)
    at Object.parse (/Users/mocha/node_modules/yargs/yargs.js:566:25)
    at Object.exports.main (/Users/mocha/lib/cli/cli.js:68:6)
    at Object.<anonymous> (/Users/mocha/bin/mocha:131:29)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

It should be handled like --require.

./bin/mocha --require non-existed
mocha inspect [spec..]

Run tests with Mocha

Rules & Behavior
  --allow-uncaught           Allow uncaught errors to propagate        [boolean]

...

  --list-interfaces  List built-in user interfaces & exit              [boolean]
  --list-reporters   List built-in reporters & exit                    [boolean]

✖ ERROR: Cannot find module 'non-existed'
@outsideris outsideris added unconfirmed-bug type: bug a defect, confirmed by a maintainer good first issue new contributors should look here! area: usability concerning user experience or interface and removed unconfirmed-bug labels Oct 5, 2019
@ntedgi

This comment was marked as outdated.

@LeonardoSalvucci

This comment was marked as outdated.

@shobhu98

This comment was marked as outdated.

@avilaviad

This comment was marked as outdated.

@kyteinsky

This comment was marked as outdated.

@kyteinsky

This comment was marked as outdated.

@alemendoza-v

This comment was marked as outdated.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Unhandled exception of--file option with non-existed file 🚀 Feature: Better error for --file option with non-existent file Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg added type: feature enhancement proposal and removed type: bug a defect, confirmed by a maintainer labels Dec 27, 2023
@JoshuaKGoldberg
Copy link
Member

Here's what the current version of Mocha, 10.2.0, prints:

$ npx mocha --file wat
Warning: Cannot find any files matching pattern "test"

Error: Cannot find module '/Users/josh/repos/mocha-examples/packages/hello-world/wat'
Require stack:
- /Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/lib/nodejs/esm-utils.js
- /Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/lib/mocha.js
- /Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/lib/cli/one-and-dones.js
- /Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/lib/cli/options.js
- /Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/bin/mocha.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at exports.requireOrImport (/Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/lib/nodejs/esm-utils.js:53:16)
    at async exports.loadFilesAsync (/Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/lib/nodejs/esm-utils.js:100:20)
    at async singleRun (/Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async exports.handler (/Users/josh/repos/mocha-examples/packages/hello-world/node_modules/mocha/lib/cli/run.js:370:5)

That first Warning: line is in yellow.

👍 that it'd be good to give a better error than "Error: Cannot find module '...'".

@JoshuaKGoldberg JoshuaKGoldberg added status: accepting prs Mocha can use your help with this one! area: node.js command-line-or-Node.js-specific labels Feb 6, 2024
@khoaHyh
Copy link
Contributor

khoaHyh commented Feb 7, 2024

Wasn't exactly sure if you wanted to keep the red Error so I have it in these two ways on my local branch. Lmk your preference and I can push it here: #5086.

Screenshot from 2024-02-06 20-57-53

Screenshot from 2024-02-06 20-58-35

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Feb 7, 2024

Hmm, do we even need the call stacks in the first place? Here are what Jest and Vitest report:

$ jest wat
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In ~/repo
  2 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 1 match
  testPathIgnorePatterns: /node_modules/ - 2 matches
  testRegex:  - 0 matches
Pattern: wat - 0 matches
 $ npx vitest wat

 DEV  v1.2.2 ~/repo

filter:  wat
include: **/*.{test,spec}.?(c|m)[jt]s?(x)
exclude:  **/node_modules/**, **/dist/**, **/cypress/**, **/.{idea,git,cache,output,temp}/**, **/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*
watch exclude:  **/node_modules/**, **/dist/**

No test files found, exiting with code 1

I think that kind of "relevant options" output would be much more useful than a big call stack. What do you think?

@khoaHyh
Copy link
Contributor

khoaHyh commented Feb 7, 2024

Good point, I don't think we need the call stack and I'm not sure a diagnostic output like Jest's or Vitest applies here. Currently --file doesn't search under a glob and just searches at the supplied path so npx mocha --file file.spec.js would fail even if it exists under a glob specified in a config file.

Screenshot from 2024-02-06 22-10-31

I think the above clues in the user that they should specify a precise path, absolute or relative, which is how --file works atm.

@JoshuaKGoldberg
Copy link
Member

Makes sense. If folks have a need for the extra fancy output they can always file a followup issue, I think. Thanks!

Accepting PRs! 🚀

@khoaHyh
Copy link
Contributor

khoaHyh commented Feb 7, 2024

#5086 ready for review 👀

@JoshuaKGoldberg
Copy link
Member

Awesome thanks! We're almost at the point where we can start reviewing PRs. This one is high up on the list given that it's a bugfix and doesn't touch too many things. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: node.js command-line-or-Node.js-specific area: usability concerning user experience or interface good first issue new contributors should look here! status: accepting prs Mocha can use your help with this one! type: feature enhancement proposal
Projects
None yet
9 participants