GithubHelp home page GithubHelp logo

require-and-clean's Introduction

Require And Clean

Require a module and then clean it and modules loaded within it from cache

Useful when you want to require a module for test.

Install

$ npm install require-and-clean --save

Know more

Assume that we have two modules:

// /home/hastijs/times-two.js
'use strict';
let i = 1;
module.exports = () => {
  i = i * 2;
  return i;
};
// /home/hastijs/plus-one.js
'use strict';
let i = require('./times-two')();
module.exports = () => {
  i = i + 1;
  return i;
};

Now we run following codes:

// /home/hastijs/
console.log(require('./plus-one')());
console.log(require('./plus-one')());
console.log(require('./times-two')());
// => 3
// => 4
// => 4

Amazing! but why? It is because of Node module caching. But sometimes we DO NOT want caching. For example when we test our code (Unit testing).

The solution is require-and-clean module.

// /home/hastijs/
const requireAndClean = require('require-and-clean');
console.log(requireAndClean('./plus-one')());
console.log(requireAndClean('./plus-one')());
console.log(requireAndClean('./times-two')());
// => 3
// => 3
// => 2

Notice: require-and-clean will not clear cache from modules loaded normaly (with require) before.

// /home/hastijs/
const requireAndClean = require('require-and-clean');
console.log(require('./plus-one')());
console.log(requireAndClean('./plus-one')());
console.log(requireAndClean('./times-two')());
// => 3
// => 4
// => 4

API

requireAndClean(modulePath)

modulePath

Type: string

Path of module you want to load.

Contributing

Everyone is very welcome to contribute to z project. Require And Clean is a HastiJS project so please see HastiJS contributing guidelines before contributing.

License

MIT © HastiJS

require-and-clean's People

Contributors

adelarmand avatar

Watchers

James Cloos 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.