npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easystub.js

v0.3.5

Published

EasyStub.js - a simple stubbing framework for javascript

Downloads

37

Readme

easystub.js

EasyStub.js - a simple stubbing framework for javascript (EasyStub.js stubs only JSON services)


Requirements

Optionals


Composition

  • module for grunt-contrib-connect
  • CasperJS API to modify stubs from a casper test
  • Javascript API to modify stubs from an HTML page
  • NodeJS API to set a custom stub server

Prerequisite

To execute at least one time

npm install easystub.js --save-dev

module for grunt-contrib-connect

This module create the stub server.

Usage

Add this lines in your Gruntfiles.js

Example:

grunt.initConfig({
    connect: {
        dev: {
            options: {
                port: 8000,
                middleware: function (connect) {
                    return [
                        connect.static(require('path').resolve('app/')),
                        
                        /* The following lines add Easystub server */ 
                        require('easystub.js').connect({
                            stubsFile: 'conf/stubs.json'
                        })
                        /* End */
                    ];
                }
            }
        }
    }
});

Options

  • stubsFile: path to the configuration file

Configuration file

The configuration file is a JSON file composed of regular expressions matching URLs and key data to be returned in the HTTP request value.

Example:

{
    "^/services/my-service$": {
        "my_custom_data_1": 123,
        "my_custom_data_2": 456
    },
    "^/services/my-second-service$": {
        "my_custom_data_1": 789,
        "my_custom_data_2": 963
    }
}

CasperJS API

With Easystub.js API for CasperJS, you can control the data returned by the stub, from your CasperJS tests.

Functions

  • casper.startInterceptor(urlExp, data): Start or change a stub
  • casper.stopInterceptor(urlExp): Stop a stub

Usage

Add this lines in your CasperJS test files

Example:

'use strict';

// Add startInterceptor and stopInterceptor to casper
require('../../node_modules/easystub.js/client/src/casper-interceptor.js')(
    casper);

casper.test
    .begin(
        'Your casper test',
        function (test) {
            casper
                .start('http://localhost:8000/index.html', function () {
                	// Load a page before use Easystub.js API
                    this.waitForSelector('body', function () {});
                })
                .then(function () {
                	// Change values of default stubs
                	this.startInterceptor('^/services/my-service$', {
       	 				"my_custom_data_1": 1,
        				"my_custom_data_2": 1
                	});
                                
                	// Or create new stubs
                	this.startInterceptor('^/services/my-new-service$', {
                    	"name": "Hello world"
                	});
                })
                .then(function () {
                    // your casper tests here
                })
                .then(function () {
                    // change values of started stub
                	this.startInterceptor('^/services/my-service$', {
       	 				"my_custom_data_1": 2,
        				"my_custom_data_2": 2
                	});          
                })
                .then(function () {
                    // your casper tests here
                })
                .then(function () {
                	// stop stubs
                	this.stopInterceptor('^/services/my-service$');
                	this.stopInterceptor('^/services/my-new-service$');
          		})
          		.run(function () {
                    test.done();
                });
        });

Javascript API

With Easystub.js API for Javascript, you can control the data returned by the stub, from your HTML IHM.

Prerequisite

Add this lines in your HTML file

<!-- load socket.io api -->
<script src="/socket.io/socket.io.js"></script>

<!-- load Easystub.js api -->
<script src="/easystub.js"></script>

Functions

  • easystub.send(urlExp, data): Start or change a stub
  • easystub.remove(urlExp): Stop a stub

Usage

Use it in HTML or a Javascript file

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Easystub.js test</title>
		
		<script src="/socket.io/socket.io.js"></script>
		<script src="/easystub.js"></script>
		
		<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
	</head>
	<body>
		<input type="button" data-action="change" value="change"/>
		<input type="button" data-action="stop" value="stop"/>
		
		<script>
			$('[data-action="change"]').click(function () {
				easystub.send('^/services/my-service$', {
       	 			"my_custom_data_1": 1,
        			"my_custom_data_2": 1
                });
			});

			$('[data-action="stop"]').click(function () {
				easystub.remove('^/services/my-service$');
			});
		</script>
	</body>
</html>

NodeJS API

With Easystub.js API for NodeJS, you can create and manage an Easystub.js server from NodeJS.

Warning: Client files "/easystub.js" and "/socket.io/socket.io.js" are not share with this API Warning: The default stub don't work with this API Warning: The Casper API don't work with this API

Functions

  • listen(port): Start the easystub.js server (websocket)
  • has(urlExp): Return if a stub is registered or not
  • get(urlExp): Return the data register for the gived regulary expression
  • getList(): Return all register stubs

Usage

Example:

var serverEasyStub = require('easystub.js').server;
var express = require('express');
var app = express();

app.get('/my-service', function(req, res){
  var stub;
  
  if (serverEasyStub.has('/my-service')) {
  	stub = serverEasyStub.get('/my-service');
  	res.send(stub.data);
  } else {
  	res.send('hello world');
  }
});

app.listen(3000);
serverEasyStub.listen(3001);