2.0.0 • Published 1 year ago

assert-json-equal v2.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
1 year ago

assert-json-equal

assert.deepStrictEqual for JSON object.

assert-json-equal provide a assertJsonEqual function.

assert.deepStrictEqual discriminate undefined value and missing property.

const a = {};
const b = {
    undef: undefined
};
assert.throws(() => {
    assert.deepStrictEqual(a, b); // => throw
});

assertJsonEqual does not discriminate undefined value and missing property. It is useful for comparing JSON objects.

import { assertJsonEqual } from "assert-json-equal"
const a = {};
const b = {
    undef: undefined
};
assertJsonEqual(a, b); // => pass

Install

Install with npm:

npm install assert-json-equal

Usage

/**
 * `assert.deepStrictEqual` for JSON object.
 * Same API with `assert.deepStrictEqual(a, b, [message])`
 */
export declare const assertJsonEqual: (actual: any, expected: any, message?: string | Error | undefined) => void;

Mechanism

Serialize and Deserialize object with JSON before compare objects with assert.deepStrictEqual.

import assert from "assert";

const normalizeJSON = <T extends any>(json: T) => {
    return JSON.parse(JSON.stringify(json));
};
/**
 * `assert.deepStrictEqual` for JSON object.
 * Same API with `assert.deepStrictEqual(a, b, [message])`
 * @param actual
 * @param expected
 * @param message
 */
export const assertJsonEqual = (actual: any, expected: any, message?: string | Error) => {
    assert.deepStrictEqual(normalizeJSON(actual), normalizeJSON(expected), message);
}

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu