/
githubmirror
/
wp-calypso
Обзор
Документация
Войти
/
githubmirror
/
wp-calypso
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
test/client/setup-test-framework.js
126 строк
3 KB
Emanuele Buccelli
Help Center: fix loading for users who sign up mid-onboarding (#112114)
30 июн 2026, 17:23
Не верифицирован
30 июн 2026, 17:23
eb018f4
Код
Авторство
О чём код?
import '@testing-library/jest-dom'; const nodeCrypto = require( 'node:crypto' ); const { ReadableStream, TransformStream } = require( 'node:stream/web' ); const { TextEncoder, TextDecoder } = require( 'util' ); const nock = require( 'nock' ); // Disables all network requests for all tests. nock.disableNetConnect(); beforeAll( () => { // reactivate nock on test start if ( ! nock.isActive() ) { nock.activate(); } } ); afterAll( () => { // helps clean up nock after each test run and avoid memory leaks nock.restore(); nock.cleanAll(); } ); // Define TextEncoder for ReactDOMServer global.TextEncoder = TextEncoder; global.TextDecoder = TextDecoder; global.ResizeObserver = require( 'resize-observer-polyfill' ); // jsdom doesn't implement IntersectionObserver, which is used by // @wordpress/dataviews' infinite-scroll hook. global.IntersectionObserver = class IntersectionObserver { constructor() { this.root = null; this.rootMargin = ''; this.thresholds = []; } observe() {} unobserve() {} disconnect() {} takeRecords() { return []; } }; global.fetch = jest.fn( () => Promise.resolve( { json: () => Promise.resolve(), } ) ); // Don't need to mock specific functions for any tests, but mocking // module because it accesses the `document` global. jest.mock( 'wpcom-proxy-request', () => ( { __esModule: true, canAccessWpcomApis: jest.fn(), isCookieAuthMissing: jest.fn( () => false ), reloadProxy: jest.fn(), requestAllBlogsAccess: jest.fn(), } ) ); jest.mock( 'smooch', () => ( { __esModule: true, } ) ); // Mock @automattic/agenttic-client to resolve dependency issues jest.mock( '@automattic/agenttic-client', () => ( { __esModule: true, getAgentManager: jest.fn( () => ( { createAgent: jest.fn(), getAgent: jest.fn(), } ) ), useAgentChat: jest.fn( () => ( { messages: [], isLoading: false, sendMessage: jest.fn(), clearMessages: jest.fn(), } ) ), } ), { virtual: true } ); // Mock @automattic/agenttic-ui to resolve dependency issues jest.mock( '@automattic/agenttic-ui', () => ( { __esModule: true, ThinkingMessage: jest.fn( () => 'Thinking...' ), AgentUI: jest.fn( () => null ), createMessageRenderer: jest.fn(), EmptyView: jest.fn( () => null ), } ), { virtual: true } ); // Mock crypto.randomUUID with its Node.js implementation global.crypto.randomUUID = () => nodeCrypto.randomUUID(); global.matchMedia = jest.fn( ( query ) => ( { matches: false, media: query, onchange: null, addListener: jest.fn(), // deprecated removeListener: jest.fn(), // deprecated addEventListener: jest.fn(), removeEventListener: jest.fn(), dispatchEvent: jest.fn(), } ) ); // This is used by @wp-playground/client global.ReadableStream = ReadableStream; global.TransformStream = TransformStream; global.Worker = require( 'worker_threads' ).Worker; // This is used by @wp-playground/client if ( typeof global.structuredClone !== 'function' ) { global.structuredClone = ( obj ) => JSON.parse( JSON.stringify( obj ) ); } // This is used by @wp-playground/client if ( ! global.crypto.subtle ) { // Mock crypto.subtle with its Node.js implementation, if needed. global.crypto.subtle = nodeCrypto.subtle; }