Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run QUnit tests from command line?

I recently started working on a Rails app that has a large amount of QUnit tests already in place for testing ember. I have been charged with the task of setting the app with a CI (I decided to use CodeShip). The issue I currently face is that the only way for me to run the qunit tests is to go to http://localhost:3000/qunit. I need to setup a way to run the tests from the command line. I have done a large amount of research, and have tried at least 10 different solutions but non have managed to work.

Currently I am attempting to use teaspoon but it I have not managed to get it to work. Any help would be much appreciated. Please let me know if I need to post more information about the setup.

like image 748
Zack Avatar asked Jul 11 '14 18:07

Zack


People also ask

How do you write a test case on Qunit?

Create a Test Case Make a call to the QUnit. test function, with two arguments. Name − The name of the test to display the test results. Function − Function testing code, having one or more assertions.

How does QUnit work?

QUnit is a perfect unit test framework for JavaScript programming language. A formal written unit test case is characterized by a known input and by an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a post-condition.


3 Answers

TL;DR

Use out-of-the-box qunit command (do npm install -g qunit beforehand), so you don't need additional dependencies.


Extending a bit Arthur's answer because he mentioned only simplest case which works only for simplest projects.

As mentioned on QUnit page, there is built-in possibility to run tests from command line. There is no need to install additional weird frameworks on top of QUnit!

npm install -g qunit
qunit # Will search for tests in "test" directory

This works for artificial tests as on their website, but in real project you probably will have your logic in some other .js file.

Having following structure:

project
│   index.js <--- Your script with logic
│   package.json <--- most probably you'll have npm file since qunit executable is installed via npm
└───test
        tests.html <--- QUnit tests included in standard HTML page for "running" locally
        tests.js <--- QUnit test code

And let's imagine that in your index.js you have following:

function doSomething(arg) {
  // do smth
  return arg;
}

And the test code in tests.js (not that it can be the whole content of the file - you don't need anything else to work):

QUnit.test( "test something", function( assert ) {
  assert.ok(doSomething(true));
});

Running in browser

This is not related directly to the question, but just want to make a reference here. Just put both your script and tests to tests.html and open the page in browser:

<script type="text/javascript" src="../index.js"></script>

<script src="tests.js"></script>

Running from command line

With the setup described below you can try to run qunit, but it will not work because it cannot find your function doSomething. To make it accessible you need to add two things to the scripts.

First is to explicitly "import" your script from tests. Since JS doesn't have sunch a functionality out-of-the box, we'll need to use require coming from NPM. And to keep our tests working from HTML (when you run it from browser, require is undefined) add simple check:

// Add this in the beginning of tests.js
// Use "require" only if run from command line
if (typeof(require) !== 'undefined') {
    // It's important to define it with the very same name in order to have both browser and CLI runs working with the same test code
    doSomething = require('../index.js').doSomething;
}

But if index.js does not expose anything, nothing will be accessible. So it's required to expose functions you want to test explicitly (read more about exports). Add this to index.js:

//This goes to the very bottom of index.js
if (typeof module !== 'undefined' && module.exports) {
  exports.doSomething = doSomething; 
}

When it's done, first check tests.html still working and not raising any errors (testing test infrastructure, yeah) and, finaly, try

qunit

And the output should be like

TAP version 13
ok 1 Testing index.js > returnTrue returns true
1..1
# pass 1
# skip 0
# todo 0
# fail 0

I don't want to deal with node for my simple (or not) project

This is an open question and I cannot answer this. But you'll need some runner to run QUnit tests anyway. So maybe having package.json with one devDependency like "qunit": "^2.6.1" is not the worst option here. There are several 3rd-party runners: grunt-qunit, PhantomJS runnner, ember-qunit-cli, also see more on official QUnit Plugins page

What if I have class, not function?

In JS everything is a function, right :)? So no problem, just change your script exports and tests import acordingly

exports.exportedMyClass = MyClass; // in index.js
MyClass= require('../index.js').exportedMyClass ; // in tests.js

See example a.k.a. small getting started here.

like image 198
The Godfather Avatar answered Sep 24 '22 15:09

The Godfather


QUnit now has its own CLI:

$ npm install -g qunit
$ qunit 'tests/*-test.js'
TAP version 13
ok 1 Module > Test #1
ok 2 Module > Test #2
1..2
# pass 2
# skip 0
# todo 0
# fail 0

Run qunit --help for more usage information.

like image 37
Nathan Arthur Avatar answered Sep 20 '22 15:09

Nathan Arthur


node-qunit-phantomjs gets the job done easy enough and is standalone, not a Grunt-, Gulp-, whatever-plugin:

$ npm install -g node-qunit-phantomjs

$ node-qunit-phantomjs tests.html
Testing tests.html
Took 8 ms to run 1 tests. 0 passed, 1 failed.
...
$ echo $?
1
like image 45
famousgarkin Avatar answered Sep 23 '22 15:09

famousgarkin