Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Packages: jest-console: Capture logging in test lifecycle
  • Loading branch information
aduth committed Sep 21, 2018
commit b3135fad95a82a2e35ea2a194e43f533a5a8ecdb
6 changes: 6 additions & 0 deletions packages/jest-console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.0.1 (Unreleased)

### Bug Fixes

- Captures and protects against unexpected console logging occurring during lifecycle.

## 2.0.0 (2018-07-12)

### Breaking Change
Expand Down
21 changes: 18 additions & 3 deletions packages/jest-console/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,31 @@ import supportedMatchers from './supported-matchers';
const setConsoleMethodSpy = ( matcherName, methodName ) => {
const spy = jest.spyOn( console, methodName ).mockName( `console.${ methodName }` );

beforeEach( () => {
/**
* Resets the spy to its initial state.
*/
function resetSpy() {
spy.mockReset();
spy.assertionsNumber = 0;
} );
}

afterEach( () => {
/**
* Verifies that the spy has only been called if expected.
*/
function assertExpectedCalls() {
if ( spy.assertionsNumber === 0 && spy.mock.calls.length > 0 ) {
expect( console ).not[ matcherName ]();
}
}

beforeAll( resetSpy );

beforeEach( () => {
assertExpectedCalls();
resetSpy();
} );

afterEach( assertExpectedCalls );
};

forEach( supportedMatchers, setConsoleMethodSpy );
12 changes: 12 additions & 0 deletions packages/jest-console/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ describe( 'jest-console', () => {
expect( console )[ matcherNameWith ]( message );
expect( spy.assertionsNumber ).toBe( 2 );
} );

describe( 'lifecycle', () => {
beforeAll( () => {
// This is a difficult one to test, since the matcher's
// own lifecycle is defined to run before ours. Infer
// that we're being watched by testing the console
// method as being a spy.
expect( console[ methodName ].assertionsNumber ).toBeGreaterThanOrEqual( 0 );
} );

it( 'captures logging in lifecycle', () => {} );
} );
} );
}
);
Expand Down