Giter VIP home page Giter VIP logo

js-cjs-dependency-injection's Introduction

Example of JavaScript Dependency Injection

Dependency injection is where an object supplies dependencies for another object. In this example project some CommonJS modules, such as unit tests, are able to override require() dependencies of other modules in the project.

How it works?

Every module imports other modules using the project root index.js file which is the central hub for all required modules.

The root index.js file exports an object with lazy loaded require statements. And the results of these statements can be overriden using arguments to Object.assign().

For example,

  • thirdparty/index.js is a module that exports { debug }
  • src/index.js is a module that exports { Widget }
  • In a unit test, we want to override debug with a mock.
  • In the unit test, before importing the Widget, we override debug like so:
const sinon = require('sinon')
const { debug: createDebug } = require('../index').thirdparty({ debug: sinon.stub() })
const { Widget } = require('../index').app()

Because the root index.js module is cached by the require.cache, its returned keys such as debug are now cached with the new sinon.stub() object instead of the original debug module. Allowing the unit test to check calls on debug().

The syntax that allows this, in the root index.js file, looks like this -

module.exports = {
    app: (inject) => Object.assign(require('./src'), inject),
    thirdparty: (inject) => Object.assign(require('./thirdparty'), inject),
}

Should a more involved hierarchy of dependencies be needed, the same pattern can be extended to do a deep Object.assign instead of the shallow example.

js-cjs-dependency-injection's People

Contributors

kesor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.