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