github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/triage/node_modules/jasmine/lib/jasmine.js (about)

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