forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtransform-test-gate-pragma-test.js
More file actions
213 lines (177 loc) · 5.01 KB
/
transform-test-gate-pragma-test.js
File metadata and controls
213 lines (177 loc) · 5.01 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* 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.
*/
'use strict';
describe('transform-test-gate-pragma', () => {
// Fake runtime
// eslint-disable-next-line no-unused-vars
const _test_gate = (gateFn, testName, cb) => {
test(testName, (...args) => {
shouldPass = gateFn(context);
return cb(...args);
});
};
// eslint-disable-next-line no-unused-vars
const _test_gate_focus = (gateFn, testName, cb) => {
// NOTE: Tests in this file are not actually focused because the calls to
// `test.only` and `fit` are compiled to `_test_gate_focus`. So if you want
// to focus something, swap the following `test` call for `test.only`.
test(testName, (...args) => {
shouldPass = gateFn(context);
isFocused = true;
return cb(...args);
});
};
// Feature flags, environment variables, etc. We can configure this in
// our test set up.
const context = {
flagThatIsOff: false,
flagThatIsOn: true,
environment: 'fake-environment',
};
let shouldPass;
let isFocused;
beforeEach(() => {
shouldPass = null;
isFocused = false;
});
test('no pragma', () => {
expect(shouldPass).toBe(null);
});
// unrelated comment
test('no pragma, unrelated comment', () => {
expect(shouldPass).toBe(null);
});
// @gate flagThatIsOn
test('basic positive test', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOff
test('basic negative test', () => {
expect(shouldPass).toBe(false);
});
// @gate flagThatIsOn
it('it method', () => {
expect(shouldPass).toBe(true);
});
/* eslint-disable jest/no-focused-tests */
// @gate flagThatIsOn
test.only('test.only', () => {
expect(isFocused).toBe(true);
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOff
it.only('it.only', () => {
expect(isFocused).toBe(true);
expect(shouldPass).toBe(false);
});
// @gate flagThatIsOn
fit('fit', () => {
expect(isFocused).toBe(true);
expect(shouldPass).toBe(true);
});
/* eslint-enable jest/no-focused-tests */
// @gate !flagThatIsOff
test('flag negation', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOn
// @gate !flagThatIsOff
test('multiple gates', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOn
// @gate flagThatIsOff
test('multiple gates 2', () => {
expect(shouldPass).toBe(false);
});
// @gate !flagThatIsOff && flagThatIsOn
test('&&', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOff || flagThatIsOn
test('||', () => {
expect(shouldPass).toBe(true);
});
// @gate (flagThatIsOn || flagThatIsOff) && flagThatIsOn
test('groups', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOn == !flagThatIsOff
test('==', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOn === !flagThatIsOff
test('===', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOn != !flagThatIsOff
test('!=', () => {
expect(shouldPass).toBe(false);
});
// @gate flagThatIsOn != !flagThatIsOff
test('!==', () => {
expect(shouldPass).toBe(false);
});
// @gate flagThatIsOn === true
test('true', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOff === false
test('false', () => {
expect(shouldPass).toBe(true);
});
// @gate environment === "fake-environment"
test('double quoted strings', () => {
expect(shouldPass).toBe(true);
});
// @gate environment === 'fake-environment'
test('single quoted strings', () => {
expect(shouldPass).toBe(true);
});
// @gate flagThatIsOn // This is a comment
test('line comment', () => {
expect(shouldPass).toBe(true);
});
});
describe('transform test-gate-pragma: actual runtime', () => {
// These tests use the actual gating runtime used by the rest of our
// test suite.
// @gate __DEV__
test('__DEV__', () => {
if (!__DEV__) {
throw Error("Doesn't work in production!");
}
});
// @gate build === "development"
test('strings', () => {
if (!__DEV__) {
throw Error("Doesn't work in production!");
}
});
// Always should fail because of the unguarded console.error
// @gate false
test('works with console.error tracking', () => {
console.error('Should cause test to fail');
});
// Always should fail because of the unguarded console.warn
// @gate false
test('works with console.warn tracking', () => {
console.warn('Should cause test to fail');
});
// @gate false
test('works with console tracking if error is thrown before end of test', () => {
console.warn('Please stop that!');
console.error('Stop that!');
throw Error('I told you to stop!');
});
});
describe('dynamic gate method', () => {
// @gate experimental && __DEV__
test('returns same conditions as pragma', () => {
expect(gate(ctx => ctx.experimental && ctx.__DEV__)).toBe(true);
});
});