πŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more β†’
Socket
Book a DemoInstallSign in
Socket

jest-expect-message

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-expect-message - npm Package Compare versions

Comparing version

to
1.1.3

types/index.d.ts

8

dist/index.js

@@ -1,9 +0,7 @@

'use strict';
"use strict";
var _withMessage = require('./withMessage');
var _withMessage = _interopRequireDefault(require("./withMessage"));
var _withMessage2 = _interopRequireDefault(_withMessage);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
global.expect = (0, _withMessage2.default)(global.expect);
global.expect = (0, _withMessage.default)(global.expect);

@@ -1,2 +0,2 @@

'use strict';
"use strict";

@@ -6,8 +6,7 @@ Object.defineProperty(exports, "__esModule", {

});
exports.default = void 0;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
class JestAssertionError extends Error {
constructor(result, callsite) {
super(result.message());
super(typeof result.message === 'function' ? result.message() : result.message);
this.matcherResult = result;

@@ -19,14 +18,30 @@

}
}
const wrapMatcher = (matcher, customMessage) => {
const wrapMatcher = (matcher, customMessage, config) => {
const newMatcher = (...args) => {
try {
return matcher(...args);
} catch (error) {
const result = matcher(...args);
if (result && typeof result.then === 'function') {
return result.catch(rethrowWithMessage).catch(function handleError(error) {
throw new JestAssertionError(error.matcherResult, handleError);
});
} else {
return result;
}
} catch (e) {
rethrowWithMessage(e);
}
function rethrowWithMessage(error) {
if (!error.matcherResult) {
throw error;
}
const { matcherResult } = error;
const {
matcherResult
} = error;
if (typeof customMessage !== 'string' || customMessage.length < 1) {

@@ -36,11 +51,23 @@ throw new JestAssertionError(matcherResult, newMatcher);

const message = () => 'Custom message:\n ' + customMessage + '\n\n' + matcherResult.message();
const matcherMessage = typeof error.matcherResult.message === 'function' ? error.matcherResult.message() : error.matcherResult.message;
const messagePrefix = config.showPrefix ? 'Custom message:\n ' : '';
throw new JestAssertionError(_extends({}, matcherResult, { message }), newMatcher);
const message = () => messagePrefix + customMessage + (config.showMatcherMessage ? '\n\n' + matcherMessage : '');
const e = new JestAssertionError({ ...matcherResult,
message
}, newMatcher);
if (!config.showStack) {
e.stack = null;
}
throw e;
}
};
return newMatcher;
};
const wrapMatchers = (matchers, customMessage) => {
const wrapMatchers = (matchers, customMessage, config) => {
return Object.keys(matchers).reduce((acc, name) => {

@@ -50,17 +77,28 @@ const matcher = matchers[name];

if (typeof matcher === 'function') {
return _extends({}, acc, {
[name]: wrapMatcher(matcher, customMessage)
});
acc[name] = wrapMatcher(matcher, customMessage, config);
} else {
acc[name] = wrapMatchers(matcher, customMessage, config); // recurse on .not/.resolves/.rejects
}
return _extends({}, acc, {
[name]: wrapMatchers(matcher, customMessage) // recurse on .not/.resolves/.rejects
});
return acc;
}, {});
};
exports.default = expect => {
var _default = expect => {
// proxy the expect function
let expectProxy = Object.assign((actual, customMessage) => wrapMatchers(expect(actual), customMessage), // partially apply expect to get all matchers and chain them
expect // clone additional properties on expect
let expectProxy = Object.assign((actual, customMessage, options = {}) => {
const config = {
showMatcherMessage: typeof options.showMatcherMessage === 'boolean' ? options.showMatcherMessage : true,
showPrefix: typeof options.showPrefix === 'boolean' ? options.showPrefix : true,
showStack: typeof options.showStack === 'boolean' ? options.showStack : true
};
let matchers = expect(actual); // partially apply expect to get all matchers and chain them
if (customMessage) {
// only pay the cost of proxying matchers if we received a customMessage
matchers = wrapMatchers(matchers, customMessage, config);
}
return matchers;
}, expect // clone additional properties on expect
);

@@ -70,2 +108,3 @@

expect.extend(o); // add new matchers to expect
expectProxy = Object.assign(expectProxy, expect); // clone new asymmetric matchers

@@ -75,2 +114,4 @@ };

return expectProxy;
};
};
exports.default = _default;
{
"name": "jest-expect-message",
"version": "1.0.2",
"version": "1.1.3",
"description": "Add custom message to Jest expects",
"main": "dist/index.js",
"types": "types/index.d.ts",
"files": [
"dist",
"types"
],
"scripts": {
"build": "babel src -d dist --ignore *.test.js",
"contributor": "all-contributors add",
"contributor:gen": "all-contributors generate",
"lint": "eslint src",
"lint:fix": "yarn lint --fix",
"precommit": "lint-staged",
"build": "babel src --out-dir dist --ignore 'src/**/*.test.js'",
"prepublishOnly": "yarn build",
"prettier": "prettier 'src/**/*.js' --write --single-quote=true --print-width=120",
"test": "jest",
"test:coverage": "yarn test --coverage",
"test:report": "codecov"
"typecheck": "tsc --noEmit types/index.d.ts"
},

@@ -38,28 +37,24 @@ "keywords": [

"devDependencies": {
"all-contributors-cli": "^5.4.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.6",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"codecov": "^3.0.4",
"eslint": "^5.3.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-jest": "^21.18.0",
"husky": "^0.14.3",
"jest": "^23.4.2",
"jest-extended": "^0.8.1",
"lint-staged": "^7.2.0",
"prettier": "^1.14.0"
"@babel/cli": "^7.18.10",
"@babel/core": "^7.19.0",
"@babel/preset-env": "^7.19.0",
"@types/jest": "^29.0.0",
"jest": "^29.0.2",
"jest-extended": "^3.1.0",
"jest-serializer-ansi-escapes": "^2.0.1",
"typescript": "^4.8.2"
},
"lint-staged": {
"*.js": [
"yarn prettier",
"git add"
]
},
"babel": {
"plugins": [
"transform-es2015-modules-commonjs",
"transform-object-rest-spread"
"ignore": [
"*.test.js"
],
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "14"
}
}
]
]

@@ -69,4 +64,27 @@ },

"testEnvironment": "node",
"setupTestFrameworkScriptFile": "jest-extended"
}
"testPathIgnorePatterns": [
".idea",
"examples",
"/node_modules/",
"/fixtures/",
"/dist/"
],
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"snapshotFormat": {
"escapeString": false,
"printBasicPrototype": false
},
"snapshotSerializers": [
"jest-serializer-ansi-escapes"
],
"moduleNameMapper": {
"src/(.*)": "<rootDir>/src/$1"
},
"setupFilesAfterEnv": [
"jest-extended/all"
]
},
"dependencies": {}
}

