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

hyperapp-test

v0.2.1

Published

Helping test your Hyperapp apps.

Downloads

9

Readme

Hyperapp Test

Build Status Codecov npm

Hyperapp Test is a JavaScript Testing utility for Hyperapp that makes it easier to write assertions for app logic related to state changes as a result of actions and the resulting views. This task is complicated by the auto wiring of actions by Hyperapp's state management and the support for asynchronous actions provided by thunks. By providing a low overhead API to solve this, testing app logic can be simple as it would be in Redux or Elm.

Hyperapp Test is unopinionated regarding which test runner or assertion library you use, and should be compatible with all major test runners and assertion libraries out there.

Hyperapp Test is compatible with Hyperapp 0.15.x and Node.js >=4.

Setup

Install with npm / Yarn:

npm i -D hyperapp-test

Then in your test files add the following at the top:

const { testApp } = require('hyperapp-test');

Or if you are using Babel for ES6 module support:

import { testApp } from 'hyperapp-test';

Usage

Assuming we want to test the following basic app:

import { h } from 'hyperapp';
/** @jsx h */

const basicApp = {
  state: {
    message: 'nothing'
  },
  actions: {
    say: (state, actions, message) => ({ message })
  },
  view: ({ message }) => <main>{message}</main>
};

We want to write a test to verify that the say action updates the state.message correctly by checking that the before and after states match our expected values.

Jest

To test this action with Jest we call the handy testApp function passing our basicApp followed by our test assertions comprised of the name of the action to fire (say), the (optional) data to include with the action, and the assertion function that destructures out the states our app went through since the last action. The first item in states is our before state and the second item is the state after our say action. Here is what that test code looks like:

import { testApp } from 'hyperapp-test';

test('a basic app', () =>
  testApp(basicApp, [
    'say',
    'hello',
    ({ states }) => expect(states).toEqual([
      // Before state
      { message: 'nothing' },
      // After state
      { message: 'hello' }
    ])
  ], [
    'say',
    'goodbye',
    ({ states }) => expect(states).toEqual([
      // Before state
      { message: 'hello' },
      // After state
      { message: 'goodbye' }
    ])
  ])
);

Mocha / Chai

The Mocha example works very similarly, just with different assertions:

import { expect } from 'chai';
import { testApp } from 'hyperapp-test';

test('a basic app', () =>
  testApp(basicApp, [
    'say',
    'hello',
    ({ states }) => expect(states).to.deep.equal([
      // Before state
      { message: 'nothing' },
      // After state
      { message: 'hello' }
    ])
  ], [
    'say',
    'goodbye',
    ({ states }) => expect(states).to.deep.equal([
      // Before state
      { message: 'hello' },
      // After state
      { message: 'goodbye' }
    ])
  ])
);

API

testApp

A function intended to be called by tests to start a new app, optionally set some intial state, and fire one or more actions with assertions on the results.

The testApp function is called with:

  • An object of the same shape that would be passed to the app function from Hyperapp.
  • Optionally an intial state object to override the default state before running any test actions.
  • One or more ActionTest definitions.

testApp returns a promise that resolves when the test successfully completes, and rejects when it fails. Here is the overall shape of testApp:

testApp = function(
  app: Hyperapp.App,
  initialState?: object,
  ...tests: ActionTest
): Promise

Each ActionTest is an array that specifies:

  • The name of the action to fire.
  • Optionally the data to include with the action.
  • An assertion function that receives an object with all the states, actions, and views that have resulted since the last action, or from the creation of the app if no prior actions.

This is the shape of each ActionTest:

ActionTest = [
  actionName: string,
  actionData?: any,
  assertion: function({
    states: object[],
    actions: object[],
    views: VirtualNode[]
  })
]

To verify the views were rendered correctly, compare expected VirtualNodes with the actual results:

import { h } from 'hyperapp';
import { testApp } from 'hyperapp-test';
/** @jsx h */

test('a basic app renders the right view', () =>
  testApp(basicApp, [
    'say',
    'hello',
    ({ views }) => expect(views).toEqual([
      <main>hello</main>
    ])
  ])
);

createTestProps

A function that creates props used internally by Hyperapp Test for tracking the last states, actions, and views. This is also responsible for firing the test actions and calling our assertion functions when the processing of the action is complete. Unless you are building your own reusable test library, you probably want to use testApp instead of this.

License

Hyperapp Test is MIT licensed. See LICENSE.