-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
86 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export declare function assertShallowDeepAlmostEqual( | ||
expect?: object | boolean | number | null | string | Date, | ||
actual?: object | boolean | number | null | string | Date, | ||
path?: string, | ||
threshold?: number, | ||
): boolean; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.assertShallowDeepAlmostEqual = void 0; | ||
function assertShallowDeepAlmostEqual(expect, actual, path = "", threshold = 0.01) { | ||
// null value | ||
if (expect === null) { | ||
if (!(actual === null)) { | ||
throw new Error(`Expected to have null but got "${actual}" at path "${path}".`); | ||
} | ||
return true; | ||
} | ||
// undefined expected value | ||
if (typeof expect === "undefined") { | ||
if (typeof actual !== "undefined") { | ||
throw new Error(`Expected to have undefined but got "${actual}" at path "${path}".`); | ||
} | ||
return true; | ||
} | ||
// scalar description | ||
if (typeof expect === "boolean" || typeof expect === "string") { | ||
if (expect !== actual) { | ||
throw new Error(`Expected to have "${expect}" but got "${actual}" at path "${path}".`); | ||
} | ||
return true; | ||
} | ||
// numbers — here is some important 'almost equal' stuff | ||
// TODO: configurable threshold | ||
if (typeof expect === "number") { | ||
if (typeof actual !== "number") { | ||
throw new Error(`Expected to have number but got "${actual}" at path "${path}".`); | ||
} | ||
if (Math.abs(expect - actual) > threshold) { | ||
throw new Error( | ||
`Expected to have "${expect}+-${threshold}" but got "${actual}" at path "${path}".`, | ||
); | ||
} | ||
return true; | ||
} | ||
// dates | ||
if (expect instanceof Date) { | ||
if (actual instanceof Date) { | ||
if (expect.getTime() !== actual.getTime()) { | ||
throw new Error( | ||
`Expected to have date "${expect.toISOString()}" but got "${actual.toISOString()}" at path "${path}".`, | ||
); | ||
} | ||
} else { | ||
throw new Error( | ||
`Expected to have date "${expect.toISOString()}" but got "${actual}" at path "${path}".`, | ||
); | ||
} | ||
return true; | ||
} | ||
if (actual === null) { | ||
throw new Error(`Expected to have an array/object but got null at path "${path}".`); | ||
} | ||
// array/object description | ||
// eslint-disable-next-line no-restricted-syntax, guard-for-in | ||
for (const prop in expect) { | ||
// @ts-ignore | ||
if (typeof actual[prop] === "undefined" && typeof expect[prop] !== "undefined") { | ||
throw new Error(`Expected "${prop}" field to be defined at path "${path}".`); | ||
} | ||
const newPath = path + (path === "/" ? "" : "/") + prop; | ||
// @ts-ignore | ||
shallowDeepAlmostEqual(expect[prop], actual[prop], newPath, threshold); | ||
} | ||
return true; | ||
} | ||
exports.assertShallowDeepAlmostEqual = assertShallowDeepAlmostEqual; |