question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[proposal] expect(fn).toLog(str, level = 'log')

See original GitHub issue

🚀 Feature Proposal

Proposal is to create “sibling” methods to .toThrow, .toThrowErrorMatchingSnapshot() & .toThrowErrorMatchingInlineSnapshot that would test if console[level] has been called during the fn invocation

Motivation

Make testing console.log/console.warn/console.error calls easier - testing it with jest.fn()/mocks is somewhat cumbersome.

Example

test('getItemProps logs a helpful error when no object is given', () => {
  expect(() =>
    render(
      <Downshift>
        {({getItemProps}) => (
          <div>
            <span {...getItemProps()} />
          </div>
        )}
      </Downshift>,
    ),
  ).toLogMatchingSnapshot('error')
})

Pitch

IMHO it’s as helpful as toThrow methods and could be included in the core.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mattphillipscommented, Aug 14, 2018

Hey @Andarist I’m not sure I understand this as a matcher, its unclear that the matcher is actually watching the console to make assertions against it.

This behaviour feels closer to Jest’s jest.useFakeTimers(), perhaps a jest.useFakeConsole() which mocks all of the available methods would be more suitable?

That way you can use all of the existing matchers maybe something like:

test('getItemProps logs a helpful error when no object is given', () => {
  jest.useFakeConsole();
  render(
    <Downshift>
      {({getItemProps}) => (
        <div>
          <span {...getItemProps()} />
        </div>
      )}
    </Downshift>,
  );
  expect(console.error).toMatchSnapshot();
})

This is a little more verbose, but it is explicit over what you are doing.

An issue with wrapping the log code as a function that is ran by the matcher is that you may want to use the returned value (in this example the rendered component) in another assertions and now you would not be able to do that.

Let me know your thoughts? 😄

EDIT: cc/ @thymikee @SimenB @rickhanlonii I’d be curious to know your thoughts too 😉

0reactions
github-actions[bot]commented, Apr 30, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The right way to describe your strengths in a proposal
You should anticipate the evaluation forms so that the evaluator can copy and paste your descriptions of your strengths right onto their forms....
Read more >
OUTDOOR TED WEEMS
co Ruissitellon over chore. wm offer. to the Arvawiesen ... four weeks, alldinee house record for scale ... ¢O-Level off 1,11 con.
Read more >
Full text of "Historical collections of Louisiana and Florida
Full text of "Historical collections of Louisiana and Florida : including translations of original manuscripts relating to their discovery and settlement, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found