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

expect-stream

v0.1.2

Published

chai extension for mocha stream tests

Downloads

12

Readme

ExpectStream

Testing streams sucks. So we built this extension to chai to reduce the suck-factor of testing node streams. Now, let's forget about all the drama and call it a victory:

victory

API

As this module extends chai most of the documentation can be found at their website. ExpectStream extends it with new methods for asserting streams asynchronously:

.produce

This method takes either one or multiple values, which are expected to be the only outputs produced by the stream under test or a function that takes the value and checks if it is the expected one. In the latter case, there are three values your callback should return to signal its state:

  • If the function returns 0, the input value is not the expected value, but does not represent an error case either. The test will go on (or timeout).

  • If the function returns 1, it's the signal that the correct value has finally arrived. The assertion will notify the test, that it is successful.

  • If the function returns -1, it signals that the given input value is erroneous. We will be notified of the error via notify.

.eventually

This property allows ExpectStream to ignore mismatches until the correct values are produced.

.exactly

Using this chainable method will adjust the assertion configuration to strict equality. Otherwise ExpectStream validates, if the produced output contains the expected value.

.filter

This method accepts a function, that can be used to ignore some outputs, which is signalled by returning false in this callback.

.on

This method takes the values the stream under test should use to produce its output, if it isn't only a readable stream.

.notify

This method is used to notify the testrunner if an error occurs or if the streams ended and have produced the expected message

.tap

With this method you can tap into the stream under test and inspect whats going on, if an error happens

Usage

Suppose we have a transform stream, that adds a Mr. to the start of every value, that enters it:

// src/Misterizer
import { Transform } from "stream";

export default class Misterizer extends Transform {
	_transform(name, enc, cb) {
		this.push(`Mr. ${name}`);
		cb();
	}
}

Now we'd like to test it, using mocha with ExpectStream:

// src/__tests__/MisterizerTest
import { expect } from "core-assert";
import Misterizer from "../Misterizer"

describe("MisterizerTest", function() {
	// this test passes
	it("Misterizes Circle", function(done) {
		expect(new Misterizer())
			.to.exactly.produce("Mr. Circle")
			.on("Circle")
			.notify(done);
	});

	// this test will trigger a timeout
	it("Misterizes Square", function(done) {
		expect(new Misterizer())
			.to.eventually.produce("Mr. Circle")
			.on("Square")
			.tap(::console.log) // will print "Square, 0)"
			.notify(done);
	});
});

But the mighty power of ExpectStream offers much more functionality, which will be presented by FunnyBot:

// src/FunnyBot
import { Readable } from "stream";

export default class FunnyBot extends Readable {
	constructor(jokes) {
		super({ objectMode: true });
		
		this.jokes = jokes;
	}
	
	_read() {
		return this.push(this.jokes.shift());
	}
}

All that is missing now are some funny jokes:

// src/__tests__/FunnyBotTest
import { expect } from "@circle/core-assert";
import FunnyBot from "../FunnyBot";

describe("FunnyBot", function() {
	it("tells multiple really funny jokes", function() {
		expect(new FunnyBot([{
			date: Date.now(),
			joke: "Hello, I am FunnyBot!"
		}, {
			date: Date.now(),
			joke: "I am not a joke"
		}, {
			date: Date.now(),
			joke: "What do you see when the Pillsbury Dough Boy bends over? Dough nuts"
		}]).filter(x => x.joke !== "I am not a joke").to.produce([{
			joke: "Hello, I am FunnyBot!" 
		}, {
			joke: "What do you see when the Pillsbury Dough Boy bends over? Dough nuts"
		}]).notify(done);
	});
	
	it("thinks about complex problems", function() {
		expect(new FunnyBot([{
			date: Date.now(),
			joke: "Hello, I am FunnyBot!"
		}, {
			date: Date.now(),
			joke: "I am not a joke"
		}, {
			date: Date.now(),
			joke: "The line is an imaginary invention of imperfect biological life forms."
		}]).to.eventually.produce([{
			joke: "Hello, I am FunnyBot!" 
		}, {
			joke: "The line is an imaginary invention of imperfect biological life forms."
		}]).notify(done);
	});
	
	it("writes up some new material", function() {
		expect(new FunnyBot([{
			firstTry: "Error. Error. Banal."
		}, {
			secondTry: "That has been done before."
		}, {
			lastTry: "Funnybot is now finished with final joke."
		}]).to.produce.exactly([{
			firstTry: "Error. Error. Banal."
		}, {
			secondTry: "That has been done before."
		}, {
			lastTry: "Funnybot is now finished with final joke."
		}]).notify(done);	
	});
});

Testing

ExpectStream is tested with mocha by executing make test in the root directory of the project.

Contributing

If you want to contribute to this repository, please ensure ...

  • to use make for development (it validates the source code and transpiles it to /lib).
  • to follow the existing coding style.
  • to use the linting tools that are listed in the package.json (which you get for free when using make).
  • to add and/or customize unit tests for any changed code.
  • to reference the corresponding issue in your pull request with a small description of your changes.

All contributors are listed in the AUTHORS file, sorted by the time of their first contribution.