Skip to content

Commit

Permalink
Create pass/fail error messages only if required (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jan 2, 2023
1 parent 2b23769 commit 30a420e
Show file tree
Hide file tree
Showing 71 changed files with 1,057 additions and 1,016 deletions.
25 changes: 13 additions & 12 deletions src/matchers/toBeAfter.js
@@ -1,18 +1,19 @@
export function toBeAfter(date, after) {
const { printReceived, matcherHint } = this.utils;
const passMessage =
matcherHint('.not.toBeAfter', 'received', '') +
'\n\n' +
`Expected date to be after ${printReceived(after)} but received:\n` +
` ${printReceived(date)}`;

const failMessage =
matcherHint('.toBeAfter', 'received', '') +
'\n\n' +
`Expected date to be after ${printReceived(after)} but received:\n` +
` ${printReceived(date)}`;

const pass = date > after;

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeAfter', 'received', '') +
'\n\n' +
`Expected date to be after ${printReceived(after)} but received:\n` +
` ${printReceived(date)}`
: matcherHint('.toBeAfter', 'received', '') +
'\n\n' +
`Expected date to be after ${printReceived(after)} but received:\n` +
` ${printReceived(date)}`,
};
}
26 changes: 13 additions & 13 deletions src/matchers/toBeAfterOrEqualTo.js
@@ -1,19 +1,19 @@
export function toBeAfterOrEqualTo(actual, expected) {
const { printReceived, matcherHint } = this.utils;

const passMessage =
matcherHint('.not.toBeAfterOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be after or equal to ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`;

const failMessage =
matcherHint('.toBeAfterOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be after or equal to ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`;

const pass = actual >= expected;

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeAfterOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be after or equal to ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`
: matcherHint('.toBeAfterOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be after or equal to ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`,
};
}
26 changes: 13 additions & 13 deletions src/matchers/toBeArray.js
@@ -1,19 +1,19 @@
export function toBeArray(expected) {
const { matcherHint, printReceived } = this.utils;

const passMessage =
matcherHint('.not.toBeArray', 'received', '') +
'\n\n' +
'Expected value to not be an array received:\n' +
` ${printReceived(expected)}`;

const failMessage =
matcherHint('.toBeArray', 'received', '') +
'\n\n' +
'Expected value to be an array received:\n' +
` ${printReceived(expected)}`;

const pass = Array.isArray(expected);

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeArray', 'received', '') +
'\n\n' +
'Expected value to not be an array received:\n' +
` ${printReceived(expected)}`
: matcherHint('.toBeArray', 'received', '') +
'\n\n' +
'Expected value to be an array received:\n' +
` ${printReceived(expected)}`,
};
}
36 changes: 19 additions & 17 deletions src/matchers/toBeArrayOfSize.js
Expand Up @@ -3,23 +3,25 @@ import { determinePropertyMessage } from '../utils';
export function toBeArrayOfSize(actual, expected) {
const { printExpected, printReceived, matcherHint } = this.utils;

const passMessage = `${matcherHint('.not.toBeArrayOfSize')}
Expected value to not be an array of size:
${printExpected(expected)}
Received:
value: ${printReceived(actual)}
length: ${printReceived(determinePropertyMessage(actual, 'length'))}`;

const failMessage = `${matcherHint('.toBeArrayOfSize')}
Expected value to be an array of size:
${printExpected(expected)}
Received:
value: ${printReceived(actual)}
length: ${printReceived(determinePropertyMessage(actual, 'length'))}`;

const pass = Array.isArray(actual) && actual.length === expected;

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeArrayOfSize') +
'\n\n' +
'Expected value to not be an array of size:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` value: ${printReceived(actual)}\n` +
` length: ${printReceived(determinePropertyMessage(actual, 'length'))}`
: matcherHint('.toBeArrayOfSize') +
'\n\n' +
'Expected value to be an array of size:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` value: ${printReceived(actual)}\n` +
` length: ${printReceived(determinePropertyMessage(actual, 'length'))}`,
};
}
25 changes: 13 additions & 12 deletions src/matchers/toBeBefore.js
@@ -1,18 +1,19 @@
export function toBeBefore(actual, expected) {
const { matcherHint, printReceived } = this.utils;
const passMessage =
matcherHint('.not.toBeBefore', 'received', '') +
'\n\n' +
`Expected date to be before ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`;

const failMessage =
matcherHint('.toBeBefore', 'received', '') +
'\n\n' +
`Expected date to be before ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`;

const pass = actual < expected;

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeBefore', 'received', '') +
'\n\n' +
`Expected date to be before ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`
: matcherHint('.toBeBefore', 'received', '') +
'\n\n' +
`Expected date to be before ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`,
};
}
26 changes: 13 additions & 13 deletions src/matchers/toBeBeforeOrEqualTo.js
@@ -1,19 +1,19 @@
export function toBeBeforeOrEqualTo(actual, expected) {
const { matcherHint, printReceived } = this.utils;

const passMessage =
matcherHint('.not.toBeBeforeOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be before or equal to ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`;

const failMessage =
matcherHint('.toBeBeforeOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be before or equal to ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`;

const pass = actual <= expected;

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeBeforeOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be before or equal to ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`
: matcherHint('.toBeBeforeOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be before or equal to ${printReceived(expected)} but received:\n` +
` ${printReceived(actual)}`,
};
}
26 changes: 13 additions & 13 deletions src/matchers/toBeBetween.js
@@ -1,19 +1,19 @@
export function toBeBetween(actual, startDate, endDate) {
const { matcherHint, printReceived } = this.utils;

const passMessage =
matcherHint('.not.toBeBetween', 'received', '') +
'\n\n' +
`Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` +
` ${printReceived(actual)}`;

const failMessage =
matcherHint('.toBeBetween', 'received', '') +
'\n\n' +
`Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` +
` ${printReceived(actual)}`;

const pass = actual >= startDate && actual <= endDate;

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeBetween', 'received', '') +
'\n\n' +
`Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` +
` ${printReceived(actual)}`
: matcherHint('.toBeBetween', 'received', '') +
'\n\n' +
`Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` +
` ${printReceived(actual)}`,
};
}
26 changes: 13 additions & 13 deletions src/matchers/toBeBoolean.js
@@ -1,19 +1,19 @@
export function toBeBoolean(actual) {
const { matcherHint, printReceived } = this.utils;

const passMessage =
matcherHint('.not.toBeBoolean', 'received', '') +
'\n\n' +
'Expected value to not be of type boolean, received:\n' +
` ${printReceived(actual)}`;

const failMessage =
matcherHint('.toBeBoolean', 'received', '') +
'\n\n' +
'Expected value to be of type boolean, received:\n' +
` ${printReceived(actual)}`;

const pass = typeof actual === 'boolean' || actual instanceof Boolean;

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeBoolean', 'received', '') +
'\n\n' +
'Expected value to not be of type boolean, received:\n' +
` ${printReceived(actual)}`
: matcherHint('.toBeBoolean', 'received', '') +
'\n\n' +
'Expected value to be of type boolean, received:\n' +
` ${printReceived(actual)}`,
};
}
26 changes: 13 additions & 13 deletions src/matchers/toBeDate.js
Expand Up @@ -3,19 +3,19 @@ import { getType } from 'jest-get-type';
export function toBeDate(actual) {
const { matcherHint, printReceived } = this.utils;

const passMessage =
matcherHint('.not.toBeDate', 'received', '') +
'\n\n' +
'Expected value to not be a date received:\n' +
` ${printReceived(actual)}`;

const failMessage =
matcherHint('.toBeDate', 'received', '') +
'\n\n' +
'Expected value to be a date received:\n' +
` ${printReceived(actual)}`;

const pass = getType(actual) === 'date' && !isNaN(actual);

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeDate', 'received', '') +
'\n\n' +
'Expected value to not be a date received:\n' +
` ${printReceived(actual)}`
: matcherHint('.toBeDate', 'received', '') +
'\n\n' +
'Expected value to be a date received:\n' +
` ${printReceived(actual)}`,
};
}
26 changes: 13 additions & 13 deletions src/matchers/toBeDateString.js
@@ -1,19 +1,19 @@
export function toBeDateString(actual) {
const { matcherHint, printReceived } = this.utils;

const passMessage =
matcherHint('.not.toBeDateString', 'received', '') +
'\n\n' +
'Expected value to not be a date string received:\n' +
` ${printReceived(actual)}`;

const failMessage =
matcherHint('.toBeDateString', 'received', '') +
'\n\n' +
'Expected value to be a date string received:\n' +
` ${printReceived(actual)}`;

const pass = !isNaN(Date.parse(actual));

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeDateString', 'received', '') +
'\n\n' +
'Expected value to not be a date string received:\n' +
` ${printReceived(actual)}`
: matcherHint('.toBeDateString', 'received', '') +
'\n\n' +
'Expected value to be a date string received:\n' +
` ${printReceived(actual)}`,
};
}
26 changes: 13 additions & 13 deletions src/matchers/toBeEmpty.js
@@ -1,21 +1,21 @@
export function toBeEmpty(actual) {
const { printReceived, matcherHint } = this.utils;

const passMessage =
matcherHint('.not.toBeEmpty', 'received', '') +
'\n\n' +
'Expected value to not be empty received:\n' +
` ${printReceived(actual)}`;

const failMessage =
matcherHint('.toBeEmpty', 'received', '') +
'\n\n' +
'Expected value to be empty received:\n' +
` ${printReceived(actual)}`;

const pass = this.equals({}, actual) || isEmptyIterable(actual);

return { pass, message: () => (pass ? passMessage : failMessage) };
return {
pass,
message: () =>
pass
? matcherHint('.not.toBeEmpty', 'received', '') +
'\n\n' +
'Expected value to not be empty received:\n' +
` ${printReceived(actual)}`
: matcherHint('.toBeEmpty', 'received', '') +
'\n\n' +
'Expected value to be empty received:\n' +
` ${printReceived(actual)}`,
};
}

const isEmptyIterable = value => {
Expand Down

0 comments on commit 30a420e

Please sign in to comment.