Name: js-handler/node_modules/nodeunit/test/test-runfiles.js
| 1: | var assert = require('assert'), |
| 2: | fs = require('fs'), |
| 3: | path = require('path'), |
| 4: | nodeunit = require('../lib/nodeunit'); |
| 5: | |
| 6: | |
| 7: | var setup = function (fn) { |
| 8: | return function (test) { |
| 9: | process.chdir(__dirname); |
| 10: | var env = { |
| 11: | mock_module1: require(__dirname + '/fixtures/mock_module1'), |
| 12: | mock_module2: require(__dirname + '/fixtures/mock_module2'), |
| 13: | mock_module3: require(__dirname + '/fixtures/dir/mock_module3'), |
| 14: | mock_module4: require(__dirname + '/fixtures/dir/mock_module4') |
| 15: | }; |
| 16: | fn.call(env, test); |
| 17: | }; |
| 18: | }; |
| 19: | |
| 20: | |
| 21: | exports.testRunFiles = setup(function (test) { |
| 22: | test.expect(24); |
| 23: | var runModule_copy = nodeunit.runModule; |
| 24: | |
| 25: | var runModule_calls = []; |
| 26: | var modules = []; |
| 27: | |
| 28: | var opts = { |
| 29: | moduleStart: function () { |
| 30: | return 'moduleStart'; |
| 31: | }, |
| 32: | testDone: function () { |
| 33: | return 'testDone'; |
| 34: | }, |
| 35: | testStart: function () { |
| 36: | return 'testStart'; |
| 37: | }, |
| 38: | log: function () { |
| 39: | return 'log'; |
| 40: | }, |
| 41: | done: function (assertions) { |
| 42: | test.equals(assertions.failures(), 0, 'failures'); |
| 43: | test.equals(assertions.length, 4, 'length'); |
| 44: | test.ok(typeof assertions.duration === "number"); |
| 45: | |
| 46: | var called_with = function (name) { |
| 47: | return runModule_calls.some(function (m) { |
| 48: | return m.name === name; |
| 49: | }); |
| 50: | }; |
| 51: | test.ok(called_with('mock_module1'), 'mock_module1 ran'); |
| 52: | test.ok(called_with('mock_module2'), 'mock_module2 ran'); |
| 53: | test.ok(called_with('mock_module3'), 'mock_module3 ran'); |
| 54: | test.ok(called_with('mock_module4'), 'mock_module4 ran'); |
| 55: | test.equals(runModule_calls.length, 4); |
| 56: | |
| 57: | nodeunit.runModule = runModule_copy; |
| 58: | test.done(); |
| 59: | } |
| 60: | }; |
| 61: | |
| 62: | nodeunit.runModule = function (name, mod, options, callback) { |
| 63: | test.equals(options.testDone, opts.testDone); |
| 64: | test.equals(options.testStart, opts.testStart); |
| 65: | test.equals(options.log, opts.log); |
| 66: | test.ok(typeof name === "string"); |
| 67: | runModule_calls.push(mod); |
| 68: | var m = [{failed: function () { |
| 69: | return false; |
| 70: | }}]; |
| 71: | modules.push(m); |
| 72: | callback(null, m); |
| 73: | }; |
| 74: | |
| 75: | nodeunit.runFiles( |
| 76: | [__dirname + '/fixtures/mock_module1.js', |
| 77: | __dirname + '/fixtures/mock_module2.js', |
| 78: | __dirname + '/fixtures/dir'], |
| 79: | opts |
| 80: | ); |
| 81: | }); |
| 82: | |
| 83: | exports.testRunFilesEmpty = function (test) { |
| 84: | test.expect(3); |
| 85: | nodeunit.runFiles([], { |
| 86: | moduleStart: function () { |
| 87: | test.ok(false, 'should not be called'); |
| 88: | }, |
| 89: | testDone: function () { |
| 90: | test.ok(false, 'should not be called'); |
| 91: | }, |
| 92: | testStart: function () { |
| 93: | test.ok(false, 'should not be called'); |
| 94: | }, |
| 95: | log: function () { |
| 96: | test.ok(false, 'should not be called'); |
| 97: | }, |
| 98: | done: function (assertions) { |
| 99: | test.equals(assertions.failures(), 0, 'failures'); |
| 100: | test.equals(assertions.length, 0, 'length'); |
| 101: | test.ok(typeof assertions.duration === "number"); |
| 102: | test.done(); |
| 103: | } |
| 104: | }); |
| 105: | }; |
| 106: | |
| 107: | |
| 108: | exports.testEmptyDir = function (test) { |
| 109: | var dir2 = __dirname + '/fixtures/dir2'; |
| 110: | |
| 111: | // git doesn't like empty directories, so we have to create one |
| 112: | path.exists(dir2, function (exists) { |
| 113: | if (!exists) { |
| 114: | fs.mkdirSync(dir2, 0777); |
| 115: | } |
| 116: | |
| 117: | // runFiles on empty directory: |
| 118: | nodeunit.runFiles([dir2], { |
| 119: | moduleStart: function () { |
| 120: | test.ok(false, 'should not be called'); |
| 121: | }, |
| 122: | testDone: function () { |
| 123: | test.ok(false, 'should not be called'); |
| 124: | }, |
| 125: | testStart: function () { |
| 126: | test.ok(false, 'should not be called'); |
| 127: | }, |
| 128: | log: function () { |
| 129: | test.ok(false, 'should not be called'); |
| 130: | }, |
| 131: | done: function (assertions) { |
| 132: | test.equals(assertions.failures(), 0, 'failures'); |
| 133: | test.equals(assertions.length, 0, 'length'); |
| 134: | test.ok(typeof assertions.duration === "number"); |
| 135: | test.done(); |
| 136: | } |
| 137: | }); |
| 138: | }); |
| 139: | }; |
| 140: | |
| 141: | |
| 142: | var CoffeeScript; |
| 143: | try { |
| 144: | CoffeeScript = require('coffee-script'); |
| 145: | } catch (e) { |
| 146: | } |
| 147: | |
| 148: | if (CoffeeScript) { |
| 149: | exports.testCoffeeScript = function (test) { |
| 150: | process.chdir(__dirname); |
| 151: | var env = { |
| 152: | mock_coffee_module: require(__dirname + |
| 153: | '/fixtures/coffee/mock_coffee_module') |
| 154: | }; |
| 155: | |
| 156: | test.expect(9); |
| 157: | var runModule_copy = nodeunit.runModule; |
| 158: | |
| 159: | var runModule_calls = []; |
| 160: | var modules = []; |
| 161: | |
| 162: | var opts = { |
| 163: | moduleStart: function () { |
| 164: | return 'moduleStart'; |
| 165: | }, |
| 166: | testDone: function () { |
| 167: | return 'testDone'; |
| 168: | }, |
| 169: | testStart: function () { |
| 170: | return 'testStart'; |
| 171: | }, |
| 172: | log: function () { |
| 173: | return 'log'; |
| 174: | }, |
| 175: | done: function (assertions) { |
| 176: | test.equals(assertions.failures(), 0, 'failures'); |
| 177: | test.equals(assertions.length, 1, 'length'); |
| 178: | test.ok(typeof assertions.duration === "number"); |
| 179: | |
| 180: | var called_with = function (name) { |
| 181: | return runModule_calls.some(function (m) { |
| 182: | return m.name === name; |
| 183: | }); |
| 184: | }; |
| 185: | test.ok( |
| 186: | called_with('mock_coffee_15'), |
| 187: | 'mock_coffee_module ran' |
| 188: | ); |
| 189: | test.equals(runModule_calls.length, 1); |
| 190: | |
| 191: | nodeunit.runModule = runModule_copy; |
| 192: | test.done(); |
| 193: | } |
| 194: | }; |
| 195: | |
| 196: | nodeunit.runModule = function (name, mod, options, callback) { |
| 197: | test.equals(options.testDone, opts.testDone); |
| 198: | test.equals(options.testStart, opts.testStart); |
| 199: | test.equals(options.log, opts.log); |
| 200: | test.ok(typeof name === "string"); |
| 201: | runModule_calls.push(mod); |
| 202: | var m = [{failed: function () { |
| 203: | return false; |
| 204: | }}]; |
| 205: | modules.push(m); |
| 206: | callback(null, m); |
| 207: | }; |
| 208: | |
| 209: | nodeunit.runFiles( |
| 210: | [__dirname + '/fixtures/coffee/mock_coffee_module.coffee'], |
| 211: | opts |
| 212: | ); |
| 213: | }; |
| 214: | } |
