Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bogus

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bogus

A small utility for stubbing dependencies in RequireJS based projects

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

bogus Build Status Dependency Status Dependency status

bogus is a small utility for stubbing dependencies when testing RequireJS based projects

In Working Effectively with Legacy Code, Michael Feathers describes Seams. In the vernacular of that book, bogus would be considered a Link Seam.

Getting bogus

You can install bogus into your project using either npm or bower

$ npm install bogus --save-dev

or

$ bower install bogus --save-dev

Usage


// SteeringWheel
define('SteeringWheel', function(){
    function SteeringWheel(){
        this.color = 'black';
    }

    return SteeringWheel;
});

// Car
define('Car', ['SteeringWheel'], function(SteeringWheel){
    function Car(){
        this.steeringWheel = new SteeringWheel();
    }

    Car.prototype.getSteeringWheelColor = function getSteeringWheelColor(){
        return this.steeringWheel.color;
    };

    return Car;
});

// load bogus
define([
    'bower_components/bogus/bogus'  // this is ofc. dependent on where you installed it
], function(
    bogus
){
    describe('myModule', function{
        var Car;

        beforeEach(function(done){
            var fakeSteeringWheel = function(){
                this.color = 'blue';
            };

            // stub out a dependency (SteeringWheel) with our fake
            bogus.stub('SteeringWheel', fakeSteeringWheel);

            // load Car module, that depends on SteeringWheel
            bogus.require('Car', function(module){
                Car = module;
                done();
            });
        });

        afterEach(function(done){
            bogus.reset(done);
        });

        describe('Car', function(){
            describe('getSteeringWheelColor method', function(){
                it('should return the actual color of the SteeringWheel', function(){
                    var car = new Car();

                    assert.equal(car.getSteeringWheelColor(), 'blue');
                });
            });
        });
    });
});

Promises

Both bogus.require and bogus.reset return promises. The beforeEach and afterEach in the example above can be written as:

beforeEach(function(){
    var fakeSteeringWheel = function(){
        this.color = 'blue';
    };

    bogus.stub('SteeringWheel', fakeSteeringWheel);

    return bogus.require('Car').then(function(module){
        Car = module;
    });
});

afterEach(function(){
    return bogus.reset();
});

Stub multiple dependencies

If you're stubbing several dependencies, you can pass a map of them to stub

var firstFake = {};
var secondFake = {};

bogus.stub({
    'path/to/first/dependency': firstFake,
    'path/to/second/dependency': secondFake
});

Development

You can run the tests with

$ npm test

or with

$ mocha

if you have mocha installed as a global

See also

License

MIT: http://mrgnrdrck.mit-license.org

FAQs

Package last updated on 24 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc