forked from Automattic/studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest-setup.ts
68 lines (60 loc) · 2 KB
/
jest-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import '@testing-library/jest-dom';
// We need this polyfill because the `ReadableStream` class is
// used by `@php-wasm/universal` and it's not available in the Jest environment.
// eslint-disable-next-line import/no-unresolved
import 'web-streams-polyfill/polyfill';
import nock from 'nock';
if ( typeof window !== 'undefined' ) {
// The ipcListener global is usually defined in preload.ts
window.ipcListener = { subscribe: jest.fn() };
// Mock `matchMedia` as it's not implemented in JSDOM
// Reference: 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(),
} ) ),
} );
/**
* Mock `crypto.subtle.generateKey` as it's not implemented in JSDOM
* https://github.com/jsdom/jsdom/issues/1612
*
* `crypto.subtle.generateKey` is required by `@php-wasm/web`
*/
Object.defineProperty( global.crypto, 'subtle', {
value: { generateKey: jest.fn() },
} );
/**
* Mock `fetch` as it's not implemented in JSDOM
* https://github.com/jsdom/jsdom/issues/1724
*
* `fetch` is required by `@wp-playground/blueprints`
*/
global.fetch = jest.fn();
}
nock.disableNetConnect();
nock.enableNetConnect( 'raw.githubusercontent.com' );
// We consider the app to be online by default.
jest.mock( './src/hooks/use-offline', () => ( {
useOffline: jest.fn().mockReturnValue( false ),
} ) );
jest.mock( './src/hooks/use-ai-icon', () => ( {
__esModule: true,
default: () => ( {
rive: jest.fn(),
RiveComponent: jest.fn(),
inactiveInput: jest.fn(),
typingInput: jest.fn(),
thinkingInput: jest.fn(),
startStateMachine: jest.fn(),
pauseStateMachine: jest.fn(),
} ),
} ) );
global.ResizeObserver = require( 'resize-observer-polyfill' );