Name: js-handler/node_modules/nodeunit/test/test-testcase.js
| 1: | /* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS! |
| 2: | * You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build. |
| 3: | * Only code on that line will be removed, its mostly to avoid requiring code |
| 4: | * that is node specific |
| 5: | */ |
| 6: | |
| 7: | var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER |
| 8: | |
| 9: | exports.testTestCase = function (test) { |
| 10: | test.expect(7); |
| 11: | var call_order = []; |
| 12: | var s = { |
| 13: | setUp: function (callback) { |
| 14: | call_order.push('setUp'); |
| 15: | test.equals(this.one, undefined, 'in setUp, this.one not set'); |
| 16: | this.one = 1; |
| 17: | callback(); |
| 18: | }, |
| 19: | tearDown: function (callback) { |
| 20: | call_order.push('tearDown'); |
| 21: | test.ok(true, 'tearDown called'); |
| 22: | callback(); |
| 23: | }, |
| 24: | test1: function (t) { |
| 25: | call_order.push('test1'); |
| 26: | test.equals(this.one, 1, 'in test1, this.one is 1'); |
| 27: | this.one = 2; |
| 28: | t.done(); |
| 29: | }, |
| 30: | test2: function (t) { |
| 31: | call_order.push('test2'); |
| 32: | test.equals(this.one, 1, 'in test2, this.one is still 1'); |
| 33: | t.done(); |
| 34: | } |
| 35: | }; |
| 36: | nodeunit.runSuite(null, s, {}, function () { |
| 37: | test.same(call_order, [ |
| 38: | 'setUp', 'test1', 'tearDown', |
| 39: | 'setUp', 'test2', 'tearDown' |
| 40: | ]); |
| 41: | test.done(); |
| 42: | }); |
| 43: | }; |
| 44: | |
| 45: | exports.tearDownAfterError = function (test) { |
| 46: | test.expect(1); |
| 47: | var s = { |
| 48: | tearDown: function (callback) { |
| 49: | test.ok(true, 'tearDown called'); |
| 50: | callback(); |
| 51: | }, |
| 52: | test: function (t) { |
| 53: | throw new Error('some error'); |
| 54: | } |
| 55: | }; |
| 56: | nodeunit.runSuite(null, s, {}, function () { |
| 57: | test.done(); |
| 58: | }); |
| 59: | }; |
| 60: | |
| 61: | exports.catchSetUpError = function (test) { |
| 62: | test.expect(2); |
| 63: | var test_error = new Error('test error'); |
| 64: | var s = { |
| 65: | setUp: function (callback) { |
| 66: | throw test_error; |
| 67: | }, |
| 68: | test: function (t) { |
| 69: | test.ok(false, 'test function should not be called'); |
| 70: | t.done(); |
| 71: | } |
| 72: | }; |
| 73: | nodeunit.runSuite(null, s, {}, function (err, assertions) { |
| 74: | test.equal(assertions.length, 1); |
| 75: | test.equal(assertions[0].error, test_error); |
| 76: | test.done(); |
| 77: | }); |
| 78: | }; |
| 79: | |
| 80: | exports.setUpErrorCallback = function (test) { |
| 81: | test.expect(2); |
| 82: | var test_error = new Error('test error'); |
| 83: | var s = { |
| 84: | setUp: function (callback) { |
| 85: | callback(test_error); |
| 86: | }, |
| 87: | test: function (t) { |
| 88: | test.ok(false, 'test function should not be called'); |
| 89: | t.done(); |
| 90: | } |
| 91: | }; |
| 92: | nodeunit.runSuite(null, s, {}, function (err, assertions) { |
| 93: | test.equal(assertions.length, 1); |
| 94: | test.equal(assertions[0].error, test_error); |
| 95: | test.done(); |
| 96: | }); |
| 97: | }; |
| 98: | |
| 99: | exports.catchTearDownError = function (test) { |
| 100: | test.expect(2); |
| 101: | var test_error = new Error('test error'); |
| 102: | var s = { |
| 103: | tearDown: function (callback) { |
| 104: | throw test_error; |
| 105: | }, |
| 106: | test: function (t) { |
| 107: | t.done(); |
| 108: | } |
| 109: | }; |
| 110: | nodeunit.runSuite(null, s, {}, function (err, assertions) { |
| 111: | test.equal(assertions.length, 1); |
| 112: | test.equal(assertions[0].error, test_error); |
| 113: | test.done(); |
| 114: | }); |
| 115: | }; |
| 116: | |
| 117: | exports.tearDownErrorCallback = function (test) { |
| 118: | test.expect(2); |
| 119: | var test_error = new Error('test error'); |
| 120: | var s = { |
| 121: | tearDown: function (callback) { |
| 122: | callback(test_error); |
| 123: | }, |
| 124: | test: function (t) { |
| 125: | t.done(); |
| 126: | } |
| 127: | }; |
| 128: | nodeunit.runSuite(null, s, {}, function (err, assertions) { |
| 129: | test.equal(assertions.length, 1); |
| 130: | test.equal(assertions[0].error, test_error); |
| 131: | test.done(); |
| 132: | }); |
| 133: | }; |
| 134: | |
| 135: | exports.testErrorAndtearDownError = function (test) { |
| 136: | test.expect(3); |
| 137: | var error1 = new Error('test error one'); |
| 138: | var error2 = new Error('test error two'); |
| 139: | var s = { |
| 140: | tearDown: function (callback) { |
| 141: | callback(error2); |
| 142: | }, |
| 143: | test: function (t) { |
| 144: | t.done(error1); |
| 145: | } |
| 146: | }; |
| 147: | nodeunit.runSuite(null, s, {}, function (err, assertions) { |
| 148: | test.equal(assertions.length, 2); |
| 149: | test.equal(assertions[0].error, error1); |
| 150: | test.equal(assertions[1].error, error2); |
| 151: | test.done(); |
| 152: | }); |
| 153: | }; |
| 154: | |
| 155: | exports.testCaseGroups = function (test) { |
| 156: | var call_order = []; |
| 157: | var s = { |
| 158: | setUp: function (callback) { |
| 159: | call_order.push('setUp'); |
| 160: | callback(); |
| 161: | }, |
| 162: | tearDown: function (callback) { |
| 163: | call_order.push('tearDown'); |
| 164: | callback(); |
| 165: | }, |
| 166: | test1: function (test) { |
| 167: | call_order.push('test1'); |
| 168: | test.done(); |
| 169: | }, |
| 170: | group1: { |
| 171: | test2: function (test) { |
| 172: | call_order.push('group1.test2'); |
| 173: | test.done(); |
| 174: | } |
| 175: | } |
| 176: | }; |
| 177: | nodeunit.runSuite(null, s, {}, function (err, assertions) { |
| 178: | test.same(call_order, [ |
| 179: | 'setUp', |
| 180: | 'test1', |
| 181: | 'tearDown', |
| 182: | 'setUp', |
| 183: | 'group1.test2', |
| 184: | 'tearDown' |
| 185: | ]); |
| 186: | test.done(); |
| 187: | }); |
| 188: | }; |
| 189: | |
| 190: | exports.nestedTestCases = function (test) { |
| 191: | var call_order = []; |
| 192: | var s = { |
| 193: | setUp: function (callback) { |
| 194: | call_order.push('setUp'); |
| 195: | callback(); |
| 196: | }, |
| 197: | tearDown: function (callback) { |
| 198: | call_order.push('tearDown'); |
| 199: | callback(); |
| 200: | }, |
| 201: | test1: function (test) { |
| 202: | call_order.push('test1'); |
| 203: | test.done(); |
| 204: | }, |
| 205: | group1: { |
| 206: | setUp: function (callback) { |
| 207: | call_order.push('group1.setUp'); |
| 208: | callback(); |
| 209: | }, |
| 210: | tearDown: function (callback) { |
| 211: | call_order.push('group1.tearDown'); |
| 212: | callback(); |
| 213: | }, |
| 214: | test2: function (test) { |
| 215: | call_order.push('group1.test2'); |
| 216: | test.done(); |
| 217: | } |
| 218: | } |
| 219: | }; |
| 220: | nodeunit.runSuite(null, s, {}, function (err, assertions) { |
| 221: | test.same(call_order, [ |
| 222: | 'setUp', |
| 223: | 'test1', |
| 224: | 'tearDown', |
| 225: | 'setUp', |
| 226: | 'group1.setUp', |
| 227: | 'group1.test2', |
| 228: | 'group1.tearDown', |
| 229: | 'tearDown' |
| 230: | ]); |
| 231: | test.done(); |
| 232: | }); |
| 233: | }; |
| 234: | |
| 235: | exports.deepNestedTestCases = function (test) { |
| 236: | var val = 'foo'; |
| 237: | var s = { |
| 238: | setUp: function (callback) { |
| 239: | val = 'bar'; |
| 240: | callback(); |
| 241: | }, |
| 242: | group1: { |
| 243: | test: { |
| 244: | test2: function (test) { |
| 245: | test.equal(val, 'bar'); |
| 246: | test.done(); |
| 247: | } |
| 248: | } |
| 249: | } |
| 250: | }; |
| 251: | nodeunit.runSuite(null, s, {}, function (err, assertions) { |
| 252: | test.ok(!assertions[0].failed()); |
| 253: | test.equal(assertions.length, 1); |
| 254: | test.done(); |
| 255: | }); |
| 256: | }; |
