javascript - How does Jest (or Mocha) work without having to be imported? - Stack Overflow

Almost every module that we use in NodeJS, need to be imported in some way. These modules offer functio

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 a bin 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
Add a ment  | 

2 Answers 2

Reset to default 6

The 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

  1. Run the mocha CLI.
  2. The program sets the global variables (e.g. describe, it, etc.).
  3. 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745216896a4617067.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信