Skip to content

Commit 43a3dae

Browse files
committed
Core: Make PerfReporter compatible with Node.js
Switch from hardcoded `window.performance` to `performance` as global more generally, so it works in Node.js as well. Note by default this remains only enabled in browsers, as controlled by `src/core/start.js` where we check: ``` if (config.reporters.perf || (config.reporters.perf === undefined && window && document)) { ``` So this change does not start enabling it in Node.js. But, you can now enable it on Node.js via `qunit_config_reporters_perf=true` or by invoking `QUnit.reporters.perf.init(QUnit)`, which previously did not do anything on Node.js.
1 parent 2f9c96b commit 43a3dae

File tree

10 files changed

+102
-27
lines changed

10 files changed

+102
-27
lines changed

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ module.exports = function (grunt) {
9191
'test/module-todo.html',
9292
'test/only-each.html',
9393
'test/overload.html',
94-
'test/performance-mark.html',
94+
'test/perf-clear-marks.html',
95+
'test/perf-mark.html',
9596
'test/preconfig-flat-reporters.html',
9697
'test/preconfig-flat-testId.html',
9798
'test/preconfig-flat.html',

docs/api/reporters/perf.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ This uses the [performance.measure()](https://developer.mozilla.org/en-US/docs/W
1919
QUnit enables the perf reporter by default in [Browser](../../browser.md) environments. The measures are automatically included in Firefox Profiler and Chrome DevTools (Safari is pending [WebKit #213870](https://bugs.webkit.org/show_bug.cgi?id=213870)).
2020

2121
```
22-
QUnit Test Run
23-
└── QUnit Test Suite: Example
22+
QUnit Run
23+
└── QUnit Module: Example
2424
├── QUnit Test: apple
2525
├── QUnit Test: banana
2626
└── QUnit Test: citron

src/core/reporters/PerfReporter.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import { window } from '../globals.js';
1+
import { globalThis } from '../globals.js';
22
import { prioritySymbol } from '../events.js';
33
import Logger from '../logger.js';
44

5-
// TODO: Change from window to globalThis in QUnit 3.0, so that the reporter
6-
// works for Node.js as well. As this can add overhead, we should keep
7-
// this opt-in on the CLI.
85
const nativePerf = (
9-
window
10-
&& typeof window.performance !== 'undefined'
6+
typeof globalThis.performance !== 'undefined'
117
// eslint-disable-next-line compat/compat -- Checked
12-
&& typeof window.performance.mark === 'function'
8+
&& typeof globalThis.performance.mark === 'function'
139
// eslint-disable-next-line compat/compat -- Checked
14-
&& typeof window.performance.measure === 'function'
10+
&& typeof globalThis.performance.measure === 'function'
1511
)
16-
? window.performance
12+
? globalThis.performance
1713
: undefined;
1814

1915
const perf = {
@@ -61,7 +57,7 @@ export default class PerfReporter {
6157
const suiteName = suiteEnd.fullName.join(' – ');
6258

6359
this.perf.mark(`qunit_suite_${suiteLevel}_end`);
64-
this.perf.measure(`QUnit Test Suite: ${suiteName}`,
60+
this.perf.measure(`QUnit Module: ${suiteName}`,
6561
`qunit_suite_${suiteLevel}_start`,
6662
`qunit_suite_${suiteLevel}_end`
6763
);
@@ -83,6 +79,6 @@ export default class PerfReporter {
8379

8480
onRunEnd () {
8581
this.perf.mark('qunit_suite_0_end');
86-
this.perf.measure('QUnit Test Run', 'qunit_suite_0_start', 'qunit_suite_0_end');
82+
this.perf.measure('QUnit Run', 'qunit_suite_0_start', 'qunit_suite_0_end');
8783
}
8884
}

test/cli/fixtures/perf-mark.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-env browser */
2+
3+
QUnit.config.reorder = false;
4+
5+
QUnit.test('foo', function (assert) {
6+
assert.true(true);
7+
});
8+
9+
QUnit.test('bar', function (assert) {
10+
assert.true(true);
11+
});
12+
13+
QUnit.test('getEntries', function (assert) {
14+
// eslint-disable-next-line compat/compat - Only for IE 10+ and Safari 10+
15+
const entries = performance.getEntriesByType('measure')
16+
.filter(function (entry) {
17+
return entry.name.indexOf('QUnit') === 0;
18+
})
19+
.map(function (entry) {
20+
return entry.toJSON();
21+
});
22+
23+
assert.propContains(entries, [
24+
{ name: 'QUnit Test: foo' },
25+
{ name: 'QUnit Test: bar' }
26+
]);
27+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# command: ["qunit", "perf-mark.js"]
2+
# env: { "qunit_config_reporters_perf": true }
3+
4+
TAP version 13
5+
ok 1 foo
6+
ok 2 bar
7+
ok 3 getEntries
8+
1..3
9+
# pass 3
10+
# skip 0
11+
# todo 0
12+
# fail 0

test/perf-clear-marks.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>perf-clear-marks</title>
6+
<link rel="stylesheet" href="../src/core/qunit.css">
7+
<script src="../qunit/qunit.js"></script>
8+
<script src="perf-clear-marks.js"></script>
9+
</head>
10+
<body>
11+
<div id="qunit"></div>
12+
</body>
13+
</html>
File renamed without changes.

test/perf-mark.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>perf-mark</title>
6+
<link rel="stylesheet" href="../src/core/qunit.css">
7+
<script src="../qunit/qunit.js"></script>
8+
<script src="perf-mark.js"></script>
9+
</head>
10+
<body>
11+
<div id="qunit"></div>
12+
</body>
13+
</html>

test/perf-mark.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-env browser */
2+
3+
QUnit.config.reorder = false;
4+
5+
QUnit.test('foo', function (assert) {
6+
assert.true(true);
7+
});
8+
9+
QUnit.test('bar', function (assert) {
10+
assert.true(true);
11+
});
12+
13+
QUnit.test('getEntries', function (assert) {
14+
var entries = performance.getEntriesByType('measure')
15+
.filter(function (entry) {
16+
return entry.name.indexOf('QUnit') === 0;
17+
})
18+
.map(function (entry) {
19+
return entry.toJSON();
20+
});
21+
22+
assert.propContains(entries, [
23+
{ name: 'QUnit Test: foo' },
24+
{ name: 'QUnit Test: bar' }
25+
]);
26+
});

test/performance-mark.html

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)