{"id":15552322,"url":"https://github.com/bahmutov/not-allowed","last_synced_at":"2025-09-26T12:30:55.180Z","repository":{"id":31539219,"uuid":"128220041","full_name":"bahmutov/not-allowed","owner":"bahmutov","description":"Throws a good user friendly error if a function is called, useful in stubs during testing","archived":false,"fork":false,"pushed_at":"2024-10-18T21:54:03.000Z","size":327,"stargazers_count":3,"open_issues_count":12,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-19T19:58:27.257Z","etag":null,"topics":["mock","sinon","stub","test","testing","utility"],"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/bahmutov.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-05T14:43:47.000Z","updated_at":"2021-12-12T08:15:22.000Z","dependencies_parsed_at":"2023-09-24T21:00:14.526Z","dependency_job_id":"782bc4a3-e5af-4295-9df0-deb89e62fbc7","html_url":"https://github.com/bahmutov/not-allowed","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fnot-allowed","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fnot-allowed/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fnot-allowed/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fnot-allowed/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bahmutov","download_url":"https://codeload.github.com/bahmutov/not-allowed/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234309714,"owners_count":18811946,"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":["mock","sinon","stub","test","testing","utility"],"created_at":"2024-10-02T14:15:56.626Z","updated_at":"2025-09-26T12:30:54.841Z","avatar_url":"https://github.com/bahmutov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# not-allowed\n\n\u003e Throws a good user friendly error if a function is called, useful in stubs during testing\n\n[![NPM][npm-icon]][npm-url]\n\n[![Build status][ci-image]][ci-url]\n[![semantic-release][semantic-image]][semantic-url]\n[![standard][standard-image]][standard-url]\n[![renovate-app badge][renovate-badge]][renovate-app]\n\n## Install\n\nRequires [Node](https://nodejs.org/en/) version 6 or above.\n\n```sh\nnpm install --save-dev not-allowed\n```\n\n## Use\n\nImagine for example you are testing a stubbed method using [sinon](http://sinonjs.org/) and expect the method to be called with string 'foo'.\n\n```js\nconst sinon = require('sinon')\nconst o = {\n  // method to be stubbed\n  say: () =\u003e 'hello'\n}\n// normally, o.say() returns \"hello\"\nconsole.log('o.say()', o.say())\n// \"hello\"\n// but we want to make sure that when called with \"foo\"\n// it returns different value\nsinon.stub(o, 'say')\n  .withArgs('foo').returns(42)\no.say('foo')\n// 42\n```\n\nBut what happens when we call the mocked method without any arguments `o.say()`? Or with unexpected argument `o.say('bar')`? Sinon will just return `undefined`, which can cause very tricky errors!\n\nWe really want to throw an error in this case because somehow our code is doing an unexpected call. Sinon already provides such feature: [`stub.throws()`](http://sinonjs.org/releases/v4.5.0/stubs/) and even `stub.throws(function() { return new Error(); })` where we can form a detailed message. So we can stub just `o.say('foo')` and throw an error for any other call.\n\n```js\nsinon.stub(o, 'say')\n  .throws('nope')\n  .withArgs('foo').returns(42)\no.say('foo')\n// 42\no.say('bar')\n// throws Error('nope')\n```\n\n**But**\n\nThe thrown error does NOT tell us _the arguments_ to the incorrect call! So we would not have any idea that the unexpected call was `o.say('bar')`! Luckily there is a method `.callsFake(fn)` in Sinon to call your function instead of throwing \"dumb\" error. We can use this method to get call's details and throw a very good error\n\n```js\nfunction onlyFoo(a) {\n  throw new Error(`Cannot call this stub with argument ${a}`)\n}\nsinon.stub(o, 'say')\n  .callsFake(onlyFoo)\n  .withArgs('foo').returns(42)\no.say('bar')\n// throws Error('Cannot call this stub with argument bar')\n```\n\nThis NPM package just makes it convenient - it serializes arguments intelligently and throws an error.\n\n```js\nconst notAllowed = require('not-allowed')\nsinon.stub(o, 'say')\n  .callsFake(notAllowed)\n  .withArgs('foo').returns(42)\no.say('bar', 'bar', 42)\n// throws Error('Not allowed to call this function with arguments\n//    foo bar 42\n// ')\n```\n\n### Small print\n\nAuthor: Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt; \u0026copy; 2018\n\n* [@bahmutov](https://twitter.com/bahmutov)\n* [glebbahmutov.com](https://glebbahmutov.com)\n* [blog](https://glebbahmutov.com/blog)\n\nLicense: MIT - do anything with the code, but don't blame me if it does not work.\n\nSupport: if you find any problems with this module, email / tweet /\n[open issue](https://github.com/bahmutov/not-allowed/issues) on Github\n\n## MIT License\n\nCopyright (c) 2018 Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt;\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\n[npm-icon]: https://nodei.co/npm/not-allowed.svg?downloads=true\n[npm-url]: https://npmjs.org/package/not-allowed\n[ci-image]: https://travis-ci.org/bahmutov/not-allowed.svg?branch=master\n[ci-url]: https://travis-ci.org/bahmutov/not-allowed\n[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg\n[semantic-url]: https://github.com/semantic-release/semantic-release\n[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg\n[standard-url]: http://standardjs.com/\n[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg\n[renovate-app]: https://renovateapp.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fnot-allowed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbahmutov%2Fnot-allowed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fnot-allowed/lists"}