Skip to content

Conversation

@francoischalifour
Copy link
Contributor

@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
Contributor 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.

5 participants