| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | 'use strict'; |
| 8 | |
| 9 | describe('transform-test-gate-pragma', () => { |
| 10 | // Fake runtime |
| 11 | // eslint-disable-next-line no-unused-vars |
| 12 | const _test_gate = (gateFn, testName, cb) => { |
| 13 | // eslint-disable-next-line jest/no-done-callback, jest/valid-title |
| 14 | it(testName, (...args) => { |
| 15 | shouldPass = gateFn(context); |
| 16 | return cb(...args); |
| 17 | }); |
| 18 | }; |
| 19 | |
| 20 | // eslint-disable-next-line no-unused-vars |
| 21 | const _test_gate_focus = (gateFn, testName, cb) => { |
| 22 | // NOTE: Tests in this file are not actually focused because the calls to |
| 23 | // `test.only` and `fit` are compiled to `_test_gate_focus`. So if you want |
| 24 | // to focus something, swap the following `test` call for `test.only`. |
| 25 | // eslint-disable-next-line jest/no-done-callback, jest/valid-title |
| 26 | it(testName, (...args) => { |
| 27 | shouldPass = gateFn(context); |
| 28 | isFocused = true; |
| 29 | return cb(...args); |
| 30 | }); |
| 31 | }; |
| 32 | |
| 33 | // Feature flags, environment variables, etc. We can configure this in |
| 34 | // our test set up. |
| 35 | const context = { |
| 36 | flagThatIsOff: false, |
| 37 | flagThatIsOn: true, |
| 38 | environment: 'fake-environment', |
| 39 | }; |
| 40 | |
| 41 | let shouldPass; |
| 42 | let isFocused; |
| 43 | beforeEach(() => { |
| 44 | shouldPass = null; |
| 45 | isFocused = false; |
| 46 | }); |
| 47 | |
| 48 | it('no pragma', () => { |
| 49 | expect(shouldPass).toBe(null); |
| 50 | }); |
| 51 | |
| 52 | // unrelated comment |
| 53 | it('no pragma, unrelated comment', () => { |
| 54 | expect(shouldPass).toBe(null); |
| 55 | }); |
| 56 | |
| 57 | // @gate flagThatIsOn |
| 58 | it('basic positive test', () => { |
| 59 | expect(shouldPass).toBe(true); |
| 60 | }); |
| 61 | |
| 62 | // @gate flagThatIsOff |
| 63 | it('basic negative test', () => { |
| 64 | expect(shouldPass).toBe(false); |
| 65 | }); |
| 66 | |
| 67 | // @gate flagThatIsOn |
| 68 | it('method', () => { |
| 69 | expect(shouldPass).toBe(true); |
| 70 | }); |
| 71 | |
| 72 | /* eslint-disable jest/no-focused-tests */ |
| 73 | |
| 74 | // @gate flagThatIsOn |
| 75 | it.only('test.only', () => { |
| 76 | expect(isFocused).toBe(true); |
| 77 | expect(shouldPass).toBe(true); |
| 78 | }); |
| 79 | |
| 80 | // @gate flagThatIsOff |
| 81 | it.only('it.only', () => { |
| 82 | expect(isFocused).toBe(true); |
| 83 | expect(shouldPass).toBe(false); |
| 84 | }); |
| 85 | |
| 86 | // @gate flagThatIsOn |
| 87 | it.only('fit', () => { |
| 88 | expect(isFocused).toBe(true); |
| 89 | expect(shouldPass).toBe(true); |
| 90 | }); |
| 91 | |
| 92 | /* eslint-enable jest/no-focused-tests */ |
| 93 | |
| 94 | // @gate !flagThatIsOff |
| 95 | it('flag negation', () => { |
| 96 | expect(shouldPass).toBe(true); |
| 97 | }); |
| 98 | |
| 99 | // @gate flagThatIsOn |
| 100 | // @gate !flagThatIsOff |
| 101 | it('multiple gates', () => { |
| 102 | expect(shouldPass).toBe(true); |
| 103 | }); |
| 104 | |
| 105 | // @gate flagThatIsOn |
| 106 | // @gate flagThatIsOff |
| 107 | it('multiple gates 2', () => { |
| 108 | expect(shouldPass).toBe(false); |
| 109 | }); |
| 110 | |
| 111 | // @gate !flagThatIsOff && flagThatIsOn |
| 112 | it('&&', () => { |
| 113 | expect(shouldPass).toBe(true); |
| 114 | }); |
| 115 | |
| 116 | // @gate flagThatIsOff || flagThatIsOn |
| 117 | it('||', () => { |
| 118 | expect(shouldPass).toBe(true); |
| 119 | }); |
| 120 | |
| 121 | // @gate (flagThatIsOn || flagThatIsOff) && flagThatIsOn |
| 122 | it('groups', () => { |
| 123 | expect(shouldPass).toBe(true); |
| 124 | }); |
| 125 | |
| 126 | // @gate flagThatIsOn == !flagThatIsOff |
| 127 | it('==', () => { |
| 128 | expect(shouldPass).toBe(true); |
| 129 | }); |
| 130 | |
| 131 | // @gate flagThatIsOn === !flagThatIsOff |
| 132 | it('===', () => { |
| 133 | expect(shouldPass).toBe(true); |
| 134 | }); |
| 135 | |
| 136 | // @gate flagThatIsOn != !flagThatIsOff |
| 137 | it('!=', () => { |
| 138 | expect(shouldPass).toBe(false); |
| 139 | }); |
| 140 | |
| 141 | // @gate flagThatIsOn != !flagThatIsOff |
| 142 | it('!==', () => { |
| 143 | expect(shouldPass).toBe(false); |
| 144 | }); |
| 145 | |
| 146 | // @gate flagThatIsOn === true |
| 147 | it('true', () => { |
| 148 | expect(shouldPass).toBe(true); |
| 149 | }); |
| 150 | |
| 151 | // @gate flagThatIsOff === false |
| 152 | it('false', () => { |
| 153 | expect(shouldPass).toBe(true); |
| 154 | }); |
| 155 | |
| 156 | // @gate environment === "fake-environment" |
| 157 | it('double quoted strings', () => { |
| 158 | expect(shouldPass).toBe(true); |
| 159 | }); |
| 160 | |
| 161 | // @gate environment === 'fake-environment' |
| 162 | it('single quoted strings', () => { |
| 163 | expect(shouldPass).toBe(true); |
| 164 | }); |
| 165 | |
| 166 | // @gate flagThatIsOn // This is a comment |
| 167 | it('line comment', () => { |
| 168 | expect(shouldPass).toBe(true); |
| 169 | }); |
| 170 | }); |
| 171 | |
| 172 | describe('transform test-gate-pragma: actual runtime', () => { |
| 173 | // These tests use the actual gating runtime used by the rest of our |
| 174 | // test suite. |
| 175 | |
| 176 | // @gate __DEV__ |
| 177 | it('__DEV__', () => { |
| 178 | if (!__DEV__) { |
| 179 | throw Error("Doesn't work in production!"); |
| 180 | } |
| 181 | }); |
| 182 | |
| 183 | // @gate build === "development" |
| 184 | it('strings', () => { |
| 185 | if (!__DEV__) { |
| 186 | throw Error("Doesn't work in production!"); |
| 187 | } |
| 188 | }); |
| 189 | |
| 190 | // Always should fail because of the unguarded console.error |
| 191 | // @gate false |
| 192 | it('works with console.error tracking', () => { |
| 193 | console.error('Should cause test to fail'); |
| 194 | }); |
| 195 | |
| 196 | // Always should fail because of the unguarded console.warn |
| 197 | // @gate false |
| 198 | it('works with console.warn tracking', () => { |
| 199 | console.warn('Should cause test to fail'); |
| 200 | }); |
| 201 | |
| 202 | // @gate false |
| 203 | it('works with console tracking if error is thrown before end of test', () => { |
| 204 | console.warn('Please stop that!'); |
| 205 | console.error('Stop that!'); |
| 206 | throw Error('I told you to stop!'); |
| 207 | }); |
| 208 | |
| 209 | // @gate false |
| 210 | it('a global error event is treated as a test failure', () => { |
| 211 | dispatchEvent( |
| 212 | new ErrorEvent('error', { |
| 213 | error: new Error('Oops!'), |
| 214 | }) |
| 215 | ); |
| 216 | }); |
| 217 | }); |
| 218 | |
| 219 | describe('dynamic gate method', () => { |
| 220 | // @gate experimental && __DEV__ |
| 221 | it('returns same conditions as pragma', () => { |
| 222 | expect(gate(ctx => ctx.experimental && ctx.__DEV__)).toBe(true); |
| 223 | }); |
| 224 | |
| 225 | it('converts string conditions to accessor function', () => { |
| 226 | expect(gate('experimental')).toBe(gate(flags => flags.experimental)); |
| 227 | }); |
| 228 | }); |