
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
jest-expect-message
Advanced tools
🃏🗯
Add custom message to Jest expects
In many testing libraries it is possible to supply a custom message for a given expectation, this is currently not possible in Jest.
For example:
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
This will throw the following error in Jest:
Expect takes at most one argument.
jest-expect-message
allows you to call expect
with a second argument of a String
message.
For example the same test as above:
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
With jest-expect-message
this will fail with your custom error message:
● returns 2 when adding 1 and 1
Custom message:
Woah this should be 2!
expect(received).toBe(expected) // Object.is equality
Expected: 3
Received: 2
With npm:
npm install --save-dev jest-expect-message
With yarn:
yarn add -D jest-expect-message
Add jest-expect-message
to your Jest setupFilesAfterEnv
configuration.
See for help
"jest": {
"setupFilesAfterEnv": ["jest-expect-message"]
}
"jest": {
"setupTestFrameworkScriptFile": "jest-expect-message"
}
If you have a custom setup file and want to use this library then add the following to your setup file.
import 'jest-expect-message';
Add the following entry to your tsconfig to enable Typescript support.
"files": ["node_modules/jest-expect-message/types/index.d.ts"],
Custom message example with typescript
"rules": {
"jest/valid-expect": [
"error",
{
"maxArgs": 2
}
]
}
expect(actual, message, options?)
actual
: The value you would normally pass into an expect
to assert against with a given matcher.message
: String, the custom message you want to be printed should the expect
fail.options
: An optional object that controls what is shown as part of the custom message.
showPrefix: boolean
: If false
will not show the Custom message:
prefix. Default: true
showMatcherMessage: boolean
: If false
will not show the matchers original error message. Default: true
showStack: boolean
: If false
will not show the matchers stack trace. Default: true
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
● returns 2 when adding 1 and 1
Custom message:
Woah this should be 2!
expect(received).toBe(expected) // Object.is equality
Expected: 3
Received: 2
1 | test('returns 2 when adding 1 and 1', () => {
> 2 | expect(1 + 1, 'Woah this should be 2!').toBe(3);
| ^
3 | });
*/
false
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!', { showPrefix: false }).toBe(3);
});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
● returns 2 when adding 1 and 1
Woah this should be 2!
expect(received).toBe(expected) // Object.is equality
Expected: 3
Received: 2
1 | test('returns 2 when adding 1 and 1', () => {
> 2 | expect(1 + 1, 'Woah this should be 2!', { showPrefix: false }).toBe(3);
| ^
3 | });
*/
false
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!', { showMatcherMessage: false }).toBe(3);
});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
● returns 2 when adding 1 and 1
Custom message:
Woah this should be 2!
1 | test('returns 2 when adding 1 and 1', () => {
> 2 | expect(1 + 1, 'Woah this should be 2!', { showMatcherMessage: false }).toBe(3);
| ^
3 | });
*/
false
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!', { showStack: false }).toBe(3);
});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
● returns 2 when adding 1 and 1
Custom message:
Woah this should be 2!
expect(received).toBe(expected) // Object.is equality
Expected: 3
Received: 2
*/
FAQs
Add custom message to Jest expects
The npm package jest-expect-message receives a total of 310,233 weekly downloads. As such, jest-expect-message popularity was classified as popular.
We found that jest-expect-message demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.