-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jest Console: Add new matchers for console.log and console.info (#137)
* Jest Console: Add new matchers for console.log and console.info * Jest-console: Update CHANGELOG with braking changes details
- Loading branch information
Showing
9 changed files
with
193 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const supportedMatchers = { | ||
error: 'toHaveErrored', | ||
info: 'toHaveInformed', | ||
log: 'toHaveLogged', | ||
warn: 'toHaveWarned', | ||
}; | ||
|
||
export default supportedMatchers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,76 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { forEach } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import '..'; | ||
|
||
describe( 'jest-console', () => { | ||
describe( 'console.error', () => { | ||
const message = 'This is error!'; | ||
|
||
test( 'toHaveErrored works', () => { | ||
console.error( message ); | ||
|
||
expect( console ).toHaveErrored(); | ||
} ); | ||
|
||
test( 'toHaveErrored works when not called', () => { | ||
expect( console ).not.toHaveErrored(); | ||
expect( | ||
() => expect( console ).toHaveErrored() | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( 'toHaveErroredWith works with arguments that match', () => { | ||
console.error( message ); | ||
|
||
expect( console ).toHaveErroredWith( message ); | ||
} ); | ||
|
||
test( 'toHaveErroredWith works when not called', () => { | ||
expect( console ).not.toHaveErroredWith( message ); | ||
expect( | ||
() => expect( console ).toHaveErroredWith( message ) | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( 'toHaveErroredWith works with many arguments that do not match', () => { | ||
console.error( 'Unknown message.' ); | ||
console.error( message, 'Unknown param.' ); | ||
|
||
expect( console ).not.toHaveErroredWith( message ); | ||
expect( | ||
() => expect( console ).toHaveErroredWith( message ) | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( 'assertions number gets incremented after every matcher call', () => { | ||
const spy = console.error; | ||
|
||
expect( spy.assertionsNumber ).toBe( 0 ); | ||
|
||
console.error( message ); | ||
|
||
expect( console ).toHaveErrored(); | ||
expect( spy.assertionsNumber ).toBe( 1 ); | ||
|
||
expect( console ).toHaveErroredWith( message ); | ||
expect( spy.assertionsNumber ).toBe( 2 ); | ||
} ); | ||
} ); | ||
|
||
describe( 'console.warn', () => { | ||
const message = 'This is warning!'; | ||
|
||
test( 'toHaveWarned works', () => { | ||
console.warn( message ); | ||
|
||
expect( console ).toHaveWarned(); | ||
} ); | ||
|
||
test( 'toHaveWarned works when not called', () => { | ||
expect( console ).not.toHaveWarned(); | ||
|
||
expect( | ||
() => expect( console ).toHaveWarned() | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( 'toHaveWarnedWith works with arguments that match', () => { | ||
console.warn( message ); | ||
|
||
expect( console ).toHaveWarnedWith( message ); | ||
} ); | ||
|
||
test( 'toHaveWarnedWith works when not called', () => { | ||
expect( console ).not.toHaveWarnedWith( message ); | ||
|
||
expect( | ||
() => expect( console ).toHaveWarnedWith( message ) | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( 'toHaveWarnedWith works with arguments that do not match', () => { | ||
console.warn( 'Unknown message.' ); | ||
console.warn( message, 'Unknown param.' ); | ||
|
||
expect( console ).not.toHaveWarnedWith( message ); | ||
|
||
expect( | ||
() => expect( console ).toHaveWarnedWith( message ) | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( 'assertions number gets incremented after every matcher call', () => { | ||
const spy = console.warn; | ||
|
||
expect( spy.assertionsNumber ).toBe( 0 ); | ||
|
||
console.warn( message ); | ||
|
||
expect( console ).toHaveWarned(); | ||
expect( spy.assertionsNumber ).toBe( 1 ); | ||
|
||
expect( console ).toHaveWarnedWith( message ); | ||
expect( spy.assertionsNumber ).toBe( 2 ); | ||
} ); | ||
} ); | ||
forEach( | ||
{ | ||
error: 'toHaveErrored', | ||
info: 'toHaveInformed', | ||
log: 'toHaveLogged', | ||
warn: 'toHaveWarned', | ||
}, | ||
( matcherName, methodName ) => { | ||
describe( `console.${ methodName }`, () => { | ||
const matcherNameWith = `${ matcherName}With`; | ||
const message = `This is ${ methodName }!`; | ||
|
||
test( '${ matcherName } works', () => { | ||
console[ methodName ]( message ); | ||
|
||
expect( console )[ matcherName ](); | ||
} ); | ||
|
||
test( `${ matcherName } works when not called`, () => { | ||
expect( console ).not[ matcherName ](); | ||
expect( | ||
() => expect( console )[ matcherName ]() | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( `${ matcherNameWith } works with arguments that match`, () => { | ||
console[ methodName ]( message ); | ||
|
||
expect( console )[ matcherNameWith ]( message ); | ||
} ); | ||
|
||
test( `${ matcherNameWith } works when not called`, () => { | ||
expect( console ).not[ matcherNameWith ]( message ); | ||
expect( | ||
() => expect( console )[ matcherNameWith ]( message ) | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( `${ matcherNameWith } works with many arguments that do not match`, () => { | ||
console[ methodName ]( 'Unknown message.' ); | ||
console[ methodName ]( message, 'Unknown param.' ); | ||
|
||
expect( console ).not[ matcherNameWith ]( message ); | ||
expect( | ||
() => expect( console )[ matcherNameWith ]( message ) | ||
).toThrowErrorMatchingSnapshot(); | ||
} ); | ||
|
||
test( 'assertions number gets incremented after every matcher call', () => { | ||
const spy = console[ methodName ]; | ||
|
||
expect( spy.assertionsNumber ).toBe( 0 ); | ||
|
||
console[ methodName ]( message ); | ||
|
||
expect( console )[ matcherName ](); | ||
expect( spy.assertionsNumber ).toBe( 1 ); | ||
|
||
expect( console )[ matcherNameWith ]( message ); | ||
expect( spy.assertionsNumber ).toBe( 2 ); | ||
} ); | ||
} ); | ||
} | ||
); | ||
} ); |
Oops, something went wrong.