{"id":16473200,"url":"https://github.com/saintedlama/betray","last_synced_at":"2025-10-06T17:53:54.881Z","repository":{"id":36447533,"uuid":"40752585","full_name":"saintedlama/betray","owner":"saintedlama","description":"Minimal test spies, stubs and mocks module for node.js","archived":false,"fork":false,"pushed_at":"2017-11-09T11:16:14.000Z","size":25,"stargazers_count":3,"open_issues_count":25,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-01T00:58:13.021Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/saintedlama.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-15T08:51:18.000Z","updated_at":"2017-11-09T08:07:28.000Z","dependencies_parsed_at":"2022-08-17T23:30:11.579Z","dependency_job_id":null,"html_url":"https://github.com/saintedlama/betray","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/saintedlama/betray","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saintedlama%2Fbetray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saintedlama%2Fbetray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saintedlama%2Fbetray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saintedlama%2Fbetray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/saintedlama","download_url":"https://codeload.github.com/saintedlama/betray/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saintedlama%2Fbetray/sbom","scorecard":{"id":795739,"data":{"date":"2025-08-11","repo":{"name":"github.com/saintedlama/betray","commit":"e2edd49bbaa627b471a49d208d1692699a49f906"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.6,"checks":[{"name":"Code-Review","score":0,"reason":"Found 2/24 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":0,"reason":"license file not detected","details":["Warn: project does not have a license file"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 3 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-23T08:54:12.702Z","repository_id":36447533,"created_at":"2025-08-23T08:54:12.702Z","updated_at":"2025-08-23T08:54:12.702Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278655006,"owners_count":26022966,"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-10-06T02:00:05.630Z","response_time":65,"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":[],"created_at":"2024-10-11T12:25:45.524Z","updated_at":"2025-10-06T17:53:54.842Z","avatar_url":"https://github.com/saintedlama.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Betray\n[![Build Status](https://travis-ci.org/saintedlama/betray.svg?branch=master)](https://travis-ci.org/saintedlama/betray)\n[![Coverage Status](https://coveralls.io/repos/github/saintedlama/betray/badge.svg?branch=master)](https://coveralls.io/github/saintedlama/betray?branch=master)\n[![betray analyzed by Codellama.io](https://app.codellama.io/api/badges/5a0438e31b4c363a0f9427c4/bb531760d98c287942b441dc779e2988)](https://app.codellama.io/repositories/5a0438e31b4c363a0f9427c4)\n\nMinimal test spies, stubs and mocks module.\n\n## Installation\n\n    npm install betray\n\n# Spies, Mocks, Stubs, ZOMBIES!\n\n## Creating a spy\n\n```javascript\nconst betray = require('betray');\n\nconst math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\nvar betrayedAdd = betray(math, 'add');\n\nvar sum = math.add(1, 2);\n\n// Original function was invoked and spies invocation counter incremented\nexpect(sum).to.equal(3);\nexpect(betrayedAdd.invoked).to.equal(1);\n```\n\n## Stubs\n\nBetray does not offer a stub API or the like :disappointed:. Minimal! Better! Extensible! :dancers:\n\n```javascript\nvar math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\nvar betrayedAdd = betray(math, 'add', [{ match  : function() { return true; }, handle : function() { return 1; }}]);\n\n// Will not invoke the original add function but will return 1 for every call. \nvar betrayedResult = math.add();\nexpect(betrayedResult).to.equal(1);\n\n// Invocation counter is incremented as well\nexpect(betrayedThrowError.invoked).to.equal(1);\n```\n\n**simpler**\n\n```javascript\nvar math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\nvar betrayedAdd = betray(math, 'add', function() { return 1; }); // Returns 1 for all calls\n\n// Will not invoke the original add function but will return 1 for every call. \nvar betrayedResult = math.add();\nexpect(betrayedResult).to.equal(1);\n\n// Invocation counter is incremented as well\nexpect(betrayedThrowError.invoked).to.equal(1);\n```\n\n**even simpler**\n\n```javascript\nvar math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\nvar betrayedAdd = betray(math, 'add', 1); // Returns 1\n\n// Will not invoke the original add function but will return 1 for every call. \nvar betrayedResult = math.add();\nexpect(betrayedResult).to.equal(1);\n\n// Invocation counter is incremented as well\nexpect(betrayedThrowError.invoked).to.equal(1);\n```\n\n## Mocks\n\nAs mocks are \"pre-programmed with expectations which form a specification of the calls they are expected to receive\" and\nwe betray in a minimal way we'll use chai expect syntax to create our own mock!\n\n```javascript\nvar math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\nbetray(math, 'add', [{\n  match  : function() { return this.add.invoked == 1 },\n  handle : function(y) {\n    expect(y).to.equal(2);\n    return 1;\n  }\n},{\n  match  : function() { return this.add.invoked == 2 },\n  handle : function(y) {\n    expect(y).to.equal(3);\n    return 2;\n  }\n}]);\n\n// Will not invoke the original add function but will return 1 for every call.\nmath.add(2);\nmath.add(3);\n```\n\n**simpler**\n\nBetray implements some convenience functions to match specific calls\n \n* onFirstCall(fn)\n* onSecondCall(fn)\n* onThirdCall(fn)\n* onCall(num, fn)\n\n```javascript\nvar math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\nbetray(math, 'add')\n  .onFirstCall(function(y) {\n    expect(y).to.equal(2);\n    return 1;\n  })\n  .onSecondCall(function(y) {\n    expect(y).to.equal(3);\n    return 2;\n  });\n\n// Will not invoke the original add function but will return 1 for every call.\nmath.add(2);\nmath.add(3);\n```\n\n## Tests and restoring\n\nWhen using betray in for example mocha you might want to betray some functionality **before** running tests and to \nrestore all betrayed functionality **after** running tests. Betray is here to help:\n\n```js\nconst betray = require('betray');\n\ndescribe('something', () =\u003e {\n    let betrayed;\n\n    beforeEach(() =\u003e { betrayed = betray.record(); });\n    afterEach(() =\u003e betrayed.restoreAll());\n\n    it('should do something', () =\u003e {\n        // Use betrayed instead of betray\n    });\n});\n```\n\nNever forget restoring betrayed functions again :clap:\n\n# API documentation\n\nCode examples are runnable mocha tests in tests/betray.js\n\n### betray(object, functionName, strategies)\nBetray a function named `functionName` of object `object` and return the betrayed function.\n\nStrategies accepts a `function`, `object` or an array. If a function is specified this function is invoked\nwhenever the original function (object and functionName) are invoked with all parameters of the original function call. If \nan object is passed this object is returned for every function call of the original function instead invoking the original \nfunction.\nIf an array is passed betray expects each item to expose a `match` and `handle` function. `match` is called with the arguments\nof the original function and only if `match` returns a truthy value `handle` is called. In case match matches a function\ncall the strategy evaluation breaks. The original function is not called.\n\n**Strategy Examples**\n\n**Passing a single function**\n\n```\nvar betray = require('betray');\nvar math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\nbetray(math, 'add', function(x) { return x; });\n// added has a value of 4, since the first argument is returned\n```\n\n**Passing an object**\n\n```\nvar betray = require('betray');\nvar math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\nbetray(math, 'add', 4);\n\nvar added = math.add(4, 10);\n// added has a value of 4, since 4 is returned for every call.\n```\n\n**Passing a function array**\n\n```\nvar betray = require('betray');\nvar math = {\n  add: function(x, y) {\n    return x + y;\n  }\n};\n\n// For all x values lower than 4 return x\nvar matchLowerThen = {\n  match: function(x, y) {\n    return x \u003c 4;\n  },\n\n  handle: function(x, y) {\n    return x;\n  }\n};\n\n// For all x values greater than 4 return y\nvar matchGreaterThen = {\n  match: function(x, y) {\n    return x \u003e 4;\n  },\n\n  handle: function(x, y) {\n    return y;\n  }\n};\n\nbetray(math, 'add', [matchLowerThen, matchGreaterThen]);\n\nvar addedLowerThen = math.add(3, 10);\n// addedLowerThen has a value of 3, since matchLowerThen strategy matched which returned x\n\nvar addedGreaterThen = math.add(5, 10);\n// addedGreaterThen has a value of 10, since matchGreaterThen strategy matched which returned x\n\nvar addedOriginal = math.add(4, 10);\n// addedOriginal has a value of 14, since no strategy matched and the original function is invoked\n```\n\n\n### betray.record()\nReturns an instance of betray that records betrayed functions and exposes a `restoreAll` function to restore all betrayed functions.\n\n### betrayed\n\n#### betrayed.invoked\nTBD\n\n#### betrayed.invocations\nTBD\n\n#### betrayed.restore\nTBD\n\n#### betrayed.when\nTBD\n\n#### betrayed.onCall\nTBD\n\n#### betrayed.onFirstCall\nTBD\n\n#### betrayed.onSecondCall\nTBD\n\n#### betrayed.onThirdCall\nTBD\n\n#### betrayed.forAll\nTBD\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaintedlama%2Fbetray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaintedlama%2Fbetray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaintedlama%2Fbetray/lists"}