jasmine.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. var path = require('path'),
  2. util = require('util'),
  3. glob = require('glob'),
  4. exit = require('./exit'),
  5. CompletionReporter = require('./reporters/completion_reporter'),
  6. ConsoleSpecFilter = require('./filters/console_spec_filter');
  7. module.exports = Jasmine;
  8. module.exports.ConsoleReporter = require('./reporters/console_reporter');
  9. function Jasmine(options) {
  10. options = options || {};
  11. var jasmineCore = options.jasmineCore || require('jasmine-core');
  12. this.jasmineCorePath = path.join(jasmineCore.files.path, 'jasmine.js');
  13. this.jasmine = jasmineCore.boot(jasmineCore);
  14. this.projectBaseDir = options.projectBaseDir || path.resolve();
  15. this.printDeprecation = options.printDeprecation || require('./printDeprecation');
  16. this.specDir = '';
  17. this.specFiles = [];
  18. this.helperFiles = [];
  19. this.env = this.jasmine.getEnv();
  20. this.reportersCount = 0;
  21. this.completionReporter = new CompletionReporter();
  22. this.onCompleteCallbackAdded = false;
  23. this.exit = exit;
  24. this.showingColors = true;
  25. this.reporter = new module.exports.ConsoleReporter();
  26. this.addReporter(this.reporter);
  27. this.defaultReporterConfigured = false;
  28. var jasmineRunner = this;
  29. this.completionReporter.onComplete(function(passed) {
  30. jasmineRunner.exitCodeCompletion(passed);
  31. });
  32. this.checkExit = checkExit(this);
  33. this.coreVersion = function() {
  34. return jasmineCore.version();
  35. };
  36. }
  37. Jasmine.prototype.randomizeTests = function(value) {
  38. this.env.randomizeTests(value);
  39. };
  40. Jasmine.prototype.seed = function(value) {
  41. this.env.seed(value);
  42. };
  43. Jasmine.prototype.showColors = function(value) {
  44. this.showingColors = value;
  45. };
  46. Jasmine.prototype.addSpecFile = function(filePath) {
  47. this.specFiles.push(filePath);
  48. };
  49. Jasmine.prototype.addReporter = function(reporter) {
  50. this.env.addReporter(reporter);
  51. this.reportersCount++;
  52. };
  53. Jasmine.prototype.clearReporters = function() {
  54. this.env.clearReporters();
  55. this.reportersCount = 0;
  56. };
  57. Jasmine.prototype.provideFallbackReporter = function(reporter) {
  58. this.env.provideFallbackReporter(reporter);
  59. };
  60. Jasmine.prototype.configureDefaultReporter = function(options) {
  61. options.timer = options.timer || new this.jasmine.Timer();
  62. options.print = options.print || function() {
  63. process.stdout.write(util.format.apply(this, arguments));
  64. };
  65. options.showColors = options.hasOwnProperty('showColors') ? options.showColors : true;
  66. options.jasmineCorePath = options.jasmineCorePath || this.jasmineCorePath;
  67. if(options.onComplete) {
  68. this.printDeprecation('Passing in an onComplete function to configureDefaultReporter is deprecated.');
  69. }
  70. this.reporter.setOptions(options);
  71. this.defaultReporterConfigured = true;
  72. };
  73. Jasmine.prototype.addMatchers = function(matchers) {
  74. this.jasmine.Expectation.addMatchers(matchers);
  75. };
  76. Jasmine.prototype.loadSpecs = function() {
  77. this.specFiles.forEach(function(file) {
  78. require(file);
  79. });
  80. };
  81. Jasmine.prototype.loadHelpers = function() {
  82. this.helperFiles.forEach(function(file) {
  83. require(file);
  84. });
  85. };
  86. Jasmine.prototype.loadConfigFile = function(configFilePath) {
  87. try {
  88. var absoluteConfigFilePath = path.resolve(this.projectBaseDir, configFilePath || 'spec/support/jasmine.json');
  89. var config = require(absoluteConfigFilePath);
  90. this.loadConfig(config);
  91. } catch (e) {
  92. if(configFilePath || e.code != 'MODULE_NOT_FOUND') { throw e; }
  93. }
  94. };
  95. Jasmine.prototype.loadConfig = function(config) {
  96. this.specDir = config.spec_dir || this.specDir;
  97. this.env.throwOnExpectationFailure(config.stopSpecOnExpectationFailure);
  98. this.env.randomizeTests(config.random);
  99. if(config.helpers) {
  100. this.addHelperFiles(config.helpers);
  101. }
  102. if(config.spec_files) {
  103. this.addSpecFiles(config.spec_files);
  104. }
  105. };
  106. Jasmine.prototype.addHelperFiles = addFiles('helperFiles');
  107. Jasmine.prototype.addSpecFiles = addFiles('specFiles');
  108. function addFiles(kind) {
  109. return function (files) {
  110. var jasmineRunner = this;
  111. var fileArr = this[kind];
  112. files.forEach(function(file) {
  113. if(!(path.isAbsolute && path.isAbsolute(file))) {
  114. file = path.join(jasmineRunner.projectBaseDir, jasmineRunner.specDir, file);
  115. }
  116. var filePaths = glob.sync(file);
  117. filePaths.forEach(function(filePath) {
  118. if(fileArr.indexOf(filePath) === -1) {
  119. fileArr.push(filePath);
  120. }
  121. });
  122. });
  123. };
  124. }
  125. Jasmine.prototype.onComplete = function(onCompleteCallback) {
  126. this.completionReporter.onComplete(onCompleteCallback);
  127. };
  128. Jasmine.prototype.stopSpecOnExpectationFailure = function(value) {
  129. this.env.throwOnExpectationFailure(value);
  130. };
  131. Jasmine.prototype.exitCodeCompletion = function(passed) {
  132. if(passed) {
  133. this.exit(0, process.platform, process.version, process.exit, require('exit'));
  134. }
  135. else {
  136. this.exit(1, process.platform, process.version, process.exit, require('exit'));
  137. }
  138. };
  139. var checkExit = function(jasmineRunner) {
  140. return function() {
  141. if (!jasmineRunner.completionReporter.isComplete()) {
  142. process.exitCode = 4;
  143. }
  144. };
  145. };
  146. Jasmine.prototype.execute = function(files, filterString) {
  147. process.on('exit', this.checkExit);
  148. this.loadHelpers();
  149. if (!this.defaultReporterConfigured) {
  150. this.configureDefaultReporter({ showColors: this.showingColors });
  151. }
  152. if(filterString) {
  153. var specFilter = new ConsoleSpecFilter({
  154. filterString: filterString
  155. });
  156. this.env.specFilter = function(spec) {
  157. return specFilter.matches(spec.getFullName());
  158. };
  159. }
  160. if (files && files.length > 0) {
  161. this.specDir = '';
  162. this.specFiles = [];
  163. this.addSpecFiles(files);
  164. }
  165. this.loadSpecs();
  166. this.addReporter(this.completionReporter);
  167. this.env.execute();
  168. };