Git Product home page Git Product logo

ethereumjs-stub-rpc-server's Introduction

npm version

Purpose

Allow for easy stubbing of an Ethereum node when testing Ethereum dApp clients. It allows you to stub out a response to any request with whatever result you like so it is easy to write tests for your dApps without having to run a full Ethereum node or do actual mining. It is intended to be run inside your tests and a new server should be created/destroyed with each test case, though you could re-use across test cases if you are okay with using the same responses in each test.

This library is not intended to simulate the inner workings of a real Ethereum node, only allow you to define canned responses to requests made of an Ethereum node. It supports requests over HTTP, WS or IPC. It dose have some basic simulation build in (can be overriden or removed) for things like eth_sendTransaction, eth_getBlock, net_version (and others) and it has a .mine() function to allow you to simulate mining a block that includes pending transactions. PRs welcome for additional baked-in behaviors, though the goal is to keep them relatively simply with a focus on validating request payloads and returning responses that are shaped correctly.

Usage

describe("my ethereum integration test", () => {
	var server;
	beforeEach(function () {
		server = require('ethereumjs-stub-rpc-server').createStubServer('HTTP', 'http://localhost:1337');
	});
	afterEach(function () {
		server.destroy();
	});

	it("uses a stub server", () => {
		server.addExpectation((requestJso) => requestJso.method === "net_version");
		server.addResponder((requestJso) => (requestJso.method === "net_version") ? "apple" : undefined);
		myPreferredEthereumJsLibrary.netVersion().then((version) => {
			assert.strictEqual(version, "apple");
			server.assertExpectations();
		});
	});
});

To see the public surface area of this project in TypeScript definition format plus JSDocs (which a TypeScript aware editor will understand) check out source/index.d.ts. You can also see the tests for more full featured examples (in particular, see adds sent transaction to block when mined for an interesting one that does mining).

ethereumjs-stub-rpc-server's People

Contributors

micahzoltu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ethereumjs-stub-rpc-server's Issues

EADDRINUSE after force-quitting stub-rpc-server

Getting an unusual error from ethrpc's tests. I force-quit (CTRL+C) the test suite during a run-thru and now I get the following error:

  1) tests that work against any node (test or live) IPC "before each" hook:
     Uncaught Error: listen EADDRINUSE testrpc.ipc
      at Object.exports._errnoException (util.js:1022:11)
      at exports._exceptionWithHostPort (util.js:1045:20)
      at Server._listen2 (net.js:1249:19)
      at listen (net.js:1298:10)
      at Server.listen (net.js:1382:7)
      at new IpcServer (node_modules/ethereumjs-stub-rpc-server/source/ipc-server.js:38:25)
      at Object.createStubServer (node_modules/ethereumjs-stub-rpc-server/source/index.js:8:14)
      at Context.<anonymous> (test/any-node.js:14:29)

(Not sure if this is an ethrpc issue or an ethereumjs-stub-rpc-server issue.)

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.