https://github.com/gobwas/cuculus
Simplest require mocking
https://github.com/gobwas/cuculus
Last synced: about 1 month ago
JSON representation
Simplest require mocking
- Host: GitHub
- URL: https://github.com/gobwas/cuculus
- Owner: gobwas
- Created: 2015-07-17T10:16:25.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-06T15:15:36.000Z (over 9 years ago)
- Last Synced: 2025-02-25T16:15:32.834Z (2 months ago)
- Language: JavaScript
- Size: 133 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url]
> Simplest require mocking
## Whats up
Hey there!
This is a simple module mocking tool for your super clever unit testing.
## Install
```sh
$ npm install --save-dev cuculus
```## Usage
The simple case:
```js
var cuculus = require("cuculus");cuculus.replace("fs", {
writeFile: function(path, contents) {
// ...
}
});// now fs is your object
cuculus.restore("fs");
// now fs is original
```Or:
```js
var cuculus = require("cuculus"),
restorer;restorer = cuculus.replace("fs", {
writeFile: function(path, contents) {
// ...
}
});// now fs is your object
restorer();
// now fs is original
```Complete case:
```js
var cuculus = require("cuculus");
// mocking library
// feel free to use your favorite
var sinon = require("sinon");cuculus.modify("fs", function(fs, onRestore) {
var stub;stub = sinon.stub(fs, "writeFile", function(path, contents) {
// your actions here
});// register restore middleware
onRestore(stub.restore.bind(stub));return fs;
});// your tests here
// and after all, or, between each test
cuculus.restore("fs");// now fs is native and without stubs
```## API
##### cuculus.replace(name: string, stub: Any) : Function()
Complete replace the module named `name` with `stub`. Returns function, that
simple proxy to `cuculus.restore(name)`.##### cuculus.modify(name: string, replacer: Function(current: Any, onRestore: Function(fn: Function))) : Function()
Modifies current module with `replacer` function. If `replacer` modifies object, then `restore`
method will not restore the changes, until you not register the backupers with `onRestore` function.##### cuculus.restore(name: string[, steps: number])
Restores module `name`. If it was modified multiple times, restores to the root, until the `steps` limit is not given.
##### cuculus.drop(name: string)
Drops the cached module from `require`. `name` should be a module name or a full path to the js file.
## License
MIT © [Sergey Kamardin](https://github.com/gobwas)
[npm-image]: https://badge.fury.io/js/cuculus.svg
[npm-url]: https://npmjs.org/package/cuculus
[travis-image]: https://travis-ci.org/gobwas/cuculus.svg?branch=master
[travis-url]: https://travis-ci.org/gobwas/cuculus
[daviddm-image]: https://david-dm.org/gobwas/cuculus.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/gobwas/cuculus