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

Add test helpers for gating by feature #18574

Closed
wants to merge 2 commits into from

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Apr 10, 2020

Adds a new test helper, it.gate. You pass it the name of a feature flag, and if the flag is enabled, it asserts that the test passes. If the flag is off, it asserts that the test fails. Example:

it.gate('enableNewReconciler', 'test that only passes in new reconciler', () => {
  // ... test goes here
});

(test.gate is equivalent and makes perhaps makes more grammatical sense, but I'm using it.gate in these examples since we use it everywhere in our tests.)

You can test multiple flags by passing an array:

it.gate(
  ['enableNewReconciler', 'enableModernEventSystem'],
  'test that passes only in new reconciler with modern event system', () => {
    // ... test goes here
  }
);

If any of the flags are disabled, the test is expected to fail.

You can check if a flag is off by adding a ! to the front:

it.gate(
  '!enableNewReconciler',
  'test that passes only in the old reconciler', () => {
    // ... test goes here
  }
);

For anything more advanced, you can pass a function:

it.gate(
  flags => flags.warnAboutSpreadingKeyToJSX || !__DEV__,
  'should warn when keys are passed as part of props', () => {
    // Test passes when the flag is enabled, or in prod since
    // there are no warnings in prod
  }
);

I also added aliases for it.old and it.new, for testing the different reconciler forks:

it.old('test that passes only in the old reconciler', () => {
  // ... test goes here
});
it.new('test that passes only in the new reconciler', () => {
  // ... test goes here
});

Next steps

Longer term I think it would nice if these were implemented as pragmas instead:

// @gate enableNewReconciler
it('test that passes only in the new reconciler', () => {
  // ... test goes here
});

Adds a new test helper, `it.gate`. You pass it the name of a feature
flag, and if the flag is enabled, it asserts that the test passes. If
the flag is off, it asserts that the test fails. Example:

```js
it.gate('enableNewReconciler', 'test that only passes in new reconciler', () => {
  // ... test goes here
});
```

(`test.gate` is equivalent and makes perhaps makes more grammatical
sense, but I'm using `it.gate` in these examples since we use `it`
everywhere in our tests.)

You can test multiple flags by passing an array:

```js
it.gate(
  ['enableNewReconciler', 'enableModernEventSystem'],
  'test that passes only in new reconciler with modern event system', () => {
    // ... test goes here
  }
);
```

If any of the flags are disabled, the test is expected to fail.

You can check if a flag is off by addding a `!` to the front:

```js
it.gate(
  '!enableNewReconciler',
  'test that passes only in the old reconciler', () => {
    // ... test goes here
  }
);
```

I also added aliases for `it.old` and `it.new`, for testing the
different reconciler forks:

```js
it.old('test that passes only in the old reconciler', () => {
  // ... test goes here
});
it.new('test that passes only in the new reconciler', () => {
  // ... test goes here
});
```
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Apr 10, 2020
@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 10, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 8aa9d74:

Sandbox Source
keen-haibt-i10x1 Configuration

@acdlite
Copy link
Collaborator Author

acdlite commented Apr 10, 2020

Update: Added this to PR description

Longer term I think it would nice if these were implemented as pragmas instead:

// @gate enableNewReconciler
it('test that passes only in the new reconciler', () => {
  // ... test goes here
});

@sizebot
Copy link

sizebot commented Apr 10, 2020

No significant bundle size changes to report.

Size changes (experimental)

Generated by 🚫 dangerJS against 8aa9d74

@sizebot
Copy link

sizebot commented Apr 10, 2020

No significant bundle size changes to report.

Size changes (stable)

Generated by 🚫 dangerJS against 8aa9d74

To show that it works
function installGatedTestHelpers(channel) {
const it = global.it;
const fit = global.fit;
const xit = global.xit;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I kind of was hoping to switch to the test() form. Does this patch that too? I guess it’s just an alias for it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah it is an alias

@acdlite
Copy link
Collaborator Author

acdlite commented Apr 12, 2020

I went ahead and opened an alternative PR for the pragma proposal: #18581

I prefer that one, but I'll keep this PR open for reference.

@acdlite
Copy link
Collaborator Author

acdlite commented Apr 13, 2020

Closed by #18581

@acdlite acdlite closed this Apr 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants