{"id":16641241,"url":"https://github.com/cmstead/chai-verify","last_synced_at":"2025-03-12T05:14:02.932Z","repository":{"id":57196614,"uuid":"197855237","full_name":"cmstead/chai-verify","owner":"cmstead","description":"Object verification method for Chai built on the philosophy of Golden Master testing","archived":false,"fork":false,"pushed_at":"2019-07-26T20:52:43.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-19T17:46:54.735Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cmstead.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-19T23:34:58.000Z","updated_at":"2019-07-26T20:52:45.000Z","dependencies_parsed_at":"2022-09-15T15:42:26.135Z","dependency_job_id":null,"html_url":"https://github.com/cmstead/chai-verify","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmstead%2Fchai-verify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmstead%2Fchai-verify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmstead%2Fchai-verify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmstead%2Fchai-verify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cmstead","download_url":"https://codeload.github.com/cmstead/chai-verify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243159172,"owners_count":20245675,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T07:45:57.215Z","updated_at":"2025-03-12T05:14:02.914Z","avatar_url":"https://github.com/cmstead.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chai-Verify #\n\nChai-verify is fast, easy way to perform small to medium object comparisons when you want the readability of Approvals without the overhead.\n\n## Setup ##\n\nInstalling chai-verify:\n\n```javascript\nnpm install chai-verify --save-dev\n```\n\nChai-verify has a peer dependency of the Chai assertion library.  If you don't have chai installed, then install this way instead:\n\n```javascript\nnpm install chai chai-verify --save-dev\n```\n\n## Usage ##\n\n### Setting up for your tests ###\n\nIn node, do the following in your test file:\n\n```javascript\nconst chai = require('chai');\nconst chaiVerify = require('chai-verify');\n\nchai.use(chaiVerify);\n```\n\nFor testing in the browser, do the following:\n\n```html\n\u003cscript src=\"/path/to/chai\"\u003e\u003c/script\u003e\n\u003cscript src=\"/path/to/chai-verify\"\u003e\u003c/script\u003e\n```\n\n### Verifying values in your tests ###\n\nUsing the assert API, you can do the following:\n\n```javascript\nit('verifies with assert', function () {\n    const badValue = { bad: 'value' };\n    const expected = { foo: 'bar' };\n    const message = 'Something went wrong!'\n\n    assert.verify(badValue, expected, message);\n});\n```\n\nIf your data contains random objects, you can also normalize your data before comparing:\n\n```javascript\nit('verifies and normalizes with assert', function () {\n    const objectWithRandomValue = { foo: Math.random() };\n    const normalizationValues = { foo: 0.001 }\n    const expected = { foo: 0.001 };\n\n    assert.normalizeAndVerify(objectWithRandomValue, expected, normalizationValues);\n});\n```\n\nUsing the expect API, you can do the following instead:\n\n```javascript\nit('verifies with expect', function () {\n    const badValue = { bad: 'value' };\n    const expected = { foo: 'bar' };\n    const message = 'Something went wrong here too!'\n\n    expect(badValue).to.be.verifiedAs(expected, message);\n});\n```\n\nYou can also perform normalization on your data with the expect/BDD syntax:\n\n```javascript\nit('verifies and normalizes with assert', function () {\n    const objectWithRandomValue = { foo: Math.random() };\n    const normalizationValues = { foo: 0.001 }\n    const expected = { foo: 0.001 };\n\n    expect(badValue).to.be.normalizedAndVerifiedAs(expected, normalizationValues);\n```\n\nBoth of the failing cases will output the following information:\n\n```\nActual and expected values do not match, received values as follows\n===================================================================\n\nActual\n------\n{\n    \"bad\": \"value\"\n}\n\nExpected\n--------\n{\n    \"foo\": \"bar\"\n}\n```\n\n## Utilities ##\n\nChai-verify comes with a set of utilities which help to simplify the process of testing objects. There are two basic types of utilities: function expectation helpers and value normalization.\n\n### Function Expectation Helpers ###\n\nBefore we get started, let's take a look at an example of using `functionExpectationAsNamed` all of the function expectation helpers will work the same way in the context of tests:\n\n```javascript\nconst objectUnderTest = {\n    numberProp: 53,\n    functionProp: function namedMethd() {}\n};\n\nconst functionExpectation = chaiVerify.utils.functionExpectationAsNamed('namedMethod');\n\nconst expectedResult = {\n    numberProp: 53,\n    functionProp: functionExpectation\n}\n\nexpect(objectUnderTest).to.be.verifiedAs(expectedResult);\n```\n\n#### functionExpectationByName ###\n\nTop-level expectation helper, this generates a string to verify the output of the stringification process.  All functions are stringified to `[Function: \u003cfunction name\u003e]` where `\u003cfunction name\u003e` is either the name of the function or 'anonymous' if the function has no name.\n\nUsage:\n\n```javascript\nconst namedFunctionExpectation = chaiVerify.utils.functionExpectationByName('aNamedFunction');\n\nconsole.log(namedFunctionExpectation); // [Function: aNamedFunction]\n\nconst anonymousFunctionExpectation = chaiVerify.utils.functionExpectationByName();\n\nconsole.log(namedFunctionExpectation); // [Function: anonymous]\n```\n\n#### functionExpectationAsNamed ####\n\nDoes the same as above, but requires a name property.\n\n```javascript\nconst namedFunctionExpectation = chaiVerify.utils.functionExpectationAsNamed('aNamedFunction');\n\nconsole.log(namedFunctionExpectation); // [Function: aNamedFunction]\n```\n\n#### functionExpectationAsAnonymous ####\n\nDoes the same as above, but only returns anonymous strings.\n\n```javascript\nconst anonymousFunctionExpectation = chaiVerify.utils.functionExpectationAsAnonymous();\n\nconsole.log(anonymousFunctionExpectation); // [Function: anonymous]\n```\n\n### Value normalization ###\n\nValue normalization is particularly useful in scenarios where generated data is random, or non-deterministic.  This can be situations like random numbers, random strings, dates, etc.\n\n#### normalizeValue ####\n\nNormalize value will normalize a single value according to the expected value provided. A normalizing function may also be used.  See example below:\n\n```javascript\nconst originalValue = Math.random() * 2;\nconst normalizationValue = 1.10101;\nconst normalizedValue = chaiVerify.utils.normalizeValue(originalValue, normalizationValue);\n\nconsole.log(normalizedValue); // 1.10101\n\nfunction normalizer(value) {\n    return 22.222;\n}\n\nconst secondNormalizedValue = chaiVerify.utils.normalizeValue(originalValue, normalizer);\n\nconsole.log(secondNormalizedValue); //22.222\n```\n\n#### normalizeValues ####\n\nNormalizes an array values with a normalizing value or function:\n\n```javascript\nconst originalValues = [1, 2, 3, 4];\nconst normalizationValue = 1.10101;\nconst normalizedValue = chaiVerify.utils.normalizeValues(originalValue, normalizationValue);\n\nconsole.log(normalizedValues); // [1.10101, 1.10101, 1.10101, 1.10101]\n\nfunction normalizer(value) {\n    return 22.222;\n}\n\nconst secondNormalizedValue = chaiVerify.utils.normalizeValues(originalValues, normalizer);\n\nconsole.log(secondNormalizedValue); // [22.222, 22.222, 22.222, 22.222]\n```\n\n#### normalizeProperties ####\n\nNormalizes specified properties on an object using values or normalizing functions:\n\n```javascript\nconst originalObject = {\n    staticProperty: 'This will not change',\n    changeByValue: 1234,\n    changeByNormalizer: '5678'\n};\n\nconst normalizingObject = {\n    changeByValue: 4321,\n    changeByNormalizer: () =\u003e '9999'\n};\n\nconst normalizedObject = chaiVerify.utils.normalizeProperties(originalObject, normalizingObject);\n\n/* Result:\n{\n    staticProperty: 'This will not change',\n    changeByValue: 4321,\n    changeByNormalizer: '9999'\n}\n*/\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmstead%2Fchai-verify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcmstead%2Fchai-verify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmstead%2Fchai-verify/lists"}