Skip to content
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

Handle .not cases in toThrowWithMessage #173

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
99 changes: 73 additions & 26 deletions src/matchers/toThrowWithMessage/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,68 +1,115 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.toThrowWithMessage fails when a callback function is not a function 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>
exports[`.toThrowWithMessage fails when given an Error with a regex error message 1`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Received value must be a function but instead \\"2\\" was found"
Expected not to throw:
<green>[TypeError: /Expected message/]</>
Thrown:
<red>[TypeError: Expected message]</>
"
`;

exports[`.toThrowWithMessage fails when a callback provided doesnt throw an error 1`] = `
"Expected the function to throw an error.
But it didn't throw anything."
exports[`.toThrowWithMessage fails when given an Error with a string error message 1`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Expected not to throw:
<green>[TypeError: Expected message]</>
Thrown:
<red>[TypeError: Expected message]</>
"
`;

exports[`.toThrowWithMessage fails when a wrong type of error is thrown 1`] = `
exports[`.toThrowWithMessage fails when given an message regex and test case fails 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Expected to throw:
<green>[TypeError: Expected message]</>
<green>[TypeError: /Expected message/]</>
Thrown:
<red>[SyntaxError: Expected message]</>
<red>[TypeError: Expected]</>
"
`;

exports[`.toThrowWithMessage fails when callback function is not provided 1`] = `
exports[`.toThrowWithMessage fails when given an message string and test case fails 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Received value must be a function but instead \\"undefined\\" was found"
Expected to throw:
<green>[TypeError: Expected message]</>
Thrown:
<red>[TypeError: Expected]</>
"
`;

exports[`.toThrowWithMessage fails when error message is not provided 1`] = `
exports[`.toThrowWithMessage validation fails when a message is not provided 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Message argument is required. "
`;

exports[`.toThrowWithMessage fails when error message provided is not a string or regex 1`] = `
exports[`.toThrowWithMessage validation fails when a message is not provided 2`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Message argument is required. "
`;

exports[`.toThrowWithMessage validation fails when a message is not string or regexp 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Unexpected argument for message
Expected: \\"string\\" or \\"regexp
Got: \\"2\\""
`;

exports[`.toThrowWithMessage fails when error type is not provided 1`] = `
exports[`.toThrowWithMessage validation fails when a message is not string or regexp 2`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Unexpected argument for message
Expected: \\"string\\" or \\"regexp
Got: \\"2\\""
`;

exports[`.toThrowWithMessage validation fails when a type is not a function 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Expected type to be a function but instead \\"undefined\\" was found"
`;

exports[`.toThrowWithMessage passes when given an Error with a regex error message 1`] = `
exports[`.toThrowWithMessage validation fails when a type is not a function 2`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Expected not to throw:
<green>[TypeError: /Expected message/]</>
Thrown:
<red>[TypeError: Expected message]</>
"
Expected type to be a function but instead \\"undefined\\" was found"
`;

exports[`.toThrowWithMessage validation fails when callback function is not a function 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Received value must be a function but instead \\"2\\" was found"
`;

exports[`.toThrowWithMessage passes when given an Error with a string error message 1`] = `
exports[`.toThrowWithMessage validation fails when callback function is not a function 2`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Expected not to throw:
<green>[TypeError: Expected message]</>
Thrown:
<red>[TypeError: Expected message]</>
"
Received value must be a function but instead \\"2\\" was found"
`;

exports[`.toThrowWithMessage validation fails when callback function is not provided 1`] = `
"<dim>expect(</><red>function</><dim>).toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Received value must be a function but instead \\"undefined\\" was found"
`;

exports[`.toThrowWithMessage validation fails when callback function is not provided 2`] = `
"<dim>expect(</><red>function</><dim>).not.toThrowWithMessage(</><green>type</><dim>, </><green>message</><dim>)</>

Received value must be a function but instead \\"undefined\\" was found"
`;

exports[`.toThrowWithMessage validation fails when error not thrown 1`] = `
"Expected the function to throw an error.
But it didn't throw anything."
`;

exports[`.toThrowWithMessage validation fails when error not thrown 2`] = `
"Expected the function to throw an error.
But it didn't throw anything."
`;
25 changes: 11 additions & 14 deletions src/matchers/toThrowWithMessage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,34 @@ const failMessage = (received, expected) => () =>
` ${printReceived(received)}\n`;

export default {
toThrowWithMessage: (callback, type, message) => {
toThrowWithMessage(callback, type, message) {
const hint = this.isNot ? negativeHint : positiveHint;
if (!callback || typeof callback !== 'function') {
return {
pass: false,
message: () => positiveHint + '\n\n' + `Received value must be a function but instead "${callback}" was found`,
pass: this.isNot,
message: () => hint + '\n\n' + `Received value must be a function but instead "${callback}" was found`,
};
}

if (!type || typeof type !== 'function') {
return {
pass: false,
message: () => positiveHint + '\n\n' + `Expected type to be a function but instead "${type}" was found`,
pass: this.isNot,
message: () => hint + '\n\n' + `Expected type to be a function but instead "${type}" was found`,
};
}

if (!message) {
return {
pass: false,
message: () => positiveHint + '\n\n' + ' Message argument is required. ',
pass: this.isNot,
message: () => hint + '\n\n' + ' Message argument is required. ',
};
}

if (typeof message !== 'string' && !(message instanceof RegExp)) {
return {
pass: false,
pass: this.isNot,
message: () =>
positiveHint +
'\n\n' +
'Unexpected argument for message\n' +
'Expected: "string" or "regexp\n' +
`Got: "${message}"`,
hint + '\n\n' + 'Unexpected argument for message\n' + 'Expected: "string" or "regexp\n' + `Got: "${message}"`,
};
}

Expand All @@ -65,7 +62,7 @@ export default {

if (!error) {
return {
pass: false,
pass: this.isNot,
message: () => 'Expected the function to throw an error.\n' + "But it didn't throw anything.",
};
}
Expand Down
156 changes: 94 additions & 62 deletions src/matchers/toThrowWithMessage/index.test.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,117 @@
import matcher from './';
const { toThrowWithMessage } = matcher;

expect.extend(matcher);

describe('.toThrowWithMessage', () => {
test('fails when callback function is not provided', () => {
const { pass, message } = toThrowWithMessage();
expect(pass).toBe(false);
expect(message()).toMatchSnapshot();
});
describe('validation', () => {
test('fails when callback function is not provided', () => {
expect(() => expect().toThrowWithMessage(TypeError, 'Expected message')).toThrowErrorMatchingSnapshot();

test('fails when a callback function is not a function', () => {
const { pass, message } = toThrowWithMessage(2);
expect(pass).toBe(false);
expect(message()).toMatchSnapshot();
});
expect(() => expect().not.toThrowWithMessage(TypeError, 'Expected message')).toThrowErrorMatchingSnapshot();
});

test('fails when error message is not provided', () => {
const callback = () => {};
const { pass, message } = toThrowWithMessage(callback, Error);
expect(pass).toBe(false);
expect(message()).toMatchSnapshot();
});
test('fails when callback function is not a function', () => {
expect(() => expect(2).toThrowWithMessage(TypeError, 'Expected message')).toThrowErrorMatchingSnapshot();

test('fails when error type is not provided', () => {
const callback = () => {};
const { pass, message } = toThrowWithMessage(callback);
expect(pass).toBe(false);
expect(message()).toMatchSnapshot();
});
expect(() => expect(2).not.toThrowWithMessage(TypeError, 'Expected message')).toThrowErrorMatchingSnapshot();
});

test('fails when error not thrown', () => {
expect(() => expect(() => {}).toThrowWithMessage(TypeError, 'Expected message')).toThrowErrorMatchingSnapshot();

expect(() =>
expect(() => {}).not.toThrowWithMessage(TypeError, 'Expected message')
).toThrowErrorMatchingSnapshot();
});

test('fails when a type is not a function', () => {
expect(() =>
expect(() => {
throw new Error();
}).toThrowWithMessage()
).toThrowErrorMatchingSnapshot();

expect(() =>
expect(() => {
throw new Error();
}).not.toThrowWithMessage()
).toThrowErrorMatchingSnapshot();
});

test('fails when a message is not provided', () => {
expect(() =>
expect(() => {
throw new Error();
}).toThrowWithMessage(Error)
).toThrowErrorMatchingSnapshot();

expect(() =>
expect(() => {
throw new Error();
}).not.toThrowWithMessage(Error)
).toThrowErrorMatchingSnapshot();
});

test('fails when a message is not string or regexp', () => {
expect(() =>
expect(() => {
throw new Error();
}).toThrowWithMessage(Error, 2)
).toThrowErrorMatchingSnapshot();

test('fails when error message provided is not a string or regex', () => {
const callback = () => {};
const { pass, message } = toThrowWithMessage(callback, Error, 2);
expect(pass).toBe(false);
expect(message()).toMatchSnapshot();
expect(() =>
expect(() => {
throw new Error();
}).not.toThrowWithMessage(Error, 2)
).toThrowErrorMatchingSnapshot();
});
});

test('fails when a callback provided doesnt throw an error', () => {
const callback = () => {};
const { pass, message } = toThrowWithMessage(callback, Error, 'error');
expect(pass).toBe(false);
expect(message()).toMatchSnapshot();
test('passes when given an message string and test case passes', () => {
expect(() =>
expect(() => {
throw TypeError('Expected message');
}).toThrowWithMessage(TypeError, 'Expected message')
).not.toThrowError();
});

test('fails when a wrong type of error is thrown', () => {
const callback = () => {
throw SyntaxError('Expected message');
};
const { pass, message } = toThrowWithMessage(callback, TypeError, 'Expected message');
expect(pass).toBe(false);
expect(message()).toMatchSnapshot();
test('passes when given a message regex and test case passes', () => {
expect(() =>
expect(() => {
throw TypeError('Expected message');
}).toThrowWithMessage(TypeError, /Expected message/)
).not.toThrowError();
});

test('passes when given an Error with a string error message', () => {
const callback = () => {
throw TypeError('Expected message');
};
const { pass, message } = toThrowWithMessage(callback, TypeError, 'Expected message');
expect(pass).toBe(true);
expect(message()).toMatchSnapshot();
test('fails when given an message string and test case fails', () => {
expect(() =>
expect(() => {
throw TypeError('Expected');
}).toThrowWithMessage(TypeError, 'Expected message')
).toThrowErrorMatchingSnapshot();
});

test('passes when given an Error with a regex error message', () => {
const callback = () => {
throw TypeError('Expected message');
};
const { pass, message } = toThrowWithMessage(callback, TypeError, /Expected message/);
expect(pass).toBe(true);
expect(message()).toMatchSnapshot();
test('fails when given an message regex and test case fails', () => {
expect(() =>
expect(() => {
throw TypeError('Expected');
}).toThrowWithMessage(TypeError, /Expected message/)
).toThrowErrorMatchingSnapshot();
});

test('passes when given an Error with a string error message: end to end', () => {
expect(() => {
throw new TypeError('Expected message');
}).toThrowWithMessage(TypeError, 'Expected message');
test('fails when given an Error with a string error message', () => {
expect(() =>
expect(() => {
throw TypeError('Expected message');
}).not.toThrowWithMessage(TypeError, 'Expected message')
).toThrowErrorMatchingSnapshot();
});

test('passes when given an Error with a regex error message: end to end', () => {
expect(() => {
throw new SyntaxError('Expected message');
}).toThrowWithMessage(SyntaxError, /Expected message/);
test('fails when given an Error with a regex error message', () => {
expect(() =>
expect(() => {
throw TypeError('Expected message');
}).not.toThrowWithMessage(TypeError, /Expected message/)
).toThrowErrorMatchingSnapshot();
});
});