-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
test.ts
157 lines (136 loc) · 4.34 KB
/
test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* External dependencies
*/
import * as path from 'path';
import { test as base, expect } from '@playwright/test';
import type { ConsoleMessage } from '@playwright/test';
/**
* Internal dependencies
*/
import { Admin, Editor, PageUtils, RequestUtils } from './index';
const STORAGE_STATE_PATH =
process.env.STORAGE_STATE_PATH ||
path.join( process.cwd(), 'artifacts/storage-states/admin.json' );
/**
* Set of console logging types observed to protect against unexpected yet
* handled (i.e. not catastrophic) errors or warnings. Each key corresponds
* to the Playwright ConsoleMessage type, its value the corresponding function
* on the console global object.
*/
const OBSERVED_CONSOLE_MESSAGE_TYPES = [ 'warn', 'error' ] as const;
/**
* Adds a page event handler to emit uncaught exception to process if one of
* the observed console logging types is encountered.
*
* @param message The console message.
*/
function observeConsoleLogging( message: ConsoleMessage ) {
const type = message.type();
if (
! OBSERVED_CONSOLE_MESSAGE_TYPES.includes(
type as ( typeof OBSERVED_CONSOLE_MESSAGE_TYPES )[ number ]
)
) {
return;
}
const text = message.text();
// An exception is made for _blanket_ deprecation warnings: Those
// which log regardless of whether a deprecated feature is in use.
if ( text.includes( 'This is a global warning' ) ) {
return;
}
// A chrome advisory warning about SameSite cookies is informational
// about future changes, tracked separately for improvement in core.
//
// See: https://core.trac.wordpress.org/ticket/37000
// See: https://www.chromestatus.com/feature/5088147346030592
// See: https://www.chromestatus.com/feature/5633521622188032
if ( text.includes( 'A cookie associated with a cross-site resource' ) ) {
return;
}
// Viewing posts on the front end can result in this error, which
// has nothing to do with Gutenberg.
if ( text.includes( 'net::ERR_UNKNOWN_URL_SCHEME' ) ) {
return;
}
// TODO: Not implemented yet.
// Network errors are ignored only if we are intentionally testing
// offline mode.
// if (
// text.includes( 'net::ERR_INTERNET_DISCONNECTED' ) &&
// isOfflineMode()
// ) {
// return;
// }
// As of WordPress 5.3.2 in Chrome 79, navigating to the block editor
// (Posts > Add New) will display a console warning about
// non - unique IDs.
// See: https://core.trac.wordpress.org/ticket/23165
if ( text.includes( 'elements with non-unique id #_wpnonce' ) ) {
return;
}
// Ignore all JQMIGRATE (jQuery migrate) deprecation warnings.
if ( text.includes( 'JQMIGRATE' ) ) {
return;
}
const logFunction =
type as ( typeof OBSERVED_CONSOLE_MESSAGE_TYPES )[ number ];
// Disable reason: We intentionally bubble up the console message
// which, unless the test explicitly anticipates the logging via
// @wordpress/jest-console matchers, will cause the intended test
// failure.
// eslint-disable-next-line no-console
console[ logFunction ]( text );
}
const test = base.extend<
{
admin: Admin;
editor: Editor;
pageUtils: PageUtils;
snapshotConfig: void;
},
{
requestUtils: RequestUtils;
}
>( {
admin: async ( { page, pageUtils }, use ) => {
await use( new Admin( { page, pageUtils } ) );
},
editor: async ( { page }, use ) => {
await use( new Editor( { page } ) );
},
page: async ( { page }, use ) => {
page.on( 'console', observeConsoleLogging );
await use( page );
// Clear local storage after each test.
await page.evaluate( () => {
window.localStorage.clear();
} );
await page.close();
},
pageUtils: async ( { page }, use ) => {
await use( new PageUtils( { page } ) );
},
requestUtils: [
async ( {}, use, workerInfo ) => {
const requestUtils = await RequestUtils.setup( {
baseURL: workerInfo.project.use.baseURL,
storageStatePath: STORAGE_STATE_PATH,
} );
await Promise.all( [
requestUtils.activateTheme( 'twentytwentyone' ),
// Disable this test plugin as it's conflicting with some of the tests.
// We already have reduced motion enabled and Playwright will wait for most of the animations anyway.
requestUtils.deactivatePlugin(
'gutenberg-test-plugin-disables-the-css-animations'
),
requestUtils.deleteAllPosts(),
requestUtils.deleteAllBlocks(),
requestUtils.resetPreferences(),
] );
await use( requestUtils );
},
{ scope: 'worker', auto: true },
],
} );
export { test, expect };