File: test.js

package info (click to toggle)
node-test 0.6.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 220 kB
  • sloc: makefile: 12; sh: 12
file content (93 lines) | stat: -rw-r--r-- 2,775 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"use strict";

var Assert = require("./assert").Assert
var Logger = require("./logger").Logger


var ERR_COMPLETED_ASSERT = "Assert in completed test"
var ERR_COMPLETED_COMPLETE = "Attemt to complete test more then one times"
var ERR_EXPECT = "AssertionError"


/**
 * Creates a test function.
 */
function Test(name, unit, logger, Assert) {
  var isSync = unit.length <= 1
  var isFailFast = !unit.length
  var isDone = false
  return function test(next) {
    logger = logger.section(name)
    var assert = Assert(logger)
    assert.end = function end() {
      if (isDone) return logger.error(Error(ERR_COMPLETED_COMPLETE))
      isDone = true
      next()
    }

    try {
      var result = unit(assert, assert.end)
      // If it"s async test that returns a promise.
      if (result && typeof(result.then) === "function") {
        result.then(function passed() {
          logger.pass("passed")
          assert.end()
        }, function failed(reason) {
          logger.fail(reason)
          assert.end()
        })
      } else {
        if (isFailFast) logger.pass("passed")
        if (isSync) assert.end()
      }
    } catch (exception) {
      if (exception.name && exception.name.startsWith(ERR_EXPECT)) assert.fail(exception)
      else logger.error(exception)
      assert.end()
    }
  }
}

function isTest(name) { return name.indexOf("test") === 0 }

/**
 * Creates a test suite / group. Calling returned function will execute
 * all test in the given suite.
 */
function Suite(name, units, logger, Assert) {
  // Collecting properties that represent test functions or suits.
  var names = Object.keys(units).filter(isTest)
  Assert = units.Assert || Assert
  // Returning a function that executes all test in this suite and all it"s
  // sub-suits.
  return function suite(end) {
    // Chaining test / suits so that each is executed after last is done.
    function next() {
      if (!names.length) return end()
      var name = names.shift()
      var unit = Unit(name, units[name], logger, units.Assert || Assert)
      unit(next)
    }
    next((logger = logger.section(name)))
  }
}
function Unit(name, units, logger, Assert) {
  return typeof(units) === "function" ? Test(name, units, logger, Assert)
                                      : Suite(name, units, logger, Assert)
}


/**
 * Test runner function.
 */
exports.run = function run(units, logger) {
  var exit = logger ? false : true
  logger = logger || new Logger()
  var unit = Unit("Running all tests:", units, logger, Assert)
  unit(function done() {
    logger.report()
    var failed = logger.errors.length !== 0 || logger.fails.length !== 0
    // Exit only if `process.exit` exist and if no logger was provided.
    if (exit && process.exit) process.exit(failed ? 1 : 0)
  })
}