{"id":20609565,"url":"https://github.com/mateodelnorte/mockrequire","last_synced_at":"2025-06-15T09:32:46.751Z","repository":{"id":512279,"uuid":"3946910","full_name":"mateodelnorte/mockrequire","owner":"mateodelnorte","description":"Simple module for mocking required dependencies. Works with any testing suite. ","archived":false,"fork":false,"pushed_at":"2025-03-03T18:29:27.000Z","size":31,"stargazers_count":17,"open_issues_count":9,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T15:51:57.877Z","etag":null,"topics":["mock","testing"],"latest_commit_sha":null,"homepage":"","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/mateodelnorte.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":"2012-04-06T04:40:57.000Z","updated_at":"2019-04-08T18:44:00.000Z","dependencies_parsed_at":"2023-07-05T15:03:16.756Z","dependency_job_id":"f2f6c40a-4a88-41ab-ba93-e41c692a026a","html_url":"https://github.com/mateodelnorte/mockrequire","commit_stats":{"total_commits":42,"total_committers":8,"mean_commits":5.25,"dds":"0.40476190476190477","last_synced_commit":"6271b4d9b22c176fd367deebae2625d320ca9f2b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mateodelnorte%2Fmockrequire","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mateodelnorte%2Fmockrequire/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mateodelnorte%2Fmockrequire/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mateodelnorte%2Fmockrequire/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mateodelnorte","download_url":"https://codeload.github.com/mateodelnorte/mockrequire/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248744108,"owners_count":21154810,"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","testing"],"created_at":"2024-11-16T10:13:54.879Z","updated_at":"2025-04-15T04:30:44.374Z","avatar_url":"https://github.com/mateodelnorte.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/mateodelnorte/mockrequire.svg?branch=master)](https://travis-ci.org/mateodelnorte/mockrequire) [![Greenkeeper badge](https://badges.greenkeeper.io/mateodelnorte/mockrequire.svg)](https://greenkeeper.io/)\n\n# MockRequire\n\nSimple module for mocking required dependencies. Works with any testing suite. (https://github.com/mateodelnorte/mockrequire).\n\n\n### Example Usage \n\nImagine your application uses the following module to onboard a user, in reaction to a recieved event. A service will require the module and call it, passing the event as a parameter. But, in the course of developing your application, you'd like to test the module in isolation from the rest of your code. The module depends on another module, 'my_db_lib'. \n\nHow can you test the module in isolation?\n\n```\nvar db = require('my_db_lib');\n\nmodule.exports = function onboardUser (event) {\n\n  // we want to test this function's behavior, but need to isolate the behavior of the module apart from the behavior of it's dependency, the db. \n\n  db.User.findByEmail(event.email, function (err, user) {\n    if(err) throw err;\n\n    user.onboarding = 'complete';\n\n    user.save(function (err) { \n      if(err) throw err; \n    });\n  });\n};\n```\n\nMockRequire provides a simple means of allowing you to mock dependencies of any module you require(). Want to unit test your code without ever having to hit a database, even though your code is require-ing your datastore modules directly? MockRequire is exactly what you need. \n\nThe following example uses 'mocha' and 'should' to create a unit test for the module above. We also use 'mockrequire' to mock out our dependencies, parts of our module we don't care to isolate in our test. \n\n```\nrequire('mocha'),\nrequire('should');\nvar mockrequire = require('mockrequire');\n\nvar user = (function() {\n  return {\n    save: function (cb) {\n      cb(null);\n      this.saved = true;\n    }\n  };\n})();\n\n// instead of require()ing our handler directly, we can mockrequire() it and supply an object containing any child dependencies we would like to mock as well. Here we're mocking my_db_lib\n\nvar onboardUser = mockrequire('./handler', {\n  'my_db_lib': {\n    User: { \n      findByEmail: function (email, cb){\n        cb(null, user);\n      }\n    }\n  }\n});\n\n// we create our unit tests with mocha\n\ndescribe('userPaymentComplete()', function(){\n  onboardUser({ email: 'fake@email.com' });\n  it('should set onboarding as \\'complete\\'', function() {\n    user.should.have.property('onboarding').equal('complete');\n  });\n  it('should save user', function() {\n    user.should.have.property('saved').equal(true);\n  });\n});\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmateodelnorte%2Fmockrequire","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmateodelnorte%2Fmockrequire","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmateodelnorte%2Fmockrequire/lists"}