@@ -13,6 +13,7 @@ import type {
13
13
BackendBridge ,
14
14
FrontendBridge ,
15
15
} from 'react-devtools-shared/src/bridge' ;
16
+ const { getTestFlags} = require ( '../../../../scripts/jest/TestFlags' ) ;
16
17
17
18
// Argument is serialized when passed from jest-cli script through to setupTests.
18
19
const compactConsole = process . env . compactConsole === 'true' ;
@@ -32,6 +33,76 @@ if (compactConsole) {
32
33
global . console = new CustomConsole ( process . stdout , process . stderr , formatter ) ;
33
34
}
34
35
36
+ const expectTestToFail = async ( callback , error ) => {
37
+ if ( callback . length > 0 ) {
38
+ throw Error (
39
+ 'Gated test helpers do not support the `done` callback. Return a ' +
40
+ 'promise instead.' ,
41
+ ) ;
42
+ }
43
+ try {
44
+ const maybePromise = callback ( ) ;
45
+ if (
46
+ maybePromise !== undefined &&
47
+ maybePromise !== null &&
48
+ typeof maybePromise . then === 'function'
49
+ ) {
50
+ await maybePromise ;
51
+ }
52
+ } catch ( testError ) {
53
+ return ;
54
+ }
55
+ throw error ;
56
+ } ;
57
+
58
+ const gatedErrorMessage = 'Gated test was expected to fail, but it passed.' ;
59
+ global . _test_gate = ( gateFn , testName , callback ) => {
60
+ let shouldPass ;
61
+ try {
62
+ const flags = getTestFlags ( ) ;
63
+ shouldPass = gateFn ( flags ) ;
64
+ } catch ( e ) {
65
+ test ( testName , ( ) => {
66
+ throw e ;
67
+ } ) ;
68
+ return ;
69
+ }
70
+ if ( shouldPass ) {
71
+ test ( testName , callback ) ;
72
+ } else {
73
+ const error = new Error ( gatedErrorMessage ) ;
74
+ Error . captureStackTrace ( error , global . _test_gate ) ;
75
+ test ( `[GATED, SHOULD FAIL] ${ testName } ` , ( ) =>
76
+ expectTestToFail ( callback , error ) ) ;
77
+ }
78
+ } ;
79
+ global . _test_gate_focus = ( gateFn , testName , callback ) => {
80
+ let shouldPass ;
81
+ try {
82
+ const flags = getTestFlags ( ) ;
83
+ shouldPass = gateFn ( flags ) ;
84
+ } catch ( e ) {
85
+ test . only ( testName , ( ) => {
86
+ throw e ;
87
+ } ) ;
88
+ return ;
89
+ }
90
+ if ( shouldPass ) {
91
+ test . only ( testName , callback ) ;
92
+ } else {
93
+ const error = new Error ( gatedErrorMessage ) ;
94
+ Error . captureStackTrace ( error , global . _test_gate_focus ) ;
95
+ test . only ( `[GATED, SHOULD FAIL] ${ testName } ` , ( ) =>
96
+ expectTestToFail ( callback , error ) ) ;
97
+ }
98
+ } ;
99
+
100
+ // Dynamic version of @gate pragma
101
+ global . gate = fn => {
102
+ const flags = getTestFlags ( ) ;
103
+ return fn ( flags ) ;
104
+ } ;
105
+
35
106
beforeEach ( ( ) => {
36
107
global . mockClipboardCopy = jest . fn ( ) ;
37
108
0 commit comments