phantom.js 2.89 KB
Newer Older
1
2
3
4
/*
 * grunt-contrib-qunit
 * http://gruntjs.com/
 *
5
 * Copyright (c) 2014 "Cowboy" Ben Alman, contributors
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Licensed under the MIT license.
 */

(function () {
  'use strict';

  // Don't re-order tests.
  QUnit.config.reorder = false
  // Run tests serially, not in parallel.
  QUnit.config.autorun = false

  // Send messages to the parent PhantomJS process via alert! Good times!!
  function sendMessage() {
    var args = [].slice.call(arguments)
    alert(JSON.stringify(args))
  }

  // These methods connect QUnit to PhantomJS.
24
  QUnit.log(function (obj) {
25
26
    // What is this I don’t even
    if (obj.message === '[object Object], undefined:undefined') { return }
27

28
    // Parse some stuff before sending it.
29
30
31
32
33
    var actual
    var expected
    if (!obj.result) {
      // Dumping large objects can be very slow, and the dump isn't used for
      // passing tests, so only dump if the test failed.
Chris Rebert's avatar
Chris Rebert committed
34
35
      actual = QUnit.dump.parse(obj.actual)
      expected = QUnit.dump.parse(obj.expected)
36
    }
37
38
    // Send it.
    sendMessage('qunit.log', obj.result, actual, expected, obj.message, obj.source)
39
  })
40

41
  QUnit.testStart(function (obj) {
42
    sendMessage('qunit.testStart', obj.name)
43
  })
44

45
46
47
  QUnit.testDone(function (obj) {
    sendMessage('qunit.testDone', obj.name, obj.failed, obj.passed, obj.total, obj.duration)
  })
48

49
  QUnit.moduleStart(function (obj) {
50
    sendMessage('qunit.moduleStart', obj.name)
51
  })
52

53
54
  QUnit.moduleDone(function (obj) {
    if (obj.failed === 0) {
fat's avatar
fat committed
55
      console.log('\r\u221A All tests passed in "' + obj.name + '" module')
56
    } else {
fat's avatar
fat committed
57
      console.log('\u00D7 ' + obj.failed + ' tests failed in "' + obj.name + '" module')
58
    }
59
60
    sendMessage('qunit.moduleDone', obj.name, obj.failed, obj.passed, obj.total)
  })
61

62
63
64
65
66
67
68
69
70
  QUnit.begin(function () {
    sendMessage('qunit.begin')
    console.log('\n\nStarting test suite')
    console.log('================================================\n')
  })

  QUnit.done(function (obj) {
    sendMessage('qunit.done', obj.failed, obj.passed, obj.total, obj.runtime)
  })
71
72

}())
73
74
75
76
77
78


// bind polyfill
// shoutout mdn: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Polyfill

if (!Function.prototype.bind) {
Jacob Thornton's avatar
Jacob Thornton committed
79
  Function.prototype.bind = function (oThis) {
80
81
82
83
84
85
    if (typeof this !== 'function') {
      // closest thing possible to the ECMAScript 5
      // internal IsCallable function
      throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
    }

Jacob Thornton's avatar
Jacob Thornton committed
86
87
88
89
90
91
    var aArgs   = Array.prototype.slice.call(arguments, 1)
    var fToBind = this
    var FNOP    = function () {}
    var fBound  = function () {
      return fToBind.apply(this instanceof FNOP ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)))
    }
92
93
94

    if (this.prototype) {
      // native functions don't have a prototype
Jacob Thornton's avatar
Jacob Thornton committed
95
      FNOP.prototype = this.prototype
96
    }
Jacob Thornton's avatar
Jacob Thornton committed
97
    fBound.prototype = new FNOP()
98

Jacob Thornton's avatar
Jacob Thornton committed
99
100
    return fBound
  }
101
}