{"id":18996548,"url":"https://github.com/sinonjs/sinon-test","last_synced_at":"2025-04-09T22:17:34.275Z","repository":{"id":3758753,"uuid":"49908092","full_name":"sinonjs/sinon-test","owner":"sinonjs","description":"Wrap your tests in a sandbox with auto-cleanup","archived":false,"fork":false,"pushed_at":"2024-05-15T11:57:24.000Z","size":2006,"stargazers_count":51,"open_issues_count":8,"forks_count":16,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-09T22:17:27.803Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sinonjs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-18T21:56:17.000Z","updated_at":"2024-09-12T11:03:32.000Z","dependencies_parsed_at":"2023-02-10T04:40:26.631Z","dependency_job_id":null,"html_url":"https://github.com/sinonjs/sinon-test","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinonjs%2Fsinon-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinonjs%2Fsinon-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinonjs%2Fsinon-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinonjs%2Fsinon-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sinonjs","download_url":"https://codeload.github.com/sinonjs/sinon-test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119286,"owners_count":21050755,"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-11-08T17:35:51.760Z","updated_at":"2025-04-09T22:17:34.249Z","avatar_url":"https://github.com/sinonjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sinon Test\n\n\u003ca href=\"CODE_OF_CONDUCT.md\"\u003e\u003cimg src=\"https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg\" alt=\"Contributor Covenant\" /\u003e\u003c/a\u003e\n\n\u003e Automatic sandbox setup and teardown for SinonJS\n\n## Why?\n\nInstead of writing tedious setup and teardown code for each\nindividual test case you can let Sinon do all the cleanup for you.\n\nSo instead of doing this (using [Mocha](https://mochajs.org/) syntax):\n\n```javascript\nvar spy1;\nvar spy2;\n\nafterEach(() =\u003e {\n    spy1.restore();\n    spy2.restore();\n});\n\nit(\"should do something\", () =\u003e {\n    spy1 = sinon.spy(myFunc);\n    spy2 = sinon.spy(myOtherFunc);\n    myFunc(1);\n    myFunc(2);\n    assert(spy1.calledWith(1));\n    assert(spy1.calledWith(2));\n});\n```\n\nYou could write just this\n\n```javascript\nit(\n    \"should do something\",\n    test(function () {\n        var spy1 = this.spy(myFunc);\n        var spy2 = this.spy(myOtherFunc);\n        myFunc(1);\n        myFunc(2);\n        assert(spy1.calledWith(1));\n        assert(spy1.calledWith(2));\n    })\n); //auto-cleanup\n```\n\nSinon will take care of removing all the spies and stubs\nfrom the wrapped functions for you. It does this by using\n`sinon.sandbox` internally.\n\nDo notice that we use a `function` and not a arrow function (ES2015)\nwhen wrapping the test with `sinon.test` as it needs\nto be able to access the `this` pointer used inside\nof the function, which using an arrow function would prevent.\n\nSee the [Usage](#usage) section for more details.\n\n## Installation\n\nvia [npm (node package manager)](https://github.com/npm/npm)\n\n    $ npm install sinon-test\n\n## Usage\n\n### Node and CommonJS build systems\n\nOnce initialized, the package creates a context for your test based on a sinon sandbox.\nYou can use `this` in a wrapped test function to create sinon spies, stubs, etc.\nAfter your test completes, the sandbox restores anything modified to its original value.\n\nIf your test function takes any arguments, pass then to the `test` wrapper\nafter the test function. If the last argument is a function, it is assumed to be a callback\nfor an asynchronous test. The test function may also return a promise.\n\nSee the [sinon documentation](http://sinonjs.org/releases/v2.3.5/sandbox/) for more documentation on sandboxes.\n\n`sinon-test` instances need to be configured with a `sinon` instance (version 2+)\nbefore they can be used.\n\n```js\nvar sinon = require(\"sinon\");\nvar sinonTest = require(\"sinon-test\");\nvar test = sinonTest(sinon);\nvar assert = require(\"assert\");\n\ndescribe(\"my function\", function () {\n    var myFunc = require(\"./my-func\");\n\n    it(\n        \"should do something\",\n        test(function () {\n            var spy = this.spy(myFunc);\n            myFunc(1);\n            assert(spy.calledWith(1));\n        })\n    ); //auto-cleanup\n});\n```\n\n### Direct browser usage\n\nIn place of the `require` statements indicated above, in the\nbrowser, you should simply reference the global `sinonTest` after\nincluding a script tag in your HTML:\n\n```html\n\u003cscript src=\"dist/sinon-test.js\"\u003e\u003c/script\u003e\n```\n\nOr if you are in an ES6 Modules environment (modern browsers only), you\nonly need to add an import statement:\n\n```html\n\u003cscript type=\"module\"\u003e\n    import sinon from \"./node_modules/sinon/pkg/sinon-esm.js\";\n    import sinonTest from \"./node_modules/sinon-test/dist/sinon-test-es.js\";\n    const test = sinonTest(sinon);\n\n    it(\n        \"should work\",\n        test(function () {\n            pass();\n        })\n    );\n\u003c/script\u003e\n```\n\n## API\n\n```javascript\nconst test = require(\"sinon-test\")(sinon);\n```\n\nIn order to [configure the sandbox](http://sinonjs.org/releases/latest/sandbox#var-sandbox--sinoncreatesandboxconfig) that is created, a configuration hash can be passed as a 2nd argument to `sinonTest`:\n\n```js\nconst test = require(\"sinon-test\")(sinon, { useFakeTimers: false });\n```\n\nThe only difference to the standard configuration object for Sinon's sandbox is the addition of the `injectIntoThis` property, which is used to inject the sandbox' props into the context object (`this`).\n\n### Backwards compatibility\n\nSinon 1.x used to ship with this functionality built-in, exposed as `sinon.test()`. You can keep all your existing test code by configuring an instance of `sinon-test`, as done above, and then assigning it to `sinon` like this in your tests:\n\n```javascript\nsinon.test = test;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinonjs%2Fsinon-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsinonjs%2Fsinon-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinonjs%2Fsinon-test/lists"}