When running jest, if a function makes use of performance.getEntries (or performance.getEntriesByName) then the test throws an exception:
performance.getEntries is not a function
How can I allow my tests to continue running without crashing?
According to the documentation, performance.getEntries was removed from perf_hooks in NodeJS v10.0.0 (https://github.com/nodejs/node/pull/19563) and the same functionality may be achieved using
const measures = []
const obs = new PerformanceObserver(list => {
measures.push(...list.getEntries())
obs.disconnect()
})
obs.observe({entryTypes: ['measure']})
function getEntriesByType(name) {
return measures
}
However the above solution is only viable when working directly in the NodeJS application, not when running tests as I do not want to change my implementation. I have tried mocking performance.getEntries as a global in setupAfterEnv, as I would for other globals. For example:
global.performance = {
getEntries: () => []
}
However when debugging, performance is always equal to {} (while performance.now() works well. I believe this is default jsdom (which is my testEnvironment).