Few questions:

  • How Karma and testing framework X (Jasmine, Mocha, QUnit) relate to each other?
  • What is the equivalent framework at Java world? I assume Jasmine, Mocha, QUnit equal to jUnit/TestNG. How about Karma?
  • Can I run testing framework X (e.g. Jasmine) without Karma?
  • Is Karma for unit test or integration/e2e test? This reference shows is for unit test, however this said is for e2e test.

推荐答案

Karma is a browser test runner.

The idea is that browsers do not have natively a concept of loading tests files, running them, and reporting results. What karma does is (roughly) :

  • starting a small web server to serve "client-side" javascript files to be tested (1)
  • also serve the "client-side" javascript files with the tests (or Specs, as they're often called) (2)
  • serve a custom web page that will run javascript code for the tests (3)
  • start a browser to load this page (4)
  • report the results of the test to the server (5)
  • karma can then again report the results to text files, the console, anything your CI server likes, etc...

Looking at each part :

(1) Those files will be your actual js files ; you will tell karma how to load them. If you use requirejs, there is a karma plugin, and some config is needed.

(2) Those tests can be written in a variety of Javascript testing framework (Jasmine, QUnit, Mocha) ; this is JS code that is run in the browser.

(3) The custom web page will be a bit different for each testing framework ; this is why karma has plugins for different frameworks.

(4) Karma can launch the page in many browsers (FF, Chrome, or headless browsers like PhantomJs.)

(5) Reporting to karma is, again, framework-dependant, and dealt with karma plugins.

So to answer your questions :

  • in Java, most people use JUnit which is both a framework to write tests and run them, but does not have the problem of differentiating the environment in which tests are run and the one in which test reports are aggregated ; karma would be the missing piece between a JUnit Suite and a JUnit TestRunner
  • Yes, you can do everything that karma does "by hand" - pick one framework (jasmine, qunit, mocha) and follow instructions. The advantage of karma is that it provide a solution out-of-the-box, if you're in a standard setup.
  • Karma can be used for both unit test (with jasmine / qunit / whatever) and integration tests (which will use another API, like webdriver, to drive the browser)

Javascript相关问答推荐

处理时间和字符串时MySQL表中显示的日期无效

Regex结果包含额外的match/group,只带一个返回

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

VUE 3捕获错误并呈现另一个组件

将内容大小设置为剩余可用空间,但如果需要,可在div上显示滚动条

如何在Bootstrap中减少网格系统中单个div的宽度

获取';无法解决导入和导入";slick-carousel/slick/slick-theme.css";';错误

更新Redux存储中的对象数组

ngOnChanges仅在第二次调用时才触发

更改agGRID/Reaction中的单元格格式

用于测试其方法和构造函数的导出/导入类

在SuperBase JS客户端中寻址JSON数据

JWT Cookie安全性

调用特定数组索引时,为什么类型脚本不判断未定义

脚本语法错误只是一个字符串,而不是一个对象?

暂停后只有一次旋转JS

如何在Flat Pickr中更改取消选中日期的 colored颜色

无法从fixture 文件中提取数据

Reaction Chart.js为显示格式化日期的xax提供相同的差距

从添加的袋子中删除产品在Redux-Toolkit中不起作用