mocha
mocha copied to clipboard
🚀 Feature: Better error for `--file` option with non-existent file
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'
i would like to give it a try
I just made this fix, it'll verify if file exists and if not will remove from args and show a warning like the image but continue to process the other files. Do you think that is resolved like this? Must i send a PR?
[It's my first collaboration :) ]
Thanks!
I want to work on this issue
Can I work on this issue as my first issue?
I guess no one is working on this one for now.
I am not able to reproduce this issue. Mocha returns a warning and exits, along with an error.
➜ ~ mocha --require sldnksl.js
✖ ERROR: Error: Cannot find module 'sldnksl.js'
Require stack:
- /usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js
- /usr/lib/node_modules/mocha/lib/mocha.js
- /usr/lib/node_modules/mocha/lib/cli/one-and-dones.js
- /usr/lib/node_modules/mocha/lib/cli/options.js
- /usr/lib/node_modules/mocha/bin/mocha
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at exports.requireOrImport (/usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js:56:20)
at async exports.handleRequires (/usr/lib/node_modules/mocha/lib/cli/run-helpers.js:94:28)
at async /usr/lib/node_modules/mocha/lib/cli/run.js:353:25 {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js',
'/usr/lib/node_modules/mocha/lib/mocha.js',
'/usr/lib/node_modules/mocha/lib/cli/one-and-dones.js',
'/usr/lib/node_modules/mocha/lib/cli/options.js',
'/usr/lib/node_modules/mocha/bin/mocha'
]
}
➜ ~ mocha --file sldnksl.js
Warning: Cannot find any files matching pattern "test"
Error: Cannot find module '/home/tyrell/sldnksl.js'
Require stack:
- /usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js
- /usr/lib/node_modules/mocha/lib/mocha.js
- /usr/lib/node_modules/mocha/lib/cli/one-and-dones.js
- /usr/lib/node_modules/mocha/lib/cli/options.js
- /usr/lib/node_modules/mocha/bin/mocha
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.exports.requireOrImport (/usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js:56:20)
at async Object.exports.loadFilesAsync (/usr/lib/node_modules/mocha/lib/nodejs/esm-utils.js:88:20)
at async singleRun (/usr/lib/node_modules/mocha/lib/cli/run-helpers.js:125:3)
at async Object.exports.handler (/usr/lib/node_modules/mocha/lib/cli/run.js:374:5)
Has this issue been solved?
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 '...'".
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: https://github.com/mochajs/mocha/pull/5086.
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?
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.
I think the above clues in the user that they should specify a precise path, absolute or relative, which is how --file works atm.
Makes sense. If folks have a need for the extra fancy output they can always file a followup issue, I think. Thanks!
Accepting PRs! 🚀
https://github.com/mochajs/mocha/pull/5086 ready for review 👀
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!