-
Notifications
You must be signed in to change notification settings - Fork 46.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pragma for feature testing: @gate #18581
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
scripts/babel/__tests__/transform-test-gate-pragma-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/** | ||
* 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); | ||
}); | ||
}); | ||
|
||
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!"); | ||
} | ||
}); | ||
|
||
// 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); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat testing strategy ^