{"id":16927398,"url":"https://github.com/dubzzz/fast-check-examples","last_synced_at":"2025-09-25T11:57:17.008Z","repository":{"id":40453340,"uuid":"127954232","full_name":"dubzzz/fast-check-examples","owner":"dubzzz","description":"Property based testing (QuickCheck) examples based on fast-check","archived":false,"fork":false,"pushed_at":"2024-11-13T22:24:29.000Z","size":151,"stargazers_count":26,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-04T17:11:51.303Z","etag":null,"topics":["fast-check","property-based-testing","quickcheck","tdd"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dubzzz.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":"2018-04-03T18:51:27.000Z","updated_at":"2025-02-11T21:57:49.000Z","dependencies_parsed_at":"2022-08-09T20:51:37.739Z","dependency_job_id":null,"html_url":"https://github.com/dubzzz/fast-check-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dubzzz/fast-check-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubzzz%2Ffast-check-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubzzz%2Ffast-check-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubzzz%2Ffast-check-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubzzz%2Ffast-check-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dubzzz","download_url":"https://codeload.github.com/dubzzz/fast-check-examples/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubzzz%2Ffast-check-examples/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276913452,"owners_count":25727127,"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","status":"online","status_checked_at":"2025-09-25T02:00:09.612Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["fast-check","property-based-testing","quickcheck","tdd"],"created_at":"2024-10-13T20:34:07.567Z","updated_at":"2025-09-25T11:57:16.959Z","avatar_url":"https://github.com/dubzzz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Property based testing with [fast-check](https://fast-check.dev/)\n\nThis repository provides examples of property based tests you might write. It makes use of **fast-check** - framework written in TypeScript - but can easily be transposed to other frameworks or languages.\n\nIn order to add **fast-check** to you project, you have to run:\n\n```bash\nnpm install fast-check --save-dev\n```\n\nIf you want to run the properties of this repository locally:\n\n```bash\ngit clone https://github.com/dubzzz/fast-check-examples.git\ncd fast-check-examples\nnpm install\nnpm run test\n```\n\nPlease note that tests of failing implementations have been disabled.\n\n## Property based testing in a nutshell\n\nThe motto of property based testing: properties instead of isolated examples - *or in addition to*.\n\nIt makes it possible to cover a wider range of inputs and hence find yet undiscovered bugs (see bugs discovered by fast-check: [js-yaml](https://github.com/nodeca/js-yaml/pull/398), [query-string](https://github.com/sindresorhus/query-string/pull/138), [left-pad](https://github.com/stevemao/left-pad/issues/58)).\n\nA property can be summurized by: *for any (a, b, ...) such as precondition(a, b, ...) holds, property(a, b, ...) is true*\n\nProperty based testing frameworks try to discover inputs `(a, b, ...)` causing `property(a, b, ...)` to be false. If they find such, they have to reduce the counterexample to a minimal counterexample.\n\n## Tips to find useful properties\n\nTraps to avoid: *testing your code against itself*.\n\nKeep in mind that *properties might not be able to assess the exact value of the output*. But they should be able to test some traits of the output.\n\nHere are some tips that can help you to find your properties.\n\n### Independant of your inputs\n\n**When?** Some characteristics of your output are independant from your input\n\nFor instance:\n- for any floating point number `d`, `Math.floor(d)` is an integer\n- for any integer `n`, `Math.abs(n)` is superior to `0`\n\n### Characteristics derived from the input\n\n**When?** Output has an easy relationship with the input\n\nFor instance: if the algorithm computes the average of two numbers\n```js\nfc.assert(\n  fc.property(\n    fc.integer(), fc.integer(),\n    (a, b) =\u003e a \u003c= b\n      ? a \u003c= average(a, b) \u0026\u0026 average(a, b) \u003c= b\n      : b \u003c= average(a, b) \u0026\u0026 average(a, b) \u003c= a));\n```\n\n*In other words: the average of a and b must be between a and b*\n\nFor instance: if the algorithm factorizes a number\n```js\nfc.assert(\n  fc.property(\n    fc.nat(),\n    n =\u003e factorize(n).reduce((acc, cur) =\u003e acc*cur, 1) === n));\n```\n\n*In other words: the product of all numbers in the output must be equal to the input*\n\n### Subset of inputs\n\n**When?** Some inputs might have easy outputs\n\nFor instance: if the algorithm removes duplicates from an array\n```js\nfc.assert(\n  fc.property(\n    fc.set(fc.char()),\n    data =\u003e assert.deepEqual(removeDuplicates(data), data)));\n```\n\n*In other words: removing duplicates of an array of unique values returns the array itself*\n\nFor instance: if the algorithm checks if a string contains another one\n```js\nfc.assert(\n  fc.property(\n    fc.string(), fc.string(), fc.string(),\n    (a, b, c) =\u003e contains(b, a + b + c)));\n```\n\n*In other words: the concatenation of a, b and c always contains string b*\n\n### Combination of functions\n\n**When?** Two or more functions in your API can be combined to have something simple to assess\n\n#### Round trip\n\nThe API provides some kind of encode/decode functions. In this case the round trip is: `decode(encode(value)) === value`.\n\nFor instance: if you have two methods zip/unzip\n```js\nfc.assert(\n  fc.property(\n    fc.string(),\n    v =\u003e unzip(zip(v)) === v));\n```\n\n*In other words: unzip is the reverse of zip*\n\n#### More general combination\n\nFor instance: if you provide lcm and gcd\n```js\nfc.assert(\n  fc.property(\n    fc.nat(), fc.nat(),\n    (a, b) =\u003e lcm(a, b) * gcd(a, b) === a * b));\n```\n\n### Compare to a simpler version\n\n**When?** There is a slower but simpler implementation or you are rewriting the code\n\nFor instance: if the algorithm checks that a sorted array contains a given value in a binary way\n```js\nfc.assert(\n  fc.property(\n    fc.char(), fc.array(fc.char()).map(d =\u003e d.sort()),\n    (c, data) =\u003e binaryContains(c, data) === linearContains(c, data)));\n```\n\n*In other words: binaryContains and linearContains must always agree, only the complexity differs*\n\n## More on Property Based Testing\n\nMore details on Property based testing at:\n- [John Hughes — Don’t Write Tests](https://www.youtube.com/watch?v=hXnS_Xjwk2Y)\n- [Generating test cases so you don’t have to (Spotify)](https://labs.spotify.com/2015/06/25/rapid-check/)\n\nRemember that property based does not replace example based testing.\nNonetheless it can cover a larger range of inputs and potentially catch problems not seen with examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdubzzz%2Ffast-check-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdubzzz%2Ffast-check-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdubzzz%2Ffast-check-examples/lists"}