Kewin Dousse
Kewin Dousse

Reputation: 4027

grunt not running Qunit tests correctly

Situation : I am currently using QUnit to test a project in TypeScript/Javascript and everything works fine when I'm running them in a browser.

Problem : I'm trying to use grunt to run the QUnit tests in a headless mode (I need it for continuous integration testing) and the tests don't run properly.

Configuration Here's how I have things currently set up :

Gruntfile.js
package.json
src/
  - Ts source files
test/
  - config.js
  - Test.ts
  - Test.js
  - test.html

Gruntfile.js

/*global module:false*/
module.exports = function(grunt) {

    grunt.initConfig({
        connect: {
            server: {
                options: {
                    port: 8000,
                    base: '.'
                }
            }
        },

        qunit: {
            all: {
                options: {
                    urls: [
                        'http://localhost:8000/test/test.html'
                    ]
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-qunit');
    grunt.loadNpmTasks('grunt-contrib-connect');

    grunt.registerTask('test', ['connect', 'qunit']);

};

package.json

{
  // Name, version, description, repo and author fields...
  "engines": {
    "node": ">= 0.10.0"
  },
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-watch": "~0.6.1",
    "grunt-contrib-connect": "~0.9.0",
    "grunt-contrib-qunit": "~0.5.2"
  }
}

And then I have a .travis.yml file to run all of this. I don't know if it's really important because the tests don't run either in travis or in my local environment, but here is it anyways :

language: node_js
node_js:
 - "0.11"
 - "0.10"
before_install:
 - "npm install grunt --save-dev"
 - "npm install -g grunt-cli"
install:
 - "npm install"
 - "npm install -g typescript"
script:
 - "tsc --module amd --target ES5 ./src/*.ts"
 - "grunt test --verbose --force"

And here's the part that errors in the travis build : http://puu.sh/eKpWj/35614680e1.png

(I currently have ~20 assertions that pass when I'm running them in a browser. Also, the typescript compilation runs ok.)

Edit : And as someone asked fot it, here's the content of the Test.html file : http://pastebin.com/LN3igmjc

Edit 2 : Here's also the content of config.js :

var require = {
    baseUrl: "../src/"
};

Upvotes: 0

Views: 437

Answers (1)

Kewin Dousse
Kewin Dousse

Reputation: 4027

Actually I managed to make it work. I changed two things :

  1. I wasn't compiling the tests, as tsc --module amd --target ES5 ./src/*.ts compiled the files in the src folder, and the test files were in the test folder. I'm bashing myself for this one... So I simply added tsc --module amd --target ES5 ./test/*.ts in the .travis.yml file
  2. The biggest problem was that the QUnit tests were trying to start before the work of require.js. The solution I used was to tell QUnit to not start tests automatically by using QUnit.config.autostart = false; and make them start when I want with QUnit.start(); I placed this start() at the end of my Test.js file so that the tests start only when QUnit is done loading.

Upvotes: 1

Related Questions