Search code examples
gulpqunit

Gulp load locally saved qunit


I have the following tree structure:

- tests
|-- simple_basic.html

And I try to test it like that:

const qunit = require('node-qunit-phantomjs');
const fs = require('fs');

gulp.task('test', (done) => {
    fs.readdir('./tests', (err, files) => {
        files.forEach(file => {
          qunit('./tests/'+file);
        });
        done();
    });
});

But I get the following error:

[23:00:41] Using gulpfile ~/Kwdikas/Javascript/no_url_page/gulpfile.js
[23:00:41] Starting 'test'...
Testing file:////home/pcmagas/Kwdikas/Javascript/no_url_page/tests/simple_basic.html
[23:00:41] Finished 'test' after 16 ms
ReferenceError: Can't find variable: QUnit

  file:////home/pcmagas/Kwdikas/Javascript/no_url_page/tests/simple_basic.html:14 in global code
ReferenceError: Can't find variable: QUnit

  undefined:6
The `QUnit` object is not present on this page.

  phantomjs://code/runner-json.js:78

But How I can test using the locally saved QUnit?


Solution

  • Just include the following files:

    • ../node_modules/qunit/qunit/qunit.css
    • ../node_modules/qunit/qunit/qunit.js

    To any file located in the tests directory. Thus the simple_basic.html will turn into (placing a dummy test):

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>QUnit Example</title>
      <link rel="stylesheet" href="../node_modules/qunit/qunit/qunit.css">
    </head>
    <body>
      <div id="qunit"></div>
      <div id="qunit-fixture"></div>
      <script src="../node_modules/qunit/qunit/qunit.js"></script>
      <script>
          // Replace the test with the one you really need
          QUnit.test( "a basic test example", function( assert ) {
            var value = "hello";
            assert.equal( value, "hello", "We expect value to be hello" );
          });
      </script>
    </body>
    </html>