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

被刻印的时光 ゝ 提交于 2019-12-06 08:52:50

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.

Vojta

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 ;-)

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.

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.

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