Is it possible to run a single test using QUnit inside Karma?

孤者浪人 提交于 2019-12-22 15:07:13

问题


Jasmine has iit() and ddescribe, and Mocha has it.only and describe.only, but I can't see any way to get QUnit to focus on running a single test.

The QUnit UI allows you to run a single test, but I can't see how to get this to work inside Karma, because it doesn't display the QUnit UI.


回答1:


Here is my solution. It works fine for me, but YMMV.

Download qunit-karma-setup.js, qunit-karma-launch.js from here

Then, in your Gruntfile:

config.set({
  frameworks: ['qunit'],
  files: [
    'your-app-files-here/**/*.js',
    'qunit-karma-setup.js',
    'your-tests-here/**/*.js',
    'qunit-karma-launch.js'
  ]
});

Now, you can use omodule(), otest(), oasyncTest() functions to run only the selected modules or tests respectively.




回答2:


QUnit is not well designed for this, but you can improve it!

The filtering mechanism of QUnit is validTest function that read QUnit.config.filter (string, name of a test). See https://github.com/jquery/qunit/blob/master/src/core.js#L820

There are two problems:

  1. it only allows selecting one test,
  2. you need to know the selected test name in advance (because the tests are filtered when being created).

Suggest changing QUnit to:

  • filter using a custom "filter" function (the default implementation can do what the current validTest does),
  • filter the tests when executing (means, collects all the tests first)

Then, implementing inclusive tests will be simple ;-)




回答3:


Add this on the top of your test file

var ttest = test;
test = function() {}

And rename the test you want to run to ttest. Looks clumsy, but simple and works well.




回答4:


I believe you can use only. Taken from the docs:

test('my test 1', function (assert) { ... });

only('my test that will be run exclusively now', function (assert) { ... });

So instead of using word test, you use only.



来源:https://stackoverflow.com/questions/18975998/is-it-possible-to-run-a-single-test-using-qunit-inside-karma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!