Skip to content
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

Test infra: Support gate('enableFeatureFlag') #30760

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions scripts/babel/__tests__/transform-test-gate-pragma-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,8 @@ describe('dynamic gate method', () => {
it('returns same conditions as pragma', () => {
expect(gate(ctx => ctx.experimental && ctx.__DEV__)).toBe(true);
});

it('converts string conditions to accessor function', () => {
expect(gate('experimental')).toBe(gate(flags => flags.experimental));
});
});
19 changes: 15 additions & 4 deletions scripts/jest/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,17 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
}
};

const coerceGateConditionToFunction = gateFnOrString => {
return typeof gateFnOrString === 'string'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this improvement, it could be nice to assert a boolean result of the function to fail earlier.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was thinking the same. Would help clean up removed flags.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would help clean up removed flags.

there's already an error if you reference a flag that doesn't exist

? // `gate('foo')` is treated as equivalent to `gate(flags => flags.foo)`
flags => flags[gateFnOrString]
: // Assume this is already a function
gateFnOrString;
};

const gatedErrorMessage = 'Gated test was expected to fail, but it passed.';
global._test_gate = (gateFn, testName, callback, timeoutMS) => {
global._test_gate = (gateFnOrString, testName, callback, timeoutMS) => {
const gateFn = coerceGateConditionToFunction(gateFnOrString);
let shouldPass;
try {
const flags = getTestFlags();
Expand All @@ -230,7 +239,8 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
expectTestToFail(callback, error, timeoutMS));
}
};
global._test_gate_focus = (gateFn, testName, callback, timeoutMS) => {
global._test_gate_focus = (gateFnOrString, testName, callback, timeoutMS) => {
const gateFn = coerceGateConditionToFunction(gateFnOrString);
let shouldPass;
try {
const flags = getTestFlags();
Expand Down Expand Up @@ -259,8 +269,9 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
};

// Dynamic version of @gate pragma
global.gate = fn => {
global.gate = gateFnOrString => {
const gateFn = coerceGateConditionToFunction(gateFnOrString);
const flags = getTestFlags();
return fn(flags);
return gateFn(flags);
};
}
Loading