Summary: perf_hooks is no longer needed as these APIs are widely available in both browser and electron. Still needed in unit tests, as they run from Node Reviewed By: lblasa, aigoncharov Differential Revision: D32648253 fbshipit-source-id: 5718ea99b57f96f6858311fd0815ad18c476c99e
73 lines
2.0 KiB
JavaScript
73 lines
2.0 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
// jest-setup-after will run after Jest has been initialized, so that it can be adapted.
|
|
import {cleanup} from '@testing-library/react';
|
|
|
|
const test = global.test;
|
|
if (!test) {
|
|
throw new Error('Failed jest test object');
|
|
}
|
|
/**
|
|
* This test will not be executed on Github / SandCastle,
|
|
* since, for example, it relies on precise timer reliability
|
|
*/
|
|
test.local = function local() {
|
|
const fn = process.env.SANDCASTLE || process.env.CI ? test.skip : test;
|
|
// eslint-disable-next-line
|
|
return fn.apply(null, arguments);
|
|
};
|
|
|
|
/**
|
|
* This test will only run on non-windows machines
|
|
*/
|
|
test.unix = function local() {
|
|
const fn = process.platform === 'win32' ? test.skip : test;
|
|
// eslint-disable-next-line
|
|
return fn.apply(null, arguments);
|
|
};
|
|
|
|
afterEach(cleanup);
|
|
|
|
console.debug = function () {
|
|
// Intentional noop, we don't want debug statements in Jest runs
|
|
};
|
|
|
|
// make perf tools available in Node (it is available in Browser / Electron just fine)
|
|
const {PerformanceObserver, performance} = require('perf_hooks');
|
|
Object.freeze(performance);
|
|
Object.freeze(Object.getPrototypeOf(performance));
|
|
// Something in our unit tests is messing with the performance global
|
|
// This fixes that.....
|
|
Object.defineProperty(global, 'performance', {
|
|
get() {
|
|
return performance;
|
|
},
|
|
set() {
|
|
throw new Error('Attempt to overwrite global.performance');
|
|
},
|
|
});
|
|
|
|
global.PerformanceObserver = PerformanceObserver;
|
|
|
|
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
|
|
Object.defineProperty(window, 'matchMedia', {
|
|
writable: true,
|
|
value: jest.fn().mockImplementation((query) => ({
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
addListener: jest.fn(), // deprecated
|
|
removeListener: jest.fn(), // deprecated
|
|
addEventListener: jest.fn(),
|
|
removeEventListener: jest.fn(),
|
|
dispatchEvent: jest.fn(),
|
|
})),
|
|
});
|