How to run QUnit tests from command line?

后端 未结 4 1168
悲哀的现实
悲哀的现实 2021-02-14 05:22

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

相关标签:
4条回答
  • 2021-02-14 05:51

    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.

    0 讨论(0)
  • 2021-02-14 05:57

    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.

    0 讨论(0)
  • 2021-02-14 06:01

    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.

    0 讨论(0)
  • 2021-02-14 06:12

    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
    
    0 讨论(0)
提交回复
热议问题