Skip to content

Commit

Permalink
Core: guards performance.measure to not throw and cause tests to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcsapo authored and trentmwillis committed Oct 12, 2018
1 parent 2b30582 commit 7b8fc7a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/core/utilities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { window, setTimeout } from "../globals";
import Logger from "../logger";

export const toString = Object.prototype.toString;
export const hasOwn = Object.prototype.hasOwnProperty;
Expand All @@ -19,6 +20,17 @@ function detectPerformanceApi() {
typeof window.performance.measure === "function";
}

export function measure( comment, startMark, endMark ) {

// `performance.measure` may fail if the mark could not be found.
// reasons a specific mark could not be found include: outside code invoking `performance.clearMarks()`
try {
performance.measure( comment, startMark, endMark );
} catch ( ex ) {
Logger.warn( "performance.measure could not be executed because of ", ex.message );
}
}

export const defined = {
document: window && window.document !== undefined,
setTimeout: setTimeout !== undefined
Expand Down
5 changes: 3 additions & 2 deletions src/reports/suite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { performance, performanceNow } from "../core/utilities";
import { measure, performance, performanceNow } from "../core/utilities";

export default class SuiteReport {
constructor( name, parentSuite ) {
Expand Down Expand Up @@ -43,7 +43,8 @@ export default class SuiteReport {
performance.mark( `qunit_suite_${suiteLevel}_end` );

const suiteName = this.fullName.join( " – " );
performance.measure(

measure(
suiteLevel === 0 ? "QUnit Test Run" : `QUnit Test Suite: ${suiteName}`,
`qunit_suite_${suiteLevel}_start`,
`qunit_suite_${suiteLevel}_end`
Expand Down
5 changes: 3 additions & 2 deletions src/reports/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extend, performance, performanceNow } from "../core/utilities";
import { extend, measure, performance, performanceNow } from "../core/utilities";

export default class TestReport {
constructor( name, suite, options ) {
Expand Down Expand Up @@ -41,7 +41,8 @@ export default class TestReport {
performance.mark( "qunit_test_end" );

const testName = this.fullName.join( " – " );
performance.measure(

measure(
`QUnit Test: ${testName}`,
"qunit_test_start",
"qunit_test_end"
Expand Down
13 changes: 13 additions & 0 deletions test/performance-mark.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit Performance Test Suite</title>
<link rel="stylesheet" href="../dist/qunit.css">
<script src="../dist/qunit.js"></script>
<script src="performance-mark.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>
7 changes: 7 additions & 0 deletions test/performance-mark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
QUnit.module( "urlParams performance mark module", function() {
QUnit.test( "shouldn't fail if performance marks are cleared ", function( assert ) {
performance.clearMarks();

assert.ok( true );
} );
} );

0 comments on commit 7b8fc7a

Please sign in to comment.