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

feat(jest): use enhanced version of unbound-method rule #146

Merged
merged 2 commits into from
Aug 10, 2021

Conversation

G-Rath
Copy link
Contributor

@G-Rath G-Rath commented Aug 5, 2021

I implemented this rule a while back, but wanted to wait before adding it here in case any bugs showed up.

The rule itself is a light wrapper around @typescript-eslint/unbound-method that only calls the underlying rule if the AST node being checked isn't a call to one of the expect matchers that takes a function, i.e. toHaveBeenCalled & co, as those methods don't require the argument to be bound since they're not actually calling the function (they're checking properties on it).

For example:

it('sends the buffered messages to the Slack webhook', async () => {
  const notifier = new Notifier('https://example.com/webhook');

  notifier.bufferMessage(message);

  await notifier.sendBufferedMessages();

  // eslint-disable-next-line @typescript-eslint/unbound-method
  expect(fakeIncomingWebhook.prototype.send).toHaveBeenCalledWith({
    text: message
  });
});

This rule lets us remove the eslint-disable-next-line, while still having all other test code checked for unbound methods 🎉

In order to make it easier to include this rule in configs, it silently fails if the base rule cannot be imported for some reason, aka if you're on a Javascript project that uses jest but not typescript - this means we can safely include it here instead of having to maintain a separate jest-and-typescript config just for this.


I've also included specifying eslint-plugin-jest as an optional peer dependency, which should be fine (and tbh we should be doing this for all our other plugin dependencies), but if it turns out to cause trouble (such as on older projects) then it can be reverted as it's a "technically more correct but not strictly required" sort of thing. I'm going to revert this for now because the docs generator currently assumes all peer dependencies are required.

The document generator tool currently assumes all peer dependencies are
required, so that needs to be adjusted before making this change
@G-Rath G-Rath merged commit 9357d36 into main Aug 10, 2021
@G-Rath G-Rath deleted the update-jest-rules branch August 10, 2021 21:44
github-actions bot pushed a commit that referenced this pull request Aug 11, 2021
# [2.2.0](v2.1.2...v2.2.0) (2021-08-11)

### Features

* **jest:** use enhanced version of `unbound-method` rule ([#146](#146)) ([9357d36](9357d36))
@github-actions
Copy link

🎉 This PR is included in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant