Gutenberg: Tests: Create custom "expect console logging" Jest matcher

Created on 3 Jan 2018  路  4Comments  路  Source: WordPress/gutenberg

In a few places we're asserting that console logging is called, which requires a bit of finesse to properly temporarily disable ESLint rules and reset mock calls (in order to avoid tripping the global unintended console logging assertions):

We should plan to refactor these into a custom Jest matcher.

Possible usage:

// expect( console ).toHaveLogged( [ level[, expectedMessage ] ] );
expect( console ).toHaveLogged( 'warn', 'Should not do that!' );
expect( console ).toHaveLogged( 'error', 'Cannot do that!' );

See: https://facebook.github.io/jest/docs/en/expect.html#expectextendmatchers

Automated Testing Good First Issue [Priority] Low [Type] Enhancement

Most helpful comment

@atimmer It's not really broadly applicable, since it's in our setup that there's a spy for console logging which is globally asserted to not have been called, which is valuable but adds complexity to _expecting_ the logging.

https://github.com/WordPress/gutenberg/blob/eb7b0b771924c6d0cae2cc001e7790caf8bfe9c6/test/unit/setup-test-framework.js#L25-L37

All 4 comments

I expect this would be something that is useful to the wider JavaScript community. Is this something to contribute to Jest?

@atimmer It's not really broadly applicable, since it's in our setup that there's a spy for console logging which is globally asserted to not have been called, which is valuable but adds complexity to _expecting_ the logging.

https://github.com/WordPress/gutenberg/blob/eb7b0b771924c6d0cae2cc001e7790caf8bfe9c6/test/unit/setup-test-framework.js#L25-L37

Nice idea. Let鈥檚 do it!

I have shared a proof of concept with #4285. Let me know if it meets your expectations :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mapk picture mapk  路  80Comments

afercia picture afercia  路  78Comments

DeveloperWil picture DeveloperWil  路  102Comments

azaozz picture azaozz  路  91Comments

tofumatt picture tofumatt  路  86Comments