coverage.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (function(){
  2. //we want this at global scope so outside callers can find it. In a more realistic implementation we
  3. //should probably put it in a namespace.
  4. window.getCoverageByLine = function(silent) {
  5. var key = null;
  6. var lines = null;
  7. var source = null;
  8. //look for code coverage data
  9. if (typeof window._$jscoverage === 'object') {
  10. for (key in _$jscoverage) {}
  11. lines = _$jscoverage[key];
  12. }
  13. if (!lines && !silent) {
  14. console.log('code coverage data is NOT available');
  15. }
  16. return { 'key': key, 'lines': lines };
  17. };
  18. QUnit.done = function(t) {
  19. var cvgInfo = getCoverageByLine(true);
  20. if (!!cvgInfo.key) {
  21. var testableLines = 0;
  22. var testedLines = 0;
  23. var untestableLines = 0;
  24. for (lineIdx in cvgInfo.lines) {
  25. var cvg = cvgInfo.lines[lineIdx];
  26. if (typeof cvg === 'number') {
  27. testableLines += 1;
  28. if (cvg > 0) {
  29. testedLines += 1;
  30. }
  31. } else {
  32. untestableLines += 1;
  33. }
  34. }
  35. var coverage = '' + Math.floor(100 * testedLines / testableLines) + '%';
  36. var result = document.getElementById('qunit-testresult');
  37. if (result != null) {
  38. result.innerHTML = result.innerHTML + ' ' + coverage + ' test coverage of ' + cvgInfo.key;
  39. } else {
  40. console.log('can\'t find test-result element to update');
  41. }
  42. }
  43. };
  44. }());