Installations
npm install jest-expect-message
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
16.17.0
NPM Version
8.15.0
Statistics
362 Stars
45 Commits
42 Forks
8 Watching
3 Branches
7 Contributors
Updated on 07 Nov 2024
Bundle Size
1.65 kB
Minified
769.00 B
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
33,931,021
Last day
-20.8%
57,205
Compared to previous day
Last week
-1.3%
350,597
Compared to previous week
Last month
10.8%
1,428,951
Compared to previous month
Last year
38.8%
12,318,756
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
jest-expect-message
🃏🗯
Add custom message to Jest expects
Problem
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:
1test('returns 2 when adding 1 and 1', () => { 2 expect(1 + 1, 'Woah this should be 2!').toBe(3); 3});
This will throw the following error in Jest:
1Expect takes at most one argument.
Solution
jest-expect-message
allows you to call expect
with a second argument of a String
message.
For example the same test as above:
1test('returns 2 when adding 1 and 1', () => { 2 expect(1 + 1, 'Woah this should be 2!').toBe(3); 3});
With jest-expect-message
this will fail with your custom error message:
1 ● returns 2 when adding 1 and 1 2 3 Custom message: 4 Woah this should be 2! 5 6 expect(received).toBe(expected) // Object.is equality 7 8 Expected: 3 9 Received: 2
Installation
With npm:
1npm install --save-dev jest-expect-message
With yarn:
1yarn add -D jest-expect-message
Setup
Add jest-expect-message
to your Jest setupFilesAfterEnv
configuration.
See for help
Jest v24+
1"jest": { 2 "setupFilesAfterEnv": ["jest-expect-message"] 3}
Jest v23-
1"jest": { 2 "setupTestFrameworkScriptFile": "jest-expect-message" 3}
If you have a custom setup file and want to use this library then add the following to your setup file.
1import 'jest-expect-message';
Configure Typescript
Add the following entry to your tsconfig to enable Typescript support.
1 "files": ["node_modules/jest-expect-message/types/index.d.ts"],
Example
Custom message example with typescript
Configure ESlint
1"rules": { 2 "jest/valid-expect": [ 3 "error", 4 { 5 "maxArgs": 2 6 } 7 ] 8}
Usage
expect(actual, message, options?)
actual
: The value you would normally pass into anexpect
to assert against with a given matcher.message
: String, the custom message you want to be printed should theexpect
fail.options
: An optional object that controls what is shown as part of the custom message.showPrefix: boolean
: Iffalse
will not show theCustom message:
prefix. Default:true
showMatcherMessage: boolean
: Iffalse
will not show the matchers original error message. Default:true
showStack: boolean
: Iffalse
will not show the matchers stack trace. Default:true
1test('returns 2 when adding 1 and 1', () => { 2 expect(1 + 1, 'Woah this should be 2!').toBe(3); 3}); 4// ↓ ↓ ↓ ↓ ↓ ↓ 5/* 6 ● returns 2 when adding 1 and 1 7 8 Custom message: 9 Woah this should be 2! 10 11 expect(received).toBe(expected) // Object.is equality 12 13 Expected: 3 14 Received: 2 15 16 1 | test('returns 2 when adding 1 and 1', () => { 17> 2 | expect(1 + 1, 'Woah this should be 2!').toBe(3); 18 | ^ 19 3 | }); 20*/
showPrefix: false
1test('returns 2 when adding 1 and 1', () => { 2 expect(1 + 1, 'Woah this should be 2!', { showPrefix: false }).toBe(3); 3}); 4// ↓ ↓ ↓ ↓ ↓ ↓ 5/* 6 ● returns 2 when adding 1 and 1 7 8 Woah this should be 2! 9 10 expect(received).toBe(expected) // Object.is equality 11 12 Expected: 3 13 Received: 2 14 15 1 | test('returns 2 when adding 1 and 1', () => { 16> 2 | expect(1 + 1, 'Woah this should be 2!', { showPrefix: false }).toBe(3); 17 | ^ 18 3 | }); 19*/
showMatcherMessage: false
1test('returns 2 when adding 1 and 1', () => { 2 expect(1 + 1, 'Woah this should be 2!', { showMatcherMessage: false }).toBe(3); 3}); 4// ↓ ↓ ↓ ↓ ↓ ↓ 5/* 6 ● returns 2 when adding 1 and 1 7 8 Custom message: 9 Woah this should be 2! 10 11 1 | test('returns 2 when adding 1 and 1', () => { 12> 2 | expect(1 + 1, 'Woah this should be 2!', { showMatcherMessage: false }).toBe(3); 13 | ^ 14 3 | }); 15*/
showStack: false
1test('returns 2 when adding 1 and 1', () => { 2 expect(1 + 1, 'Woah this should be 2!', { showStack: false }).toBe(3); 3}); 4// ↓ ↓ ↓ ↓ ↓ ↓ 5/* 6 ● returns 2 when adding 1 and 1 7 8 Custom message: 9 Woah this should be 2! 10 11 expect(received).toBe(expected) // Object.is equality 12 13 Expected: 3 14 Received: 2 15*/
LICENSE
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
6 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
Reason
Found 6/29 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yaml:1
- Info: no jobLevel write permissions found
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:46: update your workflow using https://app.stepsecurity.io/secureworkflow/mattphillips/jest-expect-message/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:47: update your workflow using https://app.stepsecurity.io/secureworkflow/mattphillips/jest-expect-message/ci.yaml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yaml:55: update your workflow using https://app.stepsecurity.io/secureworkflow/mattphillips/jest-expect-message/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:61: update your workflow using https://app.stepsecurity.io/secureworkflow/mattphillips/jest-expect-message/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:62: update your workflow using https://app.stepsecurity.io/secureworkflow/mattphillips/jest-expect-message/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/mattphillips/jest-expect-message/ci.yaml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/mattphillips/jest-expect-message/ci.yaml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yaml:35: update your workflow using https://app.stepsecurity.io/secureworkflow/mattphillips/jest-expect-message/ci.yaml/main?enable=pin
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 7 are checked with a SAST tool
Score
3.1
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More