2.2.1 • Published 5 months ago

@axway/api-builder-test-utils v2.2.1

Weekly downloads
275
License
SEE LICENCE IN LI...
Repository
-
Last release
5 months ago

@axway/api-builder-test-utils

A set of utilities for testing projects and custom flow-nodes for API Builder.

Getting started

To get started with API Builder plugin development, see @axway/api-builder-sdk. The template for new flow-nodes includes both the SDK and test utils.

This module comes with a utility to help test your flow-nodes, MockRuntime.

const { MockRuntime } = require('@axway/api-builder-test-utils');

Your plugin exports the getPlugin function in index.js. This will be required in your unit-tests.

const getPlugin = require('../src');

The plugin will be loaded and invoked by the API Builder runtime, so it is necessary to use MockRuntime to emulate this for unit-testing.

const plugin = await MockRuntime.loadPlugin(getPlugin);

The runtime instance has a validate function that ensures the flow-node adheres to the schema.

it('should define valid flownodes', () => {
    // if this is invalid, it will throw and fail
    plugin.validate();
});

Then you can use getFlowNode to obtain a handle to your flow-node and invoke it with various arguments, checking the response as appropriate.

it('should succeed with valid argument', async () => {
    const flowNode = plugin.getFlowNode('myplugin');
    const params = {
        username: 'bob'
    };
    const { value, output } = await flowNode.getSomething(params);
    expect(value).to.deep.equal({ user: true });
    expect(output).to.equal('next');
});

In some cases, your flow-node may require credentials in addition to the standard parameters. They are treated as normal parameters and are passed along with the rest with the params as follows:

it('should succeed with expected arguments', async () => {
    const flowNode = runtime.getFlowNode('myplugin');
    const params = {
      username: 'bob',
      key: '1234' // The authorization parameter
    };
    const { value, output } = await flowNode.getSomething(params);
    expect(value).to.deep.equal({ user: true });
    expect(output).to.equal('next');
});

MockRuntime API

MockRuntime.loadPlugin(getPlugin, pluginConfig, options)

An async function that mocks the API Builder runtime and is used for testing API Builder plugins. Pass it the getPlugin function from index.js. It resolves to a plugin that is suitable for testing.

pluginConfig and options are values which are normally passed from API Builder. These values can be optionally provided from unit tests in order to test that your flow-node actions work with provided config, or when API Builder is configured in different ways.

const plugin = await MockRuntime.loadPlugin(getPlugin);

plugin.validate()

Validates each flow-node within the plugin to ensure they adhere to the schema.

plugin.getFlowNodeIds()

Returns the IDs, in alphabetical order, of each flow-node within the plugin.

plugin.getFlowNode(name)

Obtains a flow-node instance suitable for testing. Method actions are bound to the object instance as asynchronous functions. For example, if you have a method called getSomething:

const flowNode = plugin.getFlowNode('myplugin');
const result = await flowNode.getSomething({ username: 'jbloggs' });
result.value

The value that the action returned or resolved with.

If testing an action which has not been written using the API Builder SDK, this is the value of the second argument that was passed to the output callback.

cb.next(undefined, value);
result.output

The id of the output that was invoked. This will return undefined if the flow-node doesn't define any outputs.

result.callCount

The number of times the output callback was called. Not necesarry for testing plugins which use the SDK.

result.callback

The id of the output callback that was called.

If outputs.next() was called, you would expect the value to be next. If the default callback (i.e. outputs()) was called, this value will be undefined. Not necesarry for testing plugins which use the SDK.

MockLogger API

MockLogger.create()

A function which creates a new logger that mocks the API Builder logger. Pass it to any function which requires an API Builder logger.

Changes

0.1.1

  • #6437: Added getRawPlugin method to MockRuntime.loadPlugin. This returns the entire plugin that is provided to API Builder, and should be tested with caution.
  • #6437: Updated documentation.

0.1.0

  • #6337: Moved MockRuntime and MockLogger from @axway/api-builder-sdk
  • #6337: Added options.stub to MockLogger.create to make it easier to provide a custom stub for each log level.
2.2.1

5 months ago

1.7.1

5 months ago

1.7.0

5 months ago

2.2.0

6 months ago

2.1.0

6 months ago

1.6.1

2 years ago

2.0.2

2 years ago

1.6.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.5.10

2 years ago

1.5.11

2 years ago

1.5.9

2 years ago

1.5.8

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.13

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago