admin管理员组文章数量:1415421
Almost every module that we use in NodeJS, need to be imported in some way. These modules offer functions that we can use. However, I have noticed that testing frameworks like mocha and jest dont seem to work the same way. You just include "mocha" or "jest" under the "test" script in package.json, and it does all the work.
I am very curious as to how this works. How can we make a script / function execute just by mentioning a keyword under "scripts" in package.json.
Would be very helpful if someone can answer this! :)
Almost every module that we use in NodeJS, need to be imported in some way. These modules offer functions that we can use. However, I have noticed that testing frameworks like mocha and jest dont seem to work the same way. You just include "mocha" or "jest" under the "test" script in package.json, and it does all the work.
I am very curious as to how this works. How can we make a script / function execute just by mentioning a keyword under "scripts" in package.json.
Would be very helpful if someone can answer this! :)
Share Improve this question asked May 12, 2019 at 12:38 Mayank ShahMayank Shah 536 bronze badges 4- It's not a keyword, it's a console API. If you've installed them globally you don't even need to call the mand via npm, you could call mocha or jest directly. – zzzzBov Commented May 12, 2019 at 12:45
- If I have time later (and if there aren't other answers or duplicate questions) I'll write up a short answer for how this is done. – zzzzBov Commented May 12, 2019 at 12:47
- @zzzzBov Thank you! But I'm still pretty confused about this. It would be great if you could elaborate a little more or show me how it's done. – Mayank Shah Commented May 12, 2019 at 12:48
-
1
It doesn't look like I'm going to have the time to write up a long-form answer to this for quite some time, but Tim Wong has a good answer that covers the basics. The one thing I'd add for now is that
package.json
has abin
option which allows you to build a custom CLI for your project, and npm scripts leverage these mands. – zzzzBov Commented May 20, 2019 at 15:03
2 Answers
Reset to default 6The keyword mocha
is actually a CLI, just like ls
and cat
if you use linux.
If you are asking for how to build a CLI with Node.js, the following references might help.
- Building mand line tools with Node.js
- How to build a CLI with Node.js
How mocha
works
By default, mocha looks for the glob "./test/*.js", so you may want to put your tests in test/ folder. If you want to include subdirectories, pass the --recursive option.
Reference: https://mochajs/#the-test-directory
- Run the
mocha
CLI. - The program sets the global variables (e.g.
describe
,it
, etc.). - The program loads all javascript files under the
test
directory.
We are running tests by executing "npm run test" or "npm test" mand. Not by executing "node test.js", "npm start" or "npm run start". Calling test mand executes the test lib CLI, mocha, jest, etc. This means that you are sending your test.js files to the test CLI as arguments. As a result your test mands (it, describe, etc) interpreted by test CLI, not the javascript/node. If you try "node test.js" you'll get a "ReferenceError: it/describe is not defined". Shortly, since your test.js files executed by test CLI (mocha, jest, etc) you do not need to import these libs.
本文标签: javascriptHow does Jest (or Mocha) work without having to be importedStack Overflow
版权声明:本文标题:javascript - How does Jest (or Mocha) work without having to be imported? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745216895a2648213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论