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.
QUnit now has its own CLI:
Run
qunit --help
for more usage information.node-qunit-phantomjs gets the job done easy enough and is standalone, not a Grunt-, Gulp-, whatever-plugin:
TL;DR
Use out-of-the-box
qunit
command (donpm 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!
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:
And let's imagine that in your
index.js
you have following: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):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 functiondoSomething
. 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: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:When it's done, first check tests.html still working and not raising any errors (testing test infrastructure, yeah) and, finaly, try
And the output should be like
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 pageWhat 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
See example a.k.a. small getting started here.
You can use Grunt (task runner) for this. You would also need to install these two packages: grunt-contrib-qunit and grunt-contrib-connect
I did just recently set up a GitHub repository when trying to figure out how to run QUnit on Travis CI: https://github.com/stebru/travis-qunit-test
You're welcome to fork it and try it out for yourself.