@@ -7,2 +7,3 @@ <div align="center">

Add custom message to Jest expects
</div>

@@ -12,3 +13,3 @@

[![Build Status](https://img.shields.io/travis/mattphillips/jest-expect-message.svg?style=flat-square)](https://travis-ci.org/mattphillips/jest-expect-message)
[![Build Status](https://img.shields.io/github/workflow/status/mattphillips/jest-expect-message/GitHub%20CI/main?style=flat-square)](https://github.com/mattphillips/jest-expect-message/actions/workflows/ci.yaml)
[![Code Coverage](https://img.shields.io/codecov/c/github/mattphillips/jest-expect-message.svg?style=flat-square)](https://codecov.io/github/mattphillips/jest-expect-message)

@@ -70,2 +71,3 @@ [![version](https://img.shields.io/npm/v/jest-expect-message.svg?style=flat-square)](https://www.npmjs.com/package/jest-expect-message)

With npm:
```sh

@@ -76,2 +78,3 @@ npm install --save-dev jest-expect-message

With yarn:
```sh

@@ -83,7 +86,17 @@ yarn add -D jest-expect-message

Add `jest-expect-message` to your Jest `setupTestFrameworkScriptFile` configuration.
[See for help](https://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string)
Add `jest-expect-message` to your Jest `setupFilesAfterEnv` configuration.
[See for help](https://jestjs.io/docs/en/next/configuration#setupfilesafterenv-array)
### Jest v24+
```json
"jest": {
"setupFilesAfterEnv": ["jest-expect-message"]
}
```
### Jest v23-
```json
"jest": {
"setupTestFrameworkScriptFile": "jest-expect-message"

@@ -93,7 +106,42 @@ }

If you have a custom setup file and want to use this library then add the following to your setup file.
```js
import 'jest-expect-message';
```
### Configure Typescript
Add the following entry to your tsconfig to enable Typescript support.
```json
"files": ["node_modules/jest-expect-message/types/index.d.ts"],
```
#### Example
Custom message [example](/example) with typescript
### Configure ESlint
```json
"rules": {
"jest/valid-expect": [
"error",
{
"maxArgs": 2
}
]
}
```
## Usage
- `expect(actual, message)`
- `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.
- `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`

@@ -104,15 +152,87 @@ ```js

});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
● 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 | });
*/
```
## Contributors
### showPrefix: `false`
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
| [<img src="https://avatars0.githubusercontent.com/u/5610087?v=4" width="100px;"/><br /><sub><b>Matt Phillips</b></sub>](http://mattphillips.io)<br />[πŸ’»](https://github.com/mattphillips/jest-expect-message/commits?author=mattphillips "Code") [πŸ“–](https://github.com/mattphillips/jest-expect-message/commits?author=mattphillips "Documentation") [πŸ’‘](#example-mattphillips "Examples") [πŸ€”](#ideas-mattphillips "Ideas, Planning, & Feedback") [πŸš‡](#infra-mattphillips "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/mattphillips/jest-expect-message/commits?author=mattphillips "Tests") [πŸ”§](#tool-mattphillips "Tools") |
| :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->
```js
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 | });
*/
```
### showMatcherMessage: `false`
```js
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 | });
*/
```
### showStack: `false`
```js
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
*/
```
## LICENSE
[MIT](/LICENSE)