0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-performance-function.js
Ruben Bridgewater e038d6a1cd
test: refactor common.expectsError
This completely refactors the `expectsError` behavior: so far it's
almost identical to `assert.throws(fn, object)` in case it was used
with a function as first argument. It had a magical property check
that allowed to verify a functions `type` in case `type` was passed
used in the validation object. This pattern is now completely removed
and `assert.throws()` should be used instead.

The main intent for `common.expectsError()` is to verify error cases
for callback based APIs. This is now more flexible by accepting all
validation possibilites that `assert.throws()` accepts as well. No
magical properties exist anymore. This reduces surprising behavior
for developers who are not used to the Node.js core code base.

This has the side effect that `common` is used significantly less
frequent.

PR-URL: https://github.com/nodejs/node/pull/31092
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-12-31 15:54:20 +01:00

84 lines
2.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const {
performance,
PerformanceObserver
} = require('perf_hooks');
{
// Intentional non-op. Do not wrap in common.mustCall();
const n = performance.timerify(function noop() {});
const obs = new PerformanceObserver(common.mustCall((list) => {
const entries = list.getEntries();
const entry = entries[0];
assert(entry);
assert.strictEqual(entry.name, 'noop');
assert.strictEqual(entry.entryType, 'function');
assert.strictEqual(typeof entry.duration, 'number');
assert.strictEqual(typeof entry.startTime, 'number');
obs.disconnect();
}));
obs.observe({ entryTypes: ['function'] });
n();
}
{
// If the error throws, the error should just be bubbled up and the
// performance timeline entry will not be reported.
const obs = new PerformanceObserver(common.mustNotCall());
obs.observe({ entryTypes: ['function'] });
const n = performance.timerify(() => {
throw new Error('test');
});
assert.throws(() => n(), /^Error: test$/);
obs.disconnect();
}
{
class N {}
const n = performance.timerify(N);
const obs = new PerformanceObserver(common.mustCall((list) => {
const entries = list.getEntries();
const entry = entries[0];
assert.strictEqual(entry[0], 1);
assert.strictEqual(entry[1], 'abc');
assert(entry);
assert.strictEqual(entry.name, 'N');
assert.strictEqual(entry.entryType, 'function');
assert.strictEqual(typeof entry.duration, 'number');
assert.strictEqual(typeof entry.startTime, 'number');
obs.disconnect();
}));
obs.observe({ entryTypes: ['function'] });
new n(1, 'abc');
}
{
[1, {}, [], null, undefined, Infinity].forEach((input) => {
assert.throws(() => performance.timerify(input),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /The "fn" argument must be of type function/
});
});
}
// Function can only be wrapped once, also check length and name
{
const m = (a, b = 1) => {};
const n = performance.timerify(m);
const o = performance.timerify(m);
const p = performance.timerify(n);
assert.strictEqual(n, o);
assert.strictEqual(n, p);
assert.strictEqual(n.length, m.length);
assert.strictEqual(n.name, 'timerified m');
}