github.com/SAP/cloud-mta-build-tool@v1.2.27/internal/exec/testdata/mta/node-js/gulpfile.js (about)

     1  /**
     2   * This is the gulp script to run jasmine tests with coverage analysis using istanbul.
     3   * For more information, please see also:
     4   * @see {@link http://jasmine.github.io/2.0/introduction.html}
     5   * @see {@link https://www.npmjs.com/package/istanbul}
     6   * @see {@link https://www.npmjs.com/package/gulp}
     7   * @see {@link https://www.npmjs.com/package/gulp-istanbul}
     8   * @see {@link https://www.npmjs.com/package/gulp-jasmine}
     9   * @see {@link https://www.npmjs.com/package/jasmine-reporters}
    10   * @see {@link https://www.npmjs.com/package/glob}
    11   **/
    12  
    13  var path = require("path");
    14  
    15  var gulp = require("gulp");
    16  var jasmine = require("gulp-jasmine");
    17  var istanbul = require("gulp-istanbul");
    18  var reporters = require("jasmine-reporters");
    19  
    20  // required to replace absolute paths in results with relative ones
    21  var replace = require("gulp-replace");
    22  
    23  // configuration of the test coverage, uses glob syntax
    24  // @see {@link https://www.npmjs.com/package/glob}
    25  
    26  // the following files are included in the coverage analysis
    27  // include all javascript files and exclude myExclude.js
    28  // replace or remove files depending on what is to be excluded or included in addition
    29  var includedScripts = ["**/*.js", "!myExclude.js"];
    30  
    31  // the following files are part of the framework and are to be excluded from the test coverage
    32  var defaultExclusion = ["!**/*spec.js", "!rungulp.js", "!gulpfile.js", "!**/node_modules/**", "!appcontroller.*/**", "!vendor/**"];
    33  
    34  // test results folder for test view history
    35  // assign each test run a unique timestamp, coverage and test results
    36  // are associated via timestamp, so the test results folder fills up with files
    37  // of form:
    38  // 123456_report.xml
    39  // 123456_coverage.json
    40  // 456789_report.xml
    41  // 456789_coverage.json
    42  // ...
    43  var testResultsDir = path.join(__dirname, ".testresults");
    44  
    45  var timestamp = Date.now();
    46  var testResultFile = timestamp + "_report.xml";
    47  var coverageResultFile = timestamp + "_coverage.json";
    48  
    49  /**
    50   * Instrument the test/productive code
    51   */
    52  gulp.task("instrumentation", function() {
    53      return gulp.src(includedScripts.concat(defaultExclusion))
    54      // Covering files
    55          .pipe(istanbul({
    56              includeUntested: true // instrument all files
    57          }))
    58          // Force `require` to return covered files
    59          .pipe(istanbul.hookRequire());
    60  });
    61  
    62  /**
    63   * Execute tests with coverage information, requires instrumentation
    64   * before tests are executed
    65   */
    66  gulp.task("jasmine-istanbul", ["instrumentation"], function() {
    67      // run all tests ending with "spec", skip all tests that are part of the node_modules
    68      return gulp.src(["**/*spec.js", "!**/node_modules/**"])
    69          .pipe(jasmine({
    70              errorOnFail: false,
    71              // use the standard junit xml reporter
    72              reporter: new reporters.JUnitXmlReporter({
    73                  savePath: testResultsDir,
    74                  filePrefix: testResultFile,
    75                  consolidateAll: true
    76              })
    77          }))
    78          .pipe(istanbul.writeReports({
    79              // generage json report for the coverage
    80              reporters: ["json"],
    81              reportOpts: {
    82                  json: {
    83                      dir: testResultsDir,
    84                      file: coverageResultFile
    85                  }
    86              }
    87          }));
    88  });
    89  
    90  /**
    91   * Execute tests without coverage information
    92   */
    93  gulp.task("jasmine", function() {
    94      // run all tests ending with "spec", skip all tests that are part of the node_modules
    95      return gulp.src(["**/*spec.js", "!**/node_modules/**"])
    96          .pipe(jasmine({
    97              errorOnFail: false,
    98              // use the standard junit xml reporter
    99              reporter: new reporters.JUnitXmlReporter({
   100                  savePath: testResultsDir,
   101                  filePrefix: testResultFile,
   102                  consolidateAll: true
   103              })
   104          }));
   105  });
   106  
   107  /**
   108   * Remove absolute path portions so test view can open the referenced files
   109   */
   110  function postProcessing() {
   111      return gulp.src([
   112          path.join(testResultsDir, testResultFile),
   113          path.join(testResultsDir, coverageResultFile)
   114      ], {
   115          dot: true
   116      })
   117          .pipe(replace(__dirname, ""))
   118          .pipe(gulp.dest(testResultsDir));
   119  }
   120  
   121  // run tests with coverage analysis
   122  gulp.task("test-coverage",  ["jasmine-istanbul"], function() {
   123      return postProcessing();
   124  });
   125  
   126  // only run the tests, skip test coverage analysis
   127  gulp.task("test", ["jasmine"], function() {
   128      return postProcessing();
   129  });
   130  
   131  //the default task is to run tests with coverage analysis
   132  gulp.task("default", ["test-coverage"]);