Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with QUnit, Grunt and RequireJS

I am trying to get my Unit Tests working in Grunt, when I execute my index file in the browser the tests run successfully, however when I run the tests with grunt qunit it cannot recognise any tests.

I I am sure this is down to the way I am executing the tests, if for example I just include:

<script>
test('Always Fail', function() {
    equal(5, 2, 'The return should be 2.');
});
</script>

In the head of my index.html test page, and then run Grunt I can see this test failing. However I am trying to load my tests using requireJS, which as I have said do work within the browser.

My index.html file looks like this:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Unit Testing</title>
  <link rel="stylesheet" href="qunit/qunit.css">
  <script data-main="../unittests" src="../lib/require.js"></script>
  <script src="qunit/qunit.js"></script>
  <script>
    test('Always Fail', function() {
        equal(5, 2, 'The return should be 2.');
    });
  </script>
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
</body>
</html>

I am assuming it is this line:

<script data-main="../unittests" src="../lib/require.js"></script>

That is causing the issue and not being loaded with grunt.

My unittests.js file looks like this:

require.config({
  paths: {
      'QUnit': 'test/qunit/qunit'
  },
  shim: {
     'QUnit': {
         exports: 'QUnit',
         init: function() {
             QUnit.config.autoload = false;
             QUnit.config.autostart = false;
         }
     } 
  }
});

// require the unit tests.
require(
  ['QUnit', 'test/dummyTest'],
  function(QUnit, dummyTest) {
      // run the tests.
      dummyTest.run();
      // start QUnit.
      QUnit.load();
      QUnit.start();
  }
);

Here is my gruntfile:

module.exports = function(grunt) {

  // use grunt 
  var nocompress, optimize;

  nocompress = grunt.option('nocompress') || false;

  if(nocompress) {
    optimize = 'none';
  } else {
    optimize = 'uglify';
  }

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    requirejs: {
      compile: {
        options: {
          appDir: "dev/",
          baseUrl: "js",
          mainConfigFile: "dev/js/bootstrap.js",
          dir: "www",
          optimize: optimize,
          modules: [
            {
              name: "app"
            }
          ]
        }
      }
    },
    qunit: {
      all: ['dev/js/test/**/*.html']
    },
    jshint: {
      options: {
        curly: true,
        eqeqeq: true,
        eqnull: true,
        browser: true,
        globals: {
          jQuery: true
        },
      },
      uses_defaults: [
        'dev/js/collections/*.js',
        'dev/js/models/*.js',
        'dev/js/views/*.js',
        'dev/js/*.js', ]
    }
  });

  // Load plugins
  grunt.loadNpmTasks('grunt-contrib-qunit');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-requirejs');

  // Default task(s).
  grunt.registerTask('build-and-qunit', ['default', 'qunit']);
  grunt.registerTask('default', ['jshint', 'requirejs']);

};
like image 727
Ian Jamieson Avatar asked Jan 08 '14 15:01

Ian Jamieson


1 Answers

I've had some success using a structure similar to: https://github.com/jonnyreeves/qunit-require

  1. Disable QUnit from auto-running.
  2. Require all the dependencies.
  3. Run QUnit.
like image 103
grammar31 Avatar answered Sep 22 '22 05:09

grammar31