简体   繁体   English

咕unt声未正确运行Qunit测试

[英]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. 情况 :我目前正在使用QUnit在TypeScript / Javascript中测试项目,并且在浏览器中运行它们时一切正常。

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. 问题 :我正在尝试使用grunt在无头模式下运行QUnit测试(我需要它来进行连续集成测试),并且测试无法正常运行。

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 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 的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. 然后,我有一个.travis.yml文件来运行所有这些程序。 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 : 我不知道这是否真的很重要,因为测试既不在travis中也不在我的本地环境中运行,但是无论如何这都是这样:

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 这是travis构建错误的部分: 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.) (我目前在浏览器中运行它们时会传递约20个断言。此外,打字稿编译运行正常。)

Edit : And as someone asked fot it, here's the content of the Test.html file : http://pastebin.com/LN3igmjc 编辑:正如有人问的那样,这是Test.html文件的内容: http : //pastebin.com/LN3igmjc

Edit 2 : Here's also the content of config.js : 编辑2:这也是config.js的内容:

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

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. 我不是在编译测试,因为tsc --module amd --target ES5 ./src/*.ts编译了src文件夹中的文件,而测试文件位于test文件夹中。 I'm bashing myself for this one... So I simply added tsc --module amd --target ES5 ./test/*.ts in the .travis.yml file 我为此.travis.yml ...因此,我只是在.travis.yml文件中添加了tsc --module amd --target ES5 ./test/*.ts
  2. The biggest problem was that the QUnit tests were trying to start before the work of require.js. 最大的问题是QUnit测试试图在require.js工作之前开始。 The solution I used was to tell QUnit to not start tests automatically by using QUnit.config.autostart = false; 我使用的解决方案是通过使用QUnit.config.autostart = false;来告诉QUnit不要自动开始测试QUnit.config.autostart = false; and make them start when I want with QUnit.start(); 并在需要时使用QUnit.start();使它们启动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. 我将此start()放置在Test.js文件的末尾,以便仅在QUnit完成加载后才开始测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM