0

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/"
};
Kewin Dousse
  • 3,880
  • 2
  • 25
  • 46
  • It looks you might be using requirejs, is that correct? – Jordan Kasper Jan 19 '15 at 14:45
  • In any case, can you show us your qunit HTML file? – Jordan Kasper Jan 19 '15 at 14:45
  • Yes, I am using requirejs. – Kewin Dousse Jan 19 '15 at 14:45
  • Hmm, require uses Ajax to get dependencies, and if you're not running a web server those calls might fail. Have you tried setting up a simple [connect server](https://github.com/gruntjs/grunt-contrib-connect) and using `localhost`? – Jordan Kasper Jan 19 '15 at 14:46
  • Haven't tried it, I'll do it as soon as I can. However, ain't that strange that it runs ok in a browser ? Also you asked for the content of the HTML file, here it is -> http://pastebin.com/LN3igmjc – Kewin Dousse Jan 19 '15 at 14:52
  • 1
    Some browsers are ok with non-full-http Ajax calls and some aren't. Phantom is weird like that. – Jordan Kasper Jan 19 '15 at 15:11
  • Can you show the contents of that `config.js` file as well? – Jordan Kasper Jan 19 '15 at 15:12
  • I edited the question with the content of `config.js` – Kewin Dousse Jan 19 '15 at 15:15
  • @jakerella Thanks for your suggestion, I did as you said, and ran a connect server using the link you provided. I have also edited my question that now reflects all the results I'm getting by trying the tests through `localhost` (look at the travis output) : it looks like there is still something wrong somewhere... – Kewin Dousse Jan 19 '15 at 20:40

1 Answers1

1

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.
Kewin Dousse
  • 3,880
  • 2
  • 25
  • 46