{"id":15813801,"url":"https://github.com/joincolony/jest-sandbox","last_synced_at":"2026-03-06T09:34:22.790Z","repository":{"id":57280450,"uuid":"88205881","full_name":"JoinColony/jest-sandbox","owner":"JoinColony","description":"🏝 Simple sinon like sandbox for jest","archived":false,"fork":false,"pushed_at":"2020-06-02T23:52:52.000Z","size":4087,"stargazers_count":16,"open_issues_count":17,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-28T07:46:50.514Z","etag":null,"topics":["jest","sandbox","sinon","testing"],"latest_commit_sha":null,"homepage":"","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/JoinColony.png","metadata":{"files":{"readme":"Readme.md","changelog":"CHANGELOG.md","contributing":null,"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":"2017-04-13T20:56:56.000Z","updated_at":"2022-02-07T22:01:47.000Z","dependencies_parsed_at":"2022-09-19T17:21:15.339Z","dependency_job_id":null,"html_url":"https://github.com/JoinColony/jest-sandbox","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoinColony%2Fjest-sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoinColony%2Fjest-sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoinColony%2Fjest-sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoinColony%2Fjest-sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JoinColony","download_url":"https://codeload.github.com/JoinColony/jest-sandbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252794291,"owners_count":21805173,"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":["jest","sandbox","sinon","testing"],"created_at":"2024-10-05T04:06:09.829Z","updated_at":"2026-03-06T09:34:22.736Z","avatar_url":"https://github.com/JoinColony.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jest-sandbox\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/JoinColony/jest-sandbox.svg)](https://greenkeeper.io/)\n\nTrack jest spies and stubs easily.\n\n[![CircleCI](https://circleci.com/gh/JoinColony/jest-sandbox.svg?style=svg)](https://circleci.com/gh/JoinColony/jest-sandbox)\n\nThe one feature I really missed when switching from [sinon.js](http://sinonjs.org) to jest spies and mocks was _[sandboxes](http://sinonjs.org/releases/v2.1.0/sandbox/)_. They allow you to create as many spies and stubs as you want without having to track them all and clear them manually.\n\n### Meet jest-sandbox:\n\n```javascript\nimport createSandbox from 'jest-sandbox';\n\nimport cow from './moo.js';\n\ndescribe('A cow 🐄', () =\u003e {\n  const sandbox = createSandbox();\n\n  // Create as many spies as you like\n  const spyStomach = sandbox.fn();\n  const spyPoop = sandbox.spyOn(cow, 'poop');\n\n  // Clear 'em all with one call\n  beforeEach(() =\u003e sandbox.clear());\n\n  test('Eats grass', () =\u003e {\n    return cow.eatGrass.then(spyStomach).then(() =\u003e {\n      expect(spyStomach).toHaveBeenCalledWith('🌱');\n      expect(spyPoop).toHaveBeenCalledTimes(0);\n    });\n  });\n\n  test('Sometimes poops', () =\u003e {\n    return cow.digest().then(() =\u003e {\n      expect(spyStomach).toHaveBeenCalledTimes(0);\n      expect(spyPoop).toHaveBeenCalledWith('💩');\n    });\n  });\n});\n```\n\n### Install\n\n```shell\nyarn add jest-sandbox -D\n```\n\nSome people prefer `npm`;\n```shell\nnpm i jest-sandbox -D\n```\n\n### API\n\nMake a sandbox:\n\n```javascript\nimport createSandbox from 'jest-sandbox';\n\nconst sandbox = createSandbox();\n```\n\n#### `sandbox.fn([implementation])`\n\nReturns a new [mock function](https://facebook.github.io/jest/docs/mock-function-api.html) and keeps track of it. Similar to calling `jest.fn([implementation])`.\n\n#### `sandbox.spyOn(object, methodName)`\n\nSpies on a method and returns a the wrapped [mock function](https://facebook.github.io/jest/docs/mock-function-api.html). Similar to calling `jest.spyOn(object, methodName)`.\n\n#### `sandbox.clear()`\n\nClears all spies in the sandbox. Actually calls `.mockClear()` on every spy it keeps track of.\n\n#### `sandbox.reset()`\n\nResets all spies in the sandbox. Actually calls `.mockReset()` on every spy it keeps track of.\n\n#### `sandbox.restore()`\n\nRestores all spies in the sandbox. Actually calls `.mockRestore()` on every spy it keeps track of.\n\n#### Note\nKeep in mind that you can still clear, reset and restore the single spies manually, like you're used to.\n\n### Contribute\n\nI'm happy for every feature request, bugfix and / or PR.\n\nRun tests:\n\n```shell\nyarn test # npm test is also ok\n```\n\nBuild the whole shebang:\n\n```shell\nyarn build # npm build works as well\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoincolony%2Fjest-sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoincolony%2Fjest-sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoincolony%2Fjest-sandbox/lists"}