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: add toWarnDev custom Jest matcher #3574

Merged
merged 3 commits into from
Mar 4, 2019

Conversation

francoischalifour
Copy link
Member

@francoischalifour francoischalifour commented Mar 3, 2019

This adds a custom Jest matcher to assert that warnings are reported correctly in development mode. This matcher is called toWarnDev and is inspired by the one developed by the React team.

It was quite cumbersome to assert that connector/widget warnings were reported and led us to sometimes skip these checks.

This PR aims at reducing this friction and providing a tool to improve DX.

Before

test('with `hidden` as boolean warns', () => {
  const warn = jest.spyOn(global.console, 'warn');
  warn.mockImplementation(() => {});

  panel({
    hidden: true,
  });

  expect(warn).toHaveBeenCalledWith(
    '[InstantSearch.js]: The `hidden` option in the "panel" widget expects...'
  );

  warn.mockRestore();
});

After

Usage

test('with `hidden` as boolean warns', () => {
  expect(() => {
    panel({
      hidden: true,
    });
  }).toWarnDev(
    '[InstantSearch.js]: The `hidden` option in the "panel" widget expects...'
  );
});

Output

-7655b696-8443-472f-a64e-ba01fb570a61untitled

@francoischalifour francoischalifour requested a review from a team March 3, 2019 19:09
@algobot
Copy link
Contributor

algobot commented Mar 3, 2019

Deploy preview for instantsearchjs ready!

Built with commit dbb5d33

https://deploy-preview-3574--instantsearchjs.netlify.com

@Haroenv
Copy link
Contributor

Haroenv commented Mar 3, 2019

Would be amazing if it could be an inline snapshot. Is that possible?

@francoischalifour
Copy link
Member Author

That would be a lot more difficult because inline snapshots rely on Prettier, file system and custom parsers.

You can learn more in the original inline snapshots thread and in the Jest source code.

I'm not aware of any helper for that.

I would suggest sticking with regular expectations.

Copy link
Contributor

@tkrugg tkrugg left a comment

Choose a reason for hiding this comment

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

looks good and DRYer indeed!

@francoischalifour francoischalifour merged commit adebe38 into develop Mar 4, 2019
@francoischalifour francoischalifour deleted the test/toWarnDev-matcher branch March 4, 2019 16:27
francoischalifour added a commit to algolia/autocomplete that referenced this pull request Nov 9, 2020
This adopts a similar strategy to InstantSearch (algolia/instantsearch#3260) for our bundles as well as our development warnings (algolia/instantsearch#3574).
francoischalifour added a commit to algolia/autocomplete that referenced this pull request Nov 9, 2020
This adopts a similar strategy to InstantSearch (algolia/instantsearch#3260) for our bundles as well as our development warnings (algolia/instantsearch#3574).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants