-
-
Notifications
You must be signed in to change notification settings - Fork 225
feat: Add .toLog() matcher #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ollelauribostrom
wants to merge
3
commits into
jest-community:main
Choose a base branch
from
ollelauribostrom:tolog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,36 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toLog fails when the callback function outputs the expected message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toLog(</><green>expected</><dim>)</> | ||
|
||
Expected console.log not to have been called with: | ||
<green>\\"console-log\\"</> | ||
But it was called with: | ||
<red>[\\"console-log\\"]</>" | ||
`; | ||
|
||
exports[`.toLog fails when the callback function does not output any message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toLog(</><green>expected</><dim>)</> | ||
|
||
Expected console.log to have been called with: | ||
<green>\\"something\\"</> | ||
But it was not called" | ||
`; | ||
|
||
exports[`.toLog fails when the callback function does not output the expected message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toLog(</><green>expected</><dim>)</> | ||
|
||
Expected console.log to have been called with: | ||
<green>\\"something\\"</> | ||
But it was called with: | ||
<red>[\\"console-log\\"]</>" | ||
`; | ||
|
||
exports[`.toLog fails when the callback function does not output the expected message 2`] = ` | ||
"<dim>expect(</><red>received</><dim>).toLog(</><green>expected</><dim>)</> | ||
|
||
Expected console.log to have been called with: | ||
<green>\\"something\\"</> | ||
But it was called with: | ||
<red>[\\"a\\", \\"b\\", \\"c\\", \\"console-log\\"]</>" | ||
`; |
This file contains hidden or 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,30 @@ | ||
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'; | ||
|
||
import predicate from './predicate'; | ||
|
||
const calledWith = actual => | ||
actual ? 'But it was called with:\n' + ` ${printReceived(actual)}` : 'But it was not called'; | ||
|
||
const passMessage = (actual, expected) => () => | ||
matcherHint('.not.toLog') + | ||
'\n\n' + | ||
'Expected console.log not to have been called with:\n' + | ||
` ${printExpected(expected)}\n` + | ||
calledWith(actual); | ||
|
||
const failMessage = (actual, expected) => () => | ||
matcherHint('.toLog') + | ||
'\n\n' + | ||
'Expected console.log to have been called with:\n' + | ||
` ${printExpected(expected)}\n` + | ||
calledWith(actual); | ||
|
||
export default { | ||
toLog: (fn, expected) => { | ||
const { actual, pass } = predicate(fn, expected); | ||
if (pass) { | ||
return { pass: true, message: passMessage(actual, expected) }; | ||
} | ||
return { pass: false, message: failMessage(actual, expected) }; | ||
} | ||
}; |
This file contains hidden or 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,45 @@ | ||
import matcher from './'; | ||
|
||
expect.extend(matcher); | ||
|
||
const log = () => console.log('console-log'); // eslint-disable-line | ||
const noop = () => {}; | ||
|
||
describe('.toLog', () => { | ||
test('passes when the callback function outputs the expected message to the console', () => { | ||
expect(log).toLog('console-log'); | ||
expect(() => console.log(1)).toLog(1); // eslint-disable-line | ||
expect(() => console.log([1, 2])).toLog([1, 2]); // eslint-disable-line | ||
expect(() => console.log({ a: 1, b: 2 })).toLog({ a: 1, b: 2 }); // eslint-disable-line | ||
}); | ||
test('fails when the callback function does not output any message', () => { | ||
expect(() => expect(noop).toLog('something')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
test('fails when the callback function does not output the expected message', () => { | ||
expect(() => expect(log).toLog('something')).toThrowErrorMatchingSnapshot(); | ||
expect(() => | ||
expect(() => { | ||
console.log('a', 'b', 'c'); // eslint-disable-line | ||
log(); | ||
}).toLog('something') | ||
).toThrowErrorMatchingSnapshot(); | ||
}); | ||
test('passes when the callback function outputs the expected message among others', () => { | ||
expect(() => { | ||
console.log('something'); // eslint-disable-line | ||
log(); | ||
}).toLog('console-log'); | ||
}); | ||
}); | ||
|
||
describe('.not.toLog', () => { | ||
test('passes when the callback function does not output anything', () => { | ||
expect(noop).not.toLog('something'); | ||
}); | ||
test('passes when the callback function does not output the expected message', () => { | ||
expect(log).not.toLog('something'); | ||
}); | ||
test('fails when the callback function outputs the expected message', () => { | ||
expect(() => expect(log).not.toLog('console-log')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
This file contains hidden or 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,17 @@ | ||
import { equals } from 'expect/build/jasmine_utils'; | ||
|
||
export default (fn, expected) => { | ||
const log = console.log; // eslint-disable-line no-console | ||
const actual = []; | ||
console.log = (...args) => actual.push(...args); // eslint-disable-line no-console | ||
try { | ||
fn(); | ||
} finally { | ||
console.log = log; // eslint-disable-line no-console | ||
} | ||
if (actual.length < 1) { | ||
return { pass: false }; | ||
} | ||
const pass = actual.some(message => equals(message, expected)); | ||
return { actual, pass }; | ||
}; |
This file contains hidden or 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,18 @@ | ||
import predicate from './predicate'; | ||
|
||
const fn = () => console.log('console-log'); // eslint-disable-line | ||
|
||
describe('Predicate > .toLog', () => { | ||
test('returns an object with pass status and the actual output', () => { | ||
const expected = 'console-log'; | ||
const { pass, actual } = predicate(fn, expected); | ||
expect(pass).toEqual(true); | ||
expect(actual).toEqual(['console-log']); | ||
}); | ||
it('returns an object with fail status and the actual output', () => { | ||
const expected = 'something'; | ||
const { pass, actual } = predicate(fn, expected); | ||
expect(pass).toEqual(false); | ||
expect(actual).toEqual(['console-log']); | ||
}); | ||
}); |
This file contains hidden or 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,27 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toLogError fails when the callback function outputs the expected message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toLogError(</><green>expected</><dim>)</> | ||
|
||
Expected console.error not to have been called with: | ||
<green>\\"console-error\\"</> | ||
But it was called with: | ||
<red>[\\"console-error\\"]</>" | ||
`; | ||
|
||
exports[`.toLogError fails when the callback function does not output any message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toLogError(</><green>expected</><dim>)</> | ||
|
||
Expected console.error to have been called with: | ||
<green>\\"something\\"</> | ||
But it was not called" | ||
`; | ||
|
||
exports[`.toLogError fails when the callback function does not output the expected message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toLogError(</><green>expected</><dim>)</> | ||
|
||
Expected console.error to have been called with: | ||
<green>\\"something\\"</> | ||
But it was called with: | ||
<red>[\\"console-error\\"]</>" | ||
`; |
This file contains hidden or 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,30 @@ | ||
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'; | ||
|
||
import predicate from './predicate'; | ||
|
||
const calledWith = actual => | ||
actual ? 'But it was called with:\n' + ` ${printReceived(actual)}` : 'But it was not called'; | ||
|
||
const passMessage = (actual, expected) => () => | ||
matcherHint('.not.toLogError') + | ||
'\n\n' + | ||
'Expected console.error not to have been called with:\n' + | ||
` ${printExpected(expected)}\n` + | ||
calledWith(actual); | ||
|
||
const failMessage = (actual, expected) => () => | ||
matcherHint('.toLogError') + | ||
'\n\n' + | ||
'Expected console.error to have been called with:\n' + | ||
` ${printExpected(expected)}\n` + | ||
calledWith(actual); | ||
|
||
export default { | ||
toLogError: (fn, expected) => { | ||
const { actual, pass } = predicate(fn, expected); | ||
if (pass) { | ||
return { pass: true, message: passMessage(actual, expected) }; | ||
} | ||
return { pass: false, message: failMessage(actual, expected) }; | ||
} | ||
}; |
This file contains hidden or 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,30 @@ | ||
import matcher from './'; | ||
|
||
expect.extend(matcher); | ||
|
||
const error = () => console.error('console-error'); // eslint-disable-line | ||
const noop = () => {}; | ||
|
||
describe('.toLogError', () => { | ||
test('passes when the callback function outputs the expected message to the console', () => { | ||
expect(error).toLogError('console-error'); | ||
}); | ||
test('fails when the callback function does not output any message', () => { | ||
expect(() => expect(noop).toLogError('something')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
test('fails when the callback function does not output the expected message', () => { | ||
expect(() => expect(error).toLogError('something')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toLogError', () => { | ||
test('passes when the callback function does not output anything', () => { | ||
expect(noop).not.toLogError('something'); | ||
}); | ||
test('passes when the callback function does not output the expected message', () => { | ||
expect(error).not.toLogError('something'); | ||
}); | ||
test('fails when the callback function outputs the expected message', () => { | ||
expect(() => expect(error).not.toLogError('console-error')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
This file contains hidden or 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,17 @@ | ||
import { equals } from 'expect/build/jasmine_utils'; | ||
|
||
export default (fn, expected) => { | ||
const error = console.error; // eslint-disable-line no-console | ||
const actual = []; | ||
console.error = (...args) => actual.push(...args); // eslint-disable-line no-console | ||
try { | ||
fn(); | ||
} finally { | ||
console.error = error; // eslint-disable-line no-console | ||
} | ||
if (actual.length < 1) { | ||
return { pass: false }; | ||
} | ||
const pass = actual.some(message => equals(message, expected)); | ||
return { actual, pass }; | ||
}; |
This file contains hidden or 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,18 @@ | ||
import predicate from './predicate'; | ||
|
||
const fn = () => console.error('console-error'); // eslint-disable-line | ||
|
||
describe('Predicate > .toLogError', () => { | ||
test('returns an object with pass status and the actual output', () => { | ||
const expected = 'console-error'; | ||
const { pass, actual } = predicate(fn, expected); | ||
expect(pass).toEqual(true); | ||
expect(actual).toEqual(['console-error']); | ||
}); | ||
it('returns an object with fail status and the actual output', () => { | ||
const expected = 'something'; | ||
const { pass, actual } = predicate(fn, expected); | ||
expect(pass).toEqual(false); | ||
expect(actual).toEqual(['console-error']); | ||
}); | ||
}); |
This file contains hidden or 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,27 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toLogInfo fails when the callback function outputs the expected message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toLogInfo(</><green>expected</><dim>)</> | ||
|
||
Expected console.info not to have been called with: | ||
<green>\\"console-info\\"</> | ||
But it was called with: | ||
<red>[\\"console-info\\"]</>" | ||
`; | ||
|
||
exports[`.toLogInfo fails when the callback function does not output any message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toLogInfo(</><green>expected</><dim>)</> | ||
|
||
Expected console.info to have been called with: | ||
<green>\\"something\\"</> | ||
But it was not called" | ||
`; | ||
|
||
exports[`.toLogInfo fails when the callback function does not output the expected message 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toLogInfo(</><green>expected</><dim>)</> | ||
|
||
Expected console.info to have been called with: | ||
<green>\\"something\\"</> | ||
But it was called with: | ||
<red>[\\"console-info\\"]</>" | ||
`; |
This file contains hidden or 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,30 @@ | ||
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'; | ||
|
||
import predicate from './predicate'; | ||
|
||
const calledWith = actual => | ||
actual ? 'But it was called with:\n' + ` ${printReceived(actual)}` : 'But it was not called'; | ||
|
||
const passMessage = (actual, expected) => () => | ||
matcherHint('.not.toLogInfo') + | ||
'\n\n' + | ||
'Expected console.info not to have been called with:\n' + | ||
` ${printExpected(expected)}\n` + | ||
calledWith(actual); | ||
|
||
const failMessage = (actual, expected) => () => | ||
matcherHint('.toLogInfo') + | ||
'\n\n' + | ||
'Expected console.info to have been called with:\n' + | ||
` ${printExpected(expected)}\n` + | ||
calledWith(actual); | ||
|
||
export default { | ||
toLogInfo: (fn, expected) => { | ||
const { actual, pass } = predicate(fn, expected); | ||
if (pass) { | ||
return { pass: true, message: passMessage(actual, expected) }; | ||
} | ||
return { pass: false, message: failMessage(actual, expected) }; | ||
} | ||
}; |
This file contains hidden or 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,30 @@ | ||
import matcher from './'; | ||
|
||
expect.extend(matcher); | ||
|
||
const info = () => console.info('console-info'); // eslint-disable-line | ||
const noop = () => {}; | ||
|
||
describe('.toLogInfo', () => { | ||
test('passes when the callback function outputs the expected message to the console', () => { | ||
expect(info).toLogInfo('console-info'); | ||
}); | ||
test('fails when the callback function does not output any message', () => { | ||
expect(() => expect(noop).toLogInfo('something')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
test('fails when the callback function does not output the expected message', () => { | ||
expect(() => expect(info).toLogInfo('something')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toLogInfo', () => { | ||
test('passes when the callback function does not output anything', () => { | ||
expect(noop).not.toLogInfo('something'); | ||
}); | ||
test('passes when the callback function does not output the expected message', () => { | ||
expect(info).not.toLogInfo('something'); | ||
}); | ||
test('fails when the callback function outputs the expected message', () => { | ||
expect(() => expect(info).not.toLogInfo('console-info')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this path used to be correct, but it's now
jasmineUtils
instead ofjasmine_utils
.