deep-assert
Advanced tools
| export declare const $any: unique symbol; | ||
| export declare const $compare: unique symbol; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.$any = Symbol("assert.any"); | ||
| exports.$compare = Symbol("assert.compare"); |
@@ -1,5 +0,5 @@ | ||
| declare const $any: unique symbol; | ||
| declare const $compare: unique symbol; | ||
| import { $any, $compare } from "./symbols"; | ||
| export interface Options { | ||
| strict?: boolean; | ||
| allowAdditionalProps?: boolean; | ||
| } | ||
@@ -6,0 +6,0 @@ declare type InternalComparisonFunction = (actual: any) => true | Error; |
@@ -5,4 +5,3 @@ "use strict"; | ||
| var formatter_1 = require("./formatter"); | ||
| var $any = Symbol("assert.any"); | ||
| var $compare = Symbol("assert.compare"); | ||
| var symbols_1 = require("./symbols"); | ||
| var supportsArgumentsClass = (function () { | ||
@@ -50,3 +49,3 @@ return Object.prototype.toString.call(arguments); | ||
| return _a = {}, | ||
| _a[$any] = true, | ||
| _a[symbols_1.$any] = true, | ||
| _a; | ||
@@ -58,3 +57,3 @@ } | ||
| return _a = {}, | ||
| _a[$compare] = function (actual) { | ||
| _a[symbols_1.$compare] = function (actual) { | ||
| try { | ||
@@ -76,4 +75,4 @@ var result = compare(actual); | ||
| var result; | ||
| var allowAdditionalProps = false; | ||
| if (expected && expected[$any]) { | ||
| var allowAdditionalProps = opts.allowAdditionalProps === true; | ||
| if (expected && expected[symbols_1.$any]) { | ||
| if (typeof expected === "object" && Object.keys(expected).length > 0) { | ||
@@ -87,4 +86,4 @@ // $any got in the object because of an object spread | ||
| } | ||
| if (expected && expected[$compare]) { | ||
| return expected[$compare](actual); | ||
| if (expected && expected[symbols_1.$compare]) { | ||
| return expected[symbols_1.$compare](actual); | ||
| } | ||
@@ -91,0 +90,0 @@ if (actual === expected) { |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var symbols_1 = require("./symbols"); | ||
| function createObjectDiff(actual, expected, propMatches) { | ||
@@ -78,2 +79,8 @@ var maxPropsToPrint = 6; | ||
| } | ||
| else if (typeof value === "object" && value[symbols_1.$any]) { | ||
| return "any()"; | ||
| } | ||
| else if (typeof value === "object" && value[symbols_1.$compare]) { | ||
| return "satisfies(…)"; | ||
| } | ||
| else if (Array.isArray(value)) { | ||
@@ -80,0 +87,0 @@ return value.length === 0 ? "[]" : "[ … ]"; |
+8
-4
| { | ||
| "name": "deep-assert", | ||
| "version": "0.2.0", | ||
| "version": "0.3.0", | ||
| "description": "Better deep-equals object expectations, supporting dynamic bottom-up assertions using any() and satisfies().", | ||
@@ -11,2 +11,3 @@ "license": "MIT", | ||
| "scripts": { | ||
| "prebuild": "rimraf dist/", | ||
| "build": "tsc", | ||
@@ -29,4 +30,5 @@ "test": "ava test/**/*.test.ts", | ||
| "@types/node": "^11.13.8", | ||
| "ava": "^1.4.1", | ||
| "ts-node": "^8.1.0", | ||
| "ava": "^4.0.1", | ||
| "rimraf": "^2.6.3", | ||
| "ts-node": "^10.4.0", | ||
| "tslint": "^5.16.0", | ||
@@ -37,6 +39,8 @@ "tslint-config-prettier": "^1.18.0", | ||
| "ava": { | ||
| "compileEnhancements": false, | ||
| "extensions": [ | ||
| "ts" | ||
| ], | ||
| "files": [ | ||
| "test/**/*.test.ts" | ||
| ], | ||
| "require": [ | ||
@@ -43,0 +47,0 @@ "ts-node/register" |
+55
-26
@@ -5,8 +5,8 @@ # deep-assert | ||
| Providing a better deep-equals assertion experience. | ||
| The most developer-friendly way to write assertions for large or complicated objects and arrays. | ||
| * Easily write object and array expectations, with `any()` and `satisfies()` | ||
| * Create your own custom assertions | ||
| * Use `any()` and `satisfies()` property matchers | ||
| * Short, but precise diffs, even for large nested objects | ||
| * Works with objects, arrays, dates, buffers, etc | ||
| * Works with objects, arrays, dates, buffers, and more | ||
| * Write custom property assertions | ||
| * Zero dependencies | ||
@@ -35,12 +35,20 @@ | ||
| assert.deepEquals(getUsers(), [ | ||
| assert.deepEquals( | ||
| // Actual value: | ||
| { | ||
| id: assert.any(), | ||
| id: Math.random(), | ||
| name: "John Smith", | ||
| active: true | ||
| meta: { | ||
| isActive: true, | ||
| lastLogin: new Date("2019-04-29T12:31:00") | ||
| } | ||
| }, | ||
| // Expectation: | ||
| { | ||
| id: assert.any(), | ||
| name: "Jane Smith", | ||
| active: false | ||
| name: "John Smith", | ||
| meta: { | ||
| isActive: true, | ||
| lastLogin: new Date("2019-04-29T12:31:00") | ||
| } | ||
| } | ||
@@ -57,15 +65,22 @@ ]) | ||
| const uuidRegex = /^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/ | ||
| const assertUUID = assert.satisfies(value => typeof value === "string" && value.match(uuidRegex)) | ||
| const assertPositiveNumber = () => assert.satisfies(value => typeof value === "number" && value > 0) | ||
| assert.deepEquals(getUsers(), [ | ||
| assert.deepEquals( | ||
| // Actual value: | ||
| { | ||
| id: assertUUID(), | ||
| id: Math.random(), | ||
| name: "John Smith", | ||
| active: true | ||
| meta: { | ||
| isActive: true, | ||
| lastLogin: new Date("2019-04-29T12:31:00") | ||
| } | ||
| }, | ||
| // Expectation: | ||
| { | ||
| id: assertUUID(), | ||
| name: "Jane Smith", | ||
| active: false | ||
| id: assertPositiveNumber(), | ||
| name: "John Smith", | ||
| meta: { | ||
| isActive: true, | ||
| lastLogin: new Date("2019-04-29T12:31:00") | ||
| } | ||
| } | ||
@@ -77,3 +92,3 @@ ]) | ||
| Normally `deepEquals()` will fail if there are unexpected properties on the tested object. We can use `any()` with the object spread operator to allow additional properties to be present. | ||
| Normally `deepEquals()` will fail if there are properties on the tested object that don't exist on the expectation. We can use `any()` with the object spread operator to allow additional properties to be present. | ||
@@ -85,7 +100,19 @@ `deepEquals()` will then only check the expected properties and ignore all other ones. | ||
| assert.deepEquals(getUsers()[0], { | ||
| name: "John Smith", | ||
| active: true, | ||
| ...assert.any() | ||
| }) | ||
| assert.deepEquals( | ||
| // Actual value: | ||
| { | ||
| id: Math.random(), | ||
| name: "John Smith", | ||
| meta: { | ||
| isActive: true, | ||
| lastLogin: new Date("2019-04-29T12:31:00") | ||
| } | ||
| }, | ||
| // Expectation: | ||
| { | ||
| id: assert.any(), | ||
| name: "John Smith", | ||
| ...assert.any() | ||
| } | ||
| ]) | ||
| ``` | ||
@@ -95,9 +122,11 @@ | ||
| You can call `deepEquals()` in a custom `satisfies()` as well. This way you can easily test recursive data structures, for instance. | ||
| ```js | ||
| import * as assert from "assert-deep" | ||
| const a = { foo: {} } | ||
| a.foo.parent = a.foo | ||
| const actual = { foo: {} } | ||
| actual.foo.parent = actual.foo | ||
| assert.deepEquals(a, { | ||
| assert.deepEquals(actual, { | ||
| foo: assert.satisfies(foo => assert.deepEquals(foo, { parent: foo })) | ||
@@ -104,0 +133,0 @@ }) |
20334
6.51%11
22.22%361
3.44%133
27.88%7
16.67%