4

How I can test nodejs backend code using qunit & grunt. In Gruntfile.js how to specify only the js files, So in qunit repo, the sample Gruntfile.js tells about creating html files and there speI am able to see example of html files which can be tested with grunt, but if I want to test only nodejs backend java script files, then how I can use that? Can someone point to some sample snippent of Gruntfile for this?

My Gruntfile.js looks as below:

  module.exports = function( grunt ) {

  grunt.loadNpmTasks( "grunt-git-authors" );
  grunt.loadNpmTasks( "grunt-contrib-concat" );
  grunt.loadNpmTasks( "grunt-contrib-jshint" );
  grunt.loadNpmTasks( "grunt-contrib-qunit" );
  grunt.loadNpmTasks( "grunt-contrib-watch" );


  grunt.initConfig({
    pkg: grunt.file.readJSON( "package.json" ),
    qunit: {
      qunit: [
         "src/test.js"
      ]
    },
    watch: {
      files: [ "*", ".jshintrc", "{src,test}/**/{*,.*}" ],
      tasks: "default"
    }
});

 grunt.registerTask( "build", [ "qunit" ] );

};

And my test.js file looks to be as below:

 test("testing", function(assert) {
     var val1 = 10;
     equal(10, val1, "We expect to be 10");
 });

But while running "grunt qunit", it is giving below error:

    Testing src/test.js 
    >> PhantomJS timed out, possibly due to a missing QUnit start() call.
    Warning: 1/1 assertions failed (0ms) Use --force to continue.

    Aborted due to warnings.

Please let me know the correct way to mention in Gruntfile.

With regards, -M-

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
u_peerless
  • 644
  • 2
  • 9
  • 23

1 Answers1

0

You have to pass an html file instead of a js file, with qunit client side js file included in it. So in this case you can have this very minimal html file:

<script src='qunit.js'></script>
<script src='test.js'></script>

(Although people will frown at me for not having html,head,body tags and stuff. I assume you know how they work and it yourself. This is not really valid html but works in this case)

Where qunit is from http://qunitjs.com/: http://code.jquery.com/qunit/qunit-1.14.0.js

And in your gruntfile change it to "src/test.html" and it will work fine.

Basically gurnt-contrib-qunit is not that advanced yet. You gotta do more work :)

You can follow this tutorial for more information: http://jordankasper.com/blog/2013/04/automated-javascript-tests-using-grunt-phantomjs-and-qunit/

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115