Skip to content

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toBeBoolean()](#tobeboolean)
- [.toBeTrue()](#tobetrue)
- [.toBeFalse()](#tobefalse)
- [Console](#console)
- [.toLog()](#tolog)
- [Date](#date)
- [.toBeDate()](#tobedate)
- [.toBeValidDate()](#tobevaliddate)
Expand Down Expand Up @@ -356,6 +358,21 @@ test('returns false', () => {
});
```

### Console

#### .toLog()

Use `.toLog` when checking if a callback function outputs a message to the console. The specified console method is mocked during execution so nothing will be printed to the console.

```js
expect(() => {
console.log('a message');
}).toLog('a message');

expect(someFunction).toLog('some deprecation warning', 'warn')
expect(someOtherFunction).not.toLog('an error message', 'error')
```

### ~~Date~~

Proposal in #117 (*under development*)
Expand Down
36 changes: 36 additions & 0 deletions src/matchers/toLog/__snapshots__/index.test.js.snap
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\\"]</>"
`;
30 changes: 30 additions & 0 deletions src/matchers/toLog/index.js
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) };
}
};
45 changes: 45 additions & 0 deletions src/matchers/toLog/index.test.js
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();
});
});
17 changes: 17 additions & 0 deletions src/matchers/toLog/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { equals } from 'expect/build/jasmine_utils';
Copy link
Collaborator

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 of 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 };
};
18 changes: 18 additions & 0 deletions src/matchers/toLog/predicate.test.js
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']);
});
});
27 changes: 27 additions & 0 deletions src/matchers/toLogError/__snapshots__/index.test.js.snap
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\\"]</>"
`;
30 changes: 30 additions & 0 deletions src/matchers/toLogError/index.js
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) };
}
};
30 changes: 30 additions & 0 deletions src/matchers/toLogError/index.test.js
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();
});
});
17 changes: 17 additions & 0 deletions src/matchers/toLogError/predicate.js
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 };
};
18 changes: 18 additions & 0 deletions src/matchers/toLogError/predicate.test.js
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']);
});
});
27 changes: 27 additions & 0 deletions src/matchers/toLogInfo/__snapshots__/index.test.js.snap
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\\"]</>"
`;
30 changes: 30 additions & 0 deletions src/matchers/toLogInfo/index.js
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) };
}
};
30 changes: 30 additions & 0 deletions src/matchers/toLogInfo/index.test.js
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();
});
});
Loading