From ced9ce2a8d7bad53cf82ee123225716ef445cfce Mon Sep 17 00:00:00 2001
From: Brian Ingenito <28159742+bingenito@users.noreply.github.com>
Date: Tue, 9 Jul 2019 15:28:53 +0000
Subject: [PATCH] Strip 'build' dir from source maps in coverage file

---
 .gulp/tasks/tests.js | 60 ++++++++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 24 deletions(-)

diff --git a/.gulp/tasks/tests.js b/.gulp/tasks/tests.js
index ed9ca5e1..523c9359 100644
--- a/.gulp/tasks/tests.js
+++ b/.gulp/tasks/tests.js
@@ -2,6 +2,7 @@
 
 var jasmine = require('gulp-jasmine'),
     istanbul = require('gulp-istanbul'),
+    replace = require('gulp-replace'),
     remap = require('remap-istanbul/lib/gulpRemapIstanbul'),
     instanbulEnforcer = require('gulp-istanbul-enforcer');
 
@@ -10,38 +11,49 @@ module.exports = function (gulp, config) {
         return gulp.src(config.test.coverage.src)
             .pipe(istanbul({ includeUntested: true }))
             .pipe(istanbul.hookRequire())
-            .on('finish', function () {
+            .on('finish', () => {
+
+                // Run jasmine under istanbul
                 gulp.src(config.test.src)
                     .pipe(jasmine({ verbose: true, errorOnFail: true, includeStackTrace: false }))
                     .pipe(istanbul.writeReports({
                         dir: config.test.coverage.dest,
                         reporters: ['json']
                     }))
-                    //.on('end', remapCoverageFiles)
-                    .on('finish', function () {
-                        gulp.src('.')
-                            .pipe(instanbulEnforcer({
-                                coverageDirectory: config.test.coverage.dest,
-                                rootDirectory: '',
-                                thresholds: {
-                                    statements: config.test.coverage.threshold || 80
+
+                    // Take js coverage json and remap to typescript.  Output html and text
+                    .on('end', () => {
+                        return gulp.src(config.test.coverage.coverageFile)
+                            .pipe(remap({
+                                reports: {
+                                    'json': config.test.coverage.coverageFile, // overwrite js based json with ts remapped version
+                                    'html': config.test.coverage.dest,
+                                    'lcovonly': config.test.coverage.lcovFile,
+                                    'text': null
                                 }
-                            }));
-                    });
-            });
+                            }))
 
-        /** Take js coverage json and remap to typescript.  Output html and text */
-        function remapCoverageFiles() {
-            return gulp.src(config.test.coverage.coverageFile)
-                .pipe(remap({
-                    reports: {
-                        'json': config.test.coverage.coverageFile, // overwrite js based json with ts remapped version
-                        'html': config.test.coverage.dest,
-                        'lcovonly': config.test.coverage.lcovFile,
-                        'text': null
-                    }
-                }));
-        };
+                            // Remove staging build directory in code coverage for correct file mapping
+                            .on('finish', function () {
+                                return gulp.src(config.test.coverage.coverageFile)
+                                    .pipe(replace(/\\\\build\\\\src/mg, '\\\\src')) // windows
+                                    .pipe(replace(/\/build\/src/mg, '/src')) // unix)
+                                    .pipe(gulp.dest(config.test.coverage.dest))
+
+                                    // Enforce statement coverage threshold based on config or if not defined 80%
+                                    .on('finish', function () {
+                                        return gulp.src('.')
+                                            .pipe(instanbulEnforcer({
+                                                coverageDirectory: config.test.coverage.dest,
+                                                rootDirectory: '',
+                                                thresholds: {
+                                                    statements: config.test.coverage.threshold || 80
+                                                }
+                                            }));
+                                    });
+                            })
+                    })
+            });
     };
 }