File: test-failing-callbacks.js

package info (click to toggle)
node-nodeunit 0.11.3%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,076 kB
  • sloc: javascript: 3,323; makefile: 141
file content (114 lines) | stat: -rw-r--r-- 3,614 bytes parent folder | download | duplicates (5)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var nodeunit = require('../lib/nodeunit');


exports.testFailingLog = function (test) {
    test.expect(3);

    // this is meant to bubble to the top, and will be ignored for the purposes
    // of testing:
    var ignored_error = new Error('ignore this callback error');
    var err_handler = function (err) {
        if (err && err.message !== ignored_error.message) {
            throw err;
        }
    };
    process.addListener('uncaughtException', err_handler);

    // A failing callback should not affect the test outcome
    var testfn = function (test) {
        test.ok(true, 'test.ok');
        test.done();
    };
    nodeunit.runTest('testname', testfn, {
        log: function (assertion) {
            test.ok(true, 'log called');
            throw ignored_error;
        },
        testDone: function (name, assertions) {
            test.equals(assertions.failures(), 0, 'failures');
            test.equals(assertions.length, 1, 'total');
            process.removeListener('uncaughtException', err_handler);
        }
    }, test.done);
};

exports.testFailingTestDone = function (test) {
    test.expect(2);

    var ignored_error = new Error('ignore this callback error');
    var err_handler = function (err) {
        if (err && err.message !== ignored_error.message) {
            throw err;
        }
    };
    process.addListener('uncaughtException', err_handler);

    // A failing callback should not affect the test outcome
    var testfn = function (test) {
        test.done();
    };
    nodeunit.runTest('testname', testfn, {
        log: function (assertion) {
            test.ok(false, 'log should not be called');
        },
        testDone: function (name, assertions) {
            test.equals(assertions.failures(), 0, 'failures');
            test.equals(assertions.length, 0, 'total');
            process.nextTick(function () {
                process.removeListener('uncaughtException', err_handler);
                test.done();
            });
            throw ignored_error;
        }
    }, function () {});
};

exports.testAssertionObj = function (test) {
    test.expect(4);
    var testfn = function (test) {
        test.ok(true, 'ok true');
        test.done();
    };
    nodeunit.runTest('testname', testfn, {
        log: function (assertion) {
            test.ok(assertion.passed() === true, 'assertion.passed');
            test.ok(assertion.failed() === false, 'assertion.failed');
        },
        testDone: function (name, assertions) {
            test.equals(assertions.failures(), 0, 'failures');
            test.equals(assertions.length, 1, 'total');
        }
    }, test.done);
};

exports.testLogOptional = function (test) {
    test.expect(2);
    var testfn = function (test) {
        test.ok(true, 'ok true');
        test.done();
    };
    nodeunit.runTest('testname', testfn, {
        testDone: function (name, assertions) {
            test.equals(assertions.failures(), 0, 'failures');
            test.equals(assertions.length, 1, 'total');
        }
    }, test.done);
};

exports.testExpectWithFailure = function (test) {
    test.expect(3);
    var testfn = function (test) {
        test.expect(1);
        test.ok(false, 'test.ok');
        test.done();
    };
    nodeunit.runTest('testname', testfn, {
        log: function (assertion) {
            test.equals(assertion.method, 'ok', 'assertion.method');
        },
        testDone: function (name, assertions) {
            test.equals(assertions.failures(), 1, 'failures');
            test.equals(assertions.length, 1, 'total');
        }
    }, test.done);
};