I am trying to get my unit tests to work in Grunt, when I run my index file in a browser, the tests succeed, however, when I run the tests with grunt qunit , it cannot recognize any tests.
I am sure that this is not the way I run the tests, if, for example, I just include:
<script> test('Always Fail', function() { equal(5, 2, 'The return should be 2.'); }); </script>
At the beginning of my test page index.html, and then run Grunt, I see that this test does not work. However, I am trying to download my tests using requireJS, which, as I said, works in 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 assume this is a line:
<script data-main="../unittests" src="../lib/require.js"></script>
This causes a problem and does not load 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 grunt file:
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']); };