我们在 NodeJS 中使用的几乎每个模块都需要以某种方式导入。这些模块提供了我们可以使用的功能。但是,我注意到 mocha 和 jest 等测试框架的工作方式似乎不同。您只需在 package.json 的“测试”脚本下包含“mocha”或“jest”,它就会完成所有工作。
我很好奇这是如何工作的。我们如何通过在 package.json 中的“脚本”下提及关键字来执行脚本/函数。
如果有人能回答这个问题会很有帮助! :)
最佳答案
关键字mocha
实际上是一个CLI ,就像 ls
和 cat
如果你使用 linux。
如果您询问如何使用 Node.js 构建 CLI,以下引用资料可能会有所帮助。
mocha
的工作原理
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.org/#the-test-directory
- 运行
mocha
CLI。 - 程序设置全局变量(例如
describe
、it
等)。 - 程序加载
test
目录下的所有javascript文件。
关于javascript - Jest(或 Mocha)如何在无需导入的情况下工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099330/