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

Allow customMessage to be a function that returns message #59

Open
wants to merge 1 commit 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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Expect takes at most one argument.

## Solution

`jest-expect-message` allows you to call `expect` with a second argument of a `String` message.
`jest-expect-message` allows you to call `expect` with a second argument of a `String` message or `function`.

For example the same test as above:

Expand All @@ -49,6 +49,15 @@ test('returns 2 when adding 1 and 1', () => {
});
```

Or you can pass a function that generates and returns the extra message:

```js
test('returns 2 when adding 1 and 1', () => {
const context = 2;
expect(1 + 1, () => (`Woah this should be ${context}!`)).toBe(3);
});
```

With `jest-expect-message` this will fail with your custom error message:

```sh
Expand Down
7 changes: 7 additions & 0 deletions src/__snapshots__/withMessage.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`withMessage() calls custom message function when matcher fails 1`] = `
"Custom message:
custom message from function

expected ACTUAL to be 1"
`;

exports[`withMessage() throws error with custom message when matcher fails 1`] = `
"Custom message:
should fail
Expand Down
5 changes: 3 additions & 2 deletions src/withMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class JestAssertionError extends Error {
}
}

const wrapMatcher = (matcher, customMessage, config) => {
const wrapMatcher = (matcher, customMessageOrFn, config) => {
const newMatcher = (...args) => {
try {
const result = matcher(...args);
Expand All @@ -30,7 +30,8 @@ const wrapMatcher = (matcher, customMessage, config) => {
throw error;
}
const { matcherResult } = error;


const customMessage = typeof customMessageOrFn === 'function' ? customMessageOrFn() : customMessageOrFn;
if (typeof customMessage !== 'string' || customMessage.length < 1) {
throw new JestAssertionError(matcherResult, newMatcher);
}
Expand Down
31 changes: 31 additions & 0 deletions src/withMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ describe('withMessage()', () => {
}
});

test('calls custom message function when matcher fails', () => {
expect.assertions(5);
const originalError = new Error('Boo');
originalError.matcherResult = {
actual: ACTUAL,
expected: 1,
message: () => 'expected ACTUAL to be 1',
pass: false
};

const toBeMock = jest.fn(() => {
throw originalError;
});
const expectMock = jest.fn(() => ({ toBe: toBeMock }));

const customMessageFn = jest.fn(() => 'custom message from function');
try {
withMessage(expectMock)(ACTUAL, customMessageFn).toBe(1);
} catch (e) {
expect(e.matcherResult).toMatchObject({
actual: ACTUAL,
expected: 1,
pass: false
});
expect(e.message).toMatchSnapshot();
expect(customMessageFn).toHaveBeenCalled();
expect(expectMock).toHaveBeenCalledWith(ACTUAL);
expect(toBeMock).toHaveBeenCalledWith(1);
}
});

test('throws error with custom message when not matcher fails', () => {
expect.assertions(4);
const originalError = new Error('Boo');
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ declare namespace jest {
interface Expect {
<T = any>(
actual: T,
message?: string,
message?: string | (() => string),
options?: { showMatcherMessage?: boolean; showPrefix?: boolean; showStack?: boolean }
): JestMatchers<T>;
}
Expand Down