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

noflo-wrapper

v0.5.0

Published

NoFlo component/graph testing and embedding wrapper compatible with BDD/TDD/whatever

Downloads

24

Readme

NoFlo component/graph testing and embedding wrapper

Wraps a component to provide a convenient interface for use in normal JavaScript code. It is compatible with any testing paradigm: TDD/BDD/whatever. Read also Embedding NoFlo.

Benefits

  • Provides easy JavaScript access to long-running NoFlo graphs
  • Reduces boilerplate to set up a component testbed.
  • Provides common high-level methods.
  • Provides low-level access to the component, ports and events.
  • Compatible with different testing frameworks and complex test cases.

Getting started

Install noflo-wrapper and add it to your project's dependecies:

npm install noflo-wrapper --save

Require it:

const Wrapper = require('noflo-wrapper');

Use methods described below and run the tests just as you do it normally with your favorite testing framework.

API

Explanations below contain examples in JavaScript using Mocha and Chai in BDD style. You can also write your tests using any other framework or style.

Loading a component

First you need to create a new Wrapper object to wrap your component or graph:

const t = new Wrapper('my-noflo-app/Multiplier');

The constructor accepts either a full component name (including namespace prefix), or a function returning an instantiated component object, or a NoFlo Graph instance.

In general, components are loaded and wired up asynchronously, so you need to start the wrapper like this (and resolve the Promise) before running any tests:

before(() => t.start());

Advanced options

If the component to be tested is a NoFlo graph, you can pass custom event handlers to the Wrapper constructor:

const t = new Wrapper('my-noflo-app/Multiplier', {
  load: (err, instance) => {
    // This is called after loading the graph
  },
  ready: (err, instance) => {
    // This is called when the network is ready to be attached
  },
});

Sending inputs and expecting output

A high-level receive method listens on output ports for data and groups until a disconnect event.

A high-level send methods sends data followed by a disconnect to one or more input ports.

Here is an example that tests a simple multiplier component:

t.receive('xy', (data) => {
  chai.expect(data).to.equal(30);
  done();
});

t.send({
  x: 5,
  y: 6,
});

Note that receive is called before send, because it binds event handlers asynchronously, while send is almost an instant operation.

Short syntax for send method to send data and disconnect to just one inport looks like this:

t.send('x', 123);

Direct access to component, ports and events

In more complex test cases you might want to send IPs and handle particular events manually:

t.outs.xy.on('data', (data) => {
  chai.expect(data).to.equal(24);
  done();
});

t.ins.x.send(8);
t.ins.x.disconnect();
t.ins.y.send(3);
t.ins.y.disconnect()

Wrapper object provides ins and outs hashmaps of sockets attached to the component.

You can also access the NoFlo network directly via network property:

if (t.network.getActiveProcesses().length) {
  // Do something
});

Receiving multiple data chunks and groups

As receive is triggered by a disconnect event, there might be multiple data packets in the transmission and also some group bracket IPs. In such case they are available as arrays and counts in the callback arguments:

t.receive('xy', (data, groups, dataCount, groupCount) => {
  chai.expect(data).to.eql([4, 10, 18]);
  chai.expect(dataCount).to.equal(3);
  chai.expect(groups).to.eql(['foo', 'bar']);
  chai.expect(groupCount).to.equal(2);
  done();
});

Note that groupCount counts only closed groups via endGroup events, while groups contains unique groups sent to the output.

Receiving from multiple output ports

If a component sends output to multiple ports at the same time and you need to test results from all of them at once, that may require some syncrhonization spaghetti in your specs. But receive simplifies it by accepting a hashmap and returning a Promise that is resolved when results from all outputs in the map have been received:

let div = null;
let mod = null;

t.receive({
  quotient: (data) => {
    div = data;
  },
  remainder: (data) => {
    mod = data;
  },
})
  .then(() => {
    chai.expect(div).to.equal 3
    chai.expect(mod).to.equal 2
    done()
  });

t.send({
  dividend: 11,
  divisor: 3,
});

Using promises to chain subsequent receives

The receive method returns a Promise resolved when a transmission is received, so you can chain subsequent transmissions in a thenable way, e.g.:

t.receive('quotient', (data) => {
  chai.expect(data).to.equal(5);
})
  .then(() => {
    t.receive('quotient', (data) =>
      chai.expect(data).to.equal(8);
      done()
    });
    t.send({
      dividend: 56,
      divisor: 7,
    });
  });
t.send({
  dividend: 30,
  divisor: 6,
});

Capturing Flowtraces

noflo-wrapper supports capturing Flowtraces for your runs. This enables retroactive debugging of the data flow in tools like Flowhub.

You can enable this with:

const t = new Wrapper('my-noflo-app/Multiplier', {
  debug: true,
});

If you want to manage your own Flowtraces, you can also pass in an instance instead:

const { Flowtrace } = new Flowtrace();
const myTrace = new Flowtrace();
const t = new Wrapper('my-noflo-app/Multiplier', {
  flowtrace: myTrace,
});

Under Node.js you can save the captured Flowtrace into a file with:

const tracefile = await t.dumpTrace();

If you want to store it some other way, it can be accessed via t.tracer.

Examples

See complete BDD-style examples in spec folder.

Development

The first thing to start developing this package is:

npm install

Then run bundled Mocha specs:

npm test

Then feel free to hack on the lib and specs.

Changes

  • 0.5.0 (2020-12-10)
    • Wrapper start method now returns a Promise when no callback is provided
    • Added compatibility with NoFlo 1.4 Promises API
  • 0.4.1 (2020-11-27)
    • Fixed an issue with subgraph networks transmitting IIPs twice
  • 0.4.0 (2020-11-26)
    • Now using the new Flowtrace functionality in NoFlo 1.3
    • Wrapper can now also wrap noflo.Graph objects
  • 0.3.0 (2020-09-14)
    • Ported from CoffeeScript to ES6
    • Now using native Promises instead of Bluebird