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

harvester-test-utils

v1.0.4

Published

Utils for testing hapi-harvester based applications

Downloads

44

Readme

Harvester Test Utils

npm module for testing hapi-harvester based applications

Installation

npm install harvester-test-utils

Requirements

To use harvester-test-utils your project has to meet some prerequisites.

Usage

Testing standard actions

The harvester-test-utils offer a convenient way to test the default routes that are defined in the hapi-harvester framework.

Initialization

The hapi-harvester server needs to be initialized before the test utils can be used. Usually this is done in the before block of the test suite. You have to pass a Promise that eventually will yield a hapijs server instance. The init method also returns a Promise.

const testUtils = require('harvester-test-utils');
const baseTests = new testUtils.BaseTests();
const serverPromise = require('../../lib/server');

before(() => {
  return baseTests.init(serverPromise);
});

Test all routes

The most simple thing is to just test all default routes for a resource, of course only if you have defined all routes.

baseTests.testStandardActions('myResource');

Test specific actions only

In cases where you only defined a few actions for a resource you can test them individually.

baseTests.testGetValidSuccess('myResource');

This only tests the GET action for myResource. Also available are:

baseTests.testPatchValidSuccess('myResource');
baseTests.testPostValidSuccess('myResource');
baseTests.testDeleteValidSuccess('myResource');

Method arguments

All methods take 3 arguments.

baseTests.testGetValidSuccess(resourceName, fixture, options);
  • resourceName is a string with the resource name, e.g. 'myResource'
  • fixture is for passing in your own fixture. By default it will identify the fixtures by name and pick the first. Read more about fixtures.
  • options Currently available options are
  • namespace a string with a namespace for the endpoints. E.g. 'allMyResources'

Init server

In some cases your tests are more complex than just testing default routes. You can still use harvester-test-utils to initialize your server properly. Currently this will just register inject-then (https://github.com/bendrucker/inject-then) with the hapijs server instance. In the future this might do more initialization steps.

const expect = require('chai').expect;
const testUtils = require('harvester-test-utils');
const initServer = new testUtils.initServer;
const serverPromise = require('../../lib/server');

let server = null;

before(() => {
  return initServer(serverPromise).then(_server => {
    server = _server;
  });
});

it('will do some stuff', () => {
  return server.injectThen({
    url: '/myResource',
    method: 'post',
    payload: {
      data: {
        type: 'myResource',
        attributes: {
          fun: 'is good',
          moreFun: 'is better'
        }
      }
    }
  })
  .then(response => {
    expect(response.statusCode).to.equal(201);
  });
});

Fixtures

Fixtures in testing are a convenient way to get ready-to-use objects. You must put all fixtures in a folder called fixtures in your test folder. Fixture files can either be of type .json or .js. It must contain or export an array with at least one element.

Defining Fixtures

Fixture as JavaScript file:

module.exports = [
  {
    id: 'e05bf92b-39a2-4525-9913-a9cda6ddc9a4',
    type: 'myResource',
    attributes: {
      fun: 'is good',
      moreFUn: 'is better'
    }
  }
];

Fixture as JSON file:

[
  {
    "id": "e05bf92b-39a2-4525-9913-a9cda6ddc9a4",
    "type": "myResource",
    "attributes": {
      "fun": "is good",
      "moreFun": "is better"
    }
  }
]

Using Fixtures

Once defined in your project it's easy to load and use them. Anywhere in your test framework just require fixtures. Fixtures will be cached and only be read from disk once.

const testUtils = require('harvester-test-utils');
const fixtures = testUtils.fixtures;

const myResources = fixtures.get('myResource', 0); // gets myResource with index 0 from the array
const allFixtures = fixtures.getAll(); // gets all fixtures in the fixtures folder in an array

Seed data

With harvester-test-utils you can pre-seed your database with fixtures you need for testing.

Drop Collections and Seed

The most simple way is to drop everything and recreate it. This way you can make sure always to test from a fresh database. This method returns a Promise.

const testUtils = require('harvester-test-utils');
const fixtures = testUtils.fixtures;
const seedUtils = new testUtils.Seed();
const serverPromise = require('../../lib/server');
let server = null;

before(() => {
  return seedUtils.init(serverPromise).then(_server => {
    server = _server;
    return seedUtils.dropCollectionsAndSeed(fixtures.getAll());
  });
});

License

The MIT License (MIT)

Copyright (c) 2016 AGCO

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.