I am trying to setup a QUnit environment using requirejs and grunt-contrib-qunit.

我正在尝试使用requirejs和grunt- managed - QUnit建立一个QUnit环境。

Here is what I have.

这是我的。

gruntfile:

gruntfile:

qunit: {
  all: {
    options: {
      urls: [
        'http://localhost:8000/qunit/qunit-test-suite.html'
      ]
    }
  }
},

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

qunit-test-suite.html:

qunit-test-suite.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>QUnit Tests Suite: travis CI Test</title>
  <link rel="stylesheet" href="../components/libs/qunit/qunit/qunit.css">
</head>
<body>

  <div id="qunit"></div>
  <div id="qunit-fixture"></div>

  <script src="../components/libs/qunit/qunit/qunit.js"></script>
  <script>
    QUnit.config.autoload = false;
    QUnit.config.autostart = false;
  </script>

  <script data-main="qunit" src="../components/libs/requirejs/require.js"></script>

</body>
</html>

qunit.js:

qunit.js:

require.config({
    baseUrl: "../",
    paths: {
      'jquery': 'components/libs/jquery/dist/jquery.min',

      // Test for Foo
      'foo': 'components/app/foo/foo',
      'test-Foo': 'components/app/foo/test-Foo'
    },
    shim: {
     'QUnit': {
       exports: 'QUnit',
       init: function() {
         QUnit.config.autoload = false;
         QUnit.config.autostart = false;
       }
      }
    }
});

require(['test-Foo'], function (Foo) {
  QUnit.load();
  QUnit.start();
});

test-Foo.js:

test-Foo.js:

define(['foo'], function(Foo) {

  'use strict';

  module("Foo");

  test("Foo return Test", function() {
    equal(Foo.foo(), "foo", "Function should return 'foo'");
    equal(Foo.oof(), "oof", "Function should return 'oof'");
  });

  test("Bar return Test", function() {
    equal(Foo.bar(), "barz", "Function should return 'bar'");
  });

});

Problem is that it all works fine when I open up the test-suite.html in my browser. Once sent to PhantomJS I get the following error:

问题是,当我打开测试套件时,一切都没问题。在我的浏览器的html。一旦发送给PhantomJS,我就会得到以下错误:

Running "connect:server" (connect) task
Started connect web server on http://localhost:8000

Running "qunit:all" (qunit) task
Testing http://localhost:8000/qunit/qunit-test-suite.html

>> PhantomJS timed out, possibly due to a missing QUnit start() call.
Warning: 1/1 assertions failed (0ms) Use --force to continue.

Aborted due to warnings.

Full setup: https://github.com/markusfalk/test-travis

全设置:https://github.com/markusfalk/test-travis

Test Run: https://travis-ci.org/markusfalk/test-travis

测试运行:https://travis-ci.org/markusfalk/test-travis

Thanks for any help :)

谢谢你的帮助。

3 个解决方案

#1


4

With the help of Jörn I came up with a working setup. Trick is to setup requireJS before QUnit loads (moved requireJS config to config.js and load it first).

在Jorn的帮助下,我想出了一个工作方案。诀窍是在QUnit装载之前设置requireJS(将requireJS配置移动到config配置中)。js和load it first)。

Requirements:

要求:

  • grunt-contrib-qunit v0.7.0
  • grunt-contrib-qunit v0.7.0
  • qunit v1.18.0
  • qunit v1.18.0

HTML test suite:

HTML测试套件:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>QUnit Tests Suite: asdf</title>
    <link rel="stylesheet" href="../components/libs/qunit/qunit/qunit.css">
  </head>
  <body>

    <div id="qunit"></div>
    <div id="qunit-fixture"></div>

    <script src="config.js"></script>
    <script data-main="unit" src="../components/libs/requirejs/require.js"></script>

  </body>
</html>

config.js

config.js

var requirejs = {
  baseUrl: "../",
  paths: {
    //{{app}}
    'foo': 'components/app/foo/foo',
    'test-foo': 'components/app/foo/test-foo',

    //{{libs}}
    'unit': 'qunit/unit',
    'qunit': 'components/libs/qunit/qunit/qunit',
    'jquery.exists': 'libs/jquery.exists/jquery.exists',
    'jquery': 'components/libs/jquery/dist/jquery.min'
  },
  'shim': {
    'jquery.exists': ['jquery']
  }
};

unit.js

unit.js

require([
  'qunit',
  'test-foo'
],
function(qunit, TestFoo) {
  TestFoo();
  qunit.start();
});

test-foo.js:

test-foo.js:

define(['jquery', 'qunit', 'foo'], function($, qunit, Foo) {
  'use strict';
  return function() {
    qunit.module("Foo");
    qunit.test("Foo Test", function() {
      equal(Foo.saySomething(), "Hello", "returns 'Hello'");
    });
  };
});

And finally the module I want to test:

最后我想测试的模块:

define(['jquery'], function($) {
  'use strict';
  var Foo = {
    saySomething: function() {
      return "Hello";
    }
  };
  return {
    saySomething: Foo.saySomething
  };
});

更多相关文章

  1. Node.js嬉皮API测试模块安装
  2. 如何测试从实时网站提取数据的AJAX应用程序?
  3. JavaScript中可见性检查的测试条件
  4. 如何测试一个点是否是二次Bézier曲线的一部分?
  5. chai-as-promised:单个测试中的多个期望语句
  6. 如何测试潜在的“浏览器崩溃”JavaScript?
  7. How to learn js properly(week4)使用js建立的动态测试网页
  8. Python PyV8安装测试(Win7)
  9. 如何让django芹菜写入测试数据库进行功能测试?

随机推荐

  1. Python爬虫自动化,帮小姐姐解放双手
  2. 同学,你这简历上没项目啊!
  3. 详解 Python 的二元算术运算,为什么说减法
  4. Python 为什么能支持任意的真值判断?
  5. 一个在交流群里讨论过两轮的问题,答案竟然
  6. Python 函数为什么会默认返回 None?
  7. 【软考高级知识点】第一章.信息化和信息
  8. 老板又出难题,气得我写了个自动化软件
  9. 详解增强算术赋值:“-=”操作是怎么实现的
  10. Python 为什么没有 void 关键字?