Search code examples
javascriptcookiesgruntjsqunitgrunt-contrib-qunit

How do I run QUnit tests with Grunt through a local server instead of the file system?


I'm building a small JS object that manages cookies, basic set, get, and remove functions. I've written tests with QUnit that pass in a browser if I'm running against a local server. I'm booting up a server like so:

python -m SimpleHTTPServer 8080

If I run the tests against the local file system (e.g. file:///) they fail because cookies are host dependent. document.cookie is always "" against the file system. I'd like to continue using my grunt tasks to run the tests but this is a bit of a road block. It looks like someone anticipated this problem but perhaps never found a failing test case: http://bugs.jqueryui.com/ticket/8954.

Suggestions?


Solution

  • Use the urls option of grunt-contrib-qunit and insert the URL of the page you visit in the browser (Im guessing http://localhost:8080/test.html but the path to your tests might be different):

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

    Then run grunt qunit to run the tests. Here are the docs with more examples: https://github.com/gruntjs/grunt-contrib-qunit#testing-via-http-or-https