Generate code coverage with Istanbul
authorDan Vanderkam <danvdk@gmail.com>
Fri, 23 Oct 2015 23:20:54 +0000 (19:20 -0400)
committerDan Vanderkam <danvdk@gmail.com>
Sun, 25 Oct 2015 02:44:32 +0000 (22:44 -0400)
.gitignore
.travis.yml
auto_tests/chrome.karma.conf.js [deleted file]
auto_tests/coverage.html [new file with mode: 0644]
auto_tests/karma.conf.js [deleted file]
auto_tests/runner.html
package.json
scripts/generate-coverage.sh [new file with mode: 0755]
src/dygraph-combined.js [deleted file]

index 01b10ac..b904bed 100644 (file)
@@ -3,3 +3,4 @@ docs/options.html
 node_modules
 env
 dist
+coverage
index a2ac109..a16a7b0 100644 (file)
@@ -7,5 +7,6 @@ script: >
     npm run build &&
     npm run build-tests &&
     npm run test &&
+    npm run coverage &&
     curl -O https://raw.githubusercontent.com/danvk/travis-weigh-in/master/weigh_in.py &&
     python weigh_in.py dist/dygraph-combined.js
diff --git a/auto_tests/chrome.karma.conf.js b/auto_tests/chrome.karma.conf.js
deleted file mode 100644 (file)
index 0caceb6..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-module.exports = function (config) {
-    config.set({
-        basePath: '../',
-        frameworks: [
-          'mocha',
-          'chai'
-        ],
-        files: [
-            'dist/dygraph-combined-dev.js',
-            'src/extras/smooth-plotter.js',
-            'src/extras/synchronizer.js',
-            'auto_tests/data/*.js',
-            'auto_tests/tests/*.js',
-        ],
-        autoWatch: true,
-        singleRun: false,
-        reporters: ['mocha'],  // or 'dots', 'mocha', 'spec'
-        browsers: ['Chrome'],  // or 'Firefox', 'Safari', etc.
-        plugins: [
-            'karma-mocha',
-            'karma-chai-plugins',
-            'karma-chrome-launcher',
-            'karma-firefox-launcher',
-            'karma-spec-reporter',
-            'karma-mocha-reporter'
-        ]
-    });
-};
diff --git a/auto_tests/coverage.html b/auto_tests/coverage.html
new file mode 100644 (file)
index 0000000..0457347
--- /dev/null
@@ -0,0 +1,66 @@
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>dygraphs tests</title>
+  <link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
+</head>
+<body>
+  <div id="graph"></div>
+  <div id="mocha"></div>
+
+  <!-- Polyfills for PhantomJS -->
+  <script src="../node_modules/babel-core/browser-polyfill.js"></script>
+
+  <!-- Mocha -->
+  <script src="../node_modules/mocha/mocha.js"></script>
+  <script src="../node_modules/chai/chai.js"></script>
+  <script>
+  if (window.initMochaPhantomJS) {
+    window.initMochaPhantomJS();
+  }
+  </script>
+
+  <script>
+  var expect = chai.expect;
+  var assert = chai.assert;
+  function cleanupAfterEach() {
+    // Clean up the DOM before each test.
+    // This makes debugging easier than cleaning up after each test, since the
+    // DOM stays on the screen after a failure.
+    beforeEach(function() {
+      var graph = document.getElementById('graph');
+      graph.innerHTML = '';
+      graph.setAttribute('style', '');
+    });
+  }
+  function useProxyCanvas(utils, Proxy) {
+    var _origFunc = utils.getContext;
+    beforeEach(function() {
+      utils.getContext = function(canvas) {
+        return new Proxy(_origFunc(canvas));
+      }
+    });
+
+    afterEach(function() {
+      utils.getContext = _origFunc;
+    });
+  }
+  </script>
+  <script>mocha.setup('bdd')</script>
+
+  <!-- Test data -->
+  <script src="data/data.js"></script>
+
+  <!-- Bundled tests -->
+  <script src="../coverage/tests.js"></script>
+
+  <script>
+    mocha.checkLeaks();
+    if (window.mochaPhantomJS) {
+      mochaPhantomJS.run();
+    } else {
+      mocha.run();
+    }
+  </script>
+</body>
+</html>
diff --git a/auto_tests/karma.conf.js b/auto_tests/karma.conf.js
deleted file mode 100644 (file)
index 09d0596..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-module.exports = function (config) {
-    config.set({
-        basePath: '../',
-        frameworks: [
-          'mocha',
-          'chai'
-        ],
-        files: [
-            'dist/dygraph-combined-dev.js',
-            'src/extras/smooth-plotter.js',
-            'src/extras/synchronizer.js',
-            'auto_tests/data/*.js',
-            'auto_tests/tests/*.js',
-        ],
-        autoWatch: false,
-        singleRun: true,
-        reporters: ['mocha', 'coverage'],  // or 'dots', 'mocha', 'spec'
-        preprocessors: {
-            'dist/dygraph-combined-dev.js': ['coverage']
-        },
-        coverageReporter: {
-            dir: 'dist/coverage',
-            reporters: [
-              { type: 'html', subdir: 'report-html' },
-              { type: 'lcovonly', subdir: 'report-lcov' },
-            ]
-        },
-        browsers: ['PhantomJS'],
-        plugins: [
-            'karma-mocha',
-            'karma-chai-plugins',
-            'karma-phantomjs-launcher',
-            'karma-coverage',
-            'karma-spec-reporter',
-            'karma-mocha-reporter'
-        ]
-    });
-};
index 7102a95..c3a3d87 100644 (file)
 
   <script>
     mocha.checkLeaks();
-    mocha.run();
+    if (window.mochaPhantomJS) {
+      mochaPhantomJS.run();
+    } else {
+      mocha.run();
+    }
   </script>
 </body>
 </html>
index b60a8fb..9250bb7 100644 (file)
@@ -26,6 +26,7 @@
   },
   "homepage": "https://github.com/danvk/dygraphs",
   "devDependencies": {
+    "babel": "^5.8.23",
     "babel-core": "^5.8.25",
     "babelify": "^6.3.0",
     "browserify": "^11.2.0",
     "envify": "^3.4.0",
     "exorcist": "^0.4.0",
     "http-server": "^0.8.5",
+    "istanbul": "^0.4.0",
     "jshint": "^2.5.10",
     "lcov-parse": "0.0.9",
     "mocha": "^2.1.0",
-    "mocha-phantomjs": "^4.0.1",
+    "mocha-phantomjs": "3.5.3",
+    "mocha-phantomjs-istanbul": "0.0.2",
     "obvious-closure-library": "^20140401.0.2",
     "parse-data-uri": "^0.2.0",
     "phantomjs": "^1.9.7-8",
@@ -49,6 +52,7 @@
   },
   "scripts": {
     "build": "./scripts/build.sh",
+    "coverage": "./scripts/generate-coverage.sh",
     "watch": "./scripts/watch.sh",
     "build-tests": "./scripts/build-tests.sh",
     "test": "./scripts/run-tests.sh",
diff --git a/scripts/generate-coverage.sh b/scripts/generate-coverage.sh
new file mode 100755 (executable)
index 0000000..e2343de
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/bash
+# Generate code coverage data for posting to Coveralls.
+# This requires dist/*.js to be in place.
+# Output is coverage/lcov.info
+
+set -o errexit
+set -x
+
+# Generate per-file ES6 --> ES5 transpilations
+babel src --out-dir dist/src
+babel auto_tests/tests --out-dir dist/auto_tests/tests
+
+# Instrument the source code with Istanbul's __coverage__ variable.
+rm -rf coverage/*  # Clear out everything to ensure a hermetic run.
+mkdir -p coverage
+istanbul instrument --output coverage/src dist/src
+cp -r dist/auto_tests coverage/
+
+# Build a combined file for running the tests in-browser
+browserify coverage/auto_tests/tests/*.js -o coverage/tests.js
+
+# Run http-server and save its PID for cleanup
+http-server > /dev/null &
+SERVER_PID=$!
+function finish() {
+  kill -TERM $SERVER_PID
+}
+trap finish EXIT
+
+# Give the server a chance to start up
+sleep 1
+
+# Run the tests using mocha-phantomjs & mocha-phantomjs-istanbul
+# This produces coverage/coverage.json
+phantomjs \
+  ./node_modules/mocha-phantomjs/lib/mocha-phantomjs.coffee \
+  http://localhost:8080/auto_tests/coverage.html \
+  spec '{"hooks": "mocha-phantomjs-istanbul", "coverageFile": "coverage/coverage.json"}'
+
+if [ $CI ]; then
+  # Convert the JSON coverage to LCOV for coveralls.
+  istanbul report --include coverage/*.json lcovonly
+
+  # Monkey patch in the untransformed source paths.
+  # perl -i -pe 's,dist/main,src/main,' coverage/lcov.info
+  echo ''  # reset exit code -- failure to post coverage shouldn't be an error.
+
+else
+
+  # Convert the JSON coverage to HTML for viewing
+  istanbul report --include coverage/*.json html
+  set +x
+
+  echo 'To browse coverage, run:'
+  echo
+  echo '  open coverage/index.html'
+  echo
+
+fi
diff --git a/src/dygraph-combined.js b/src/dygraph-combined.js
deleted file mode 100644 (file)
index e6380de..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-This is not the file you are looking for.
-A reasonably up-to-date version can be found at http://dygraphs.com/dygraph-combined.js
-
-dygraph-combined.js is a "packed" version of the larger dygraphs JS files. It is
-smaller and loads more quickly, but is harder to debug.
-
-To generate this file, run "make" or generate-combined.sh.