@axway/api-builder-test-utils

Utilities for testing API Builder components

Usage no npm install needed!

<script type="module">
  import axwayApiBuilderTestUtils from 'https://cdn.skypack.dev/@axway/api-builder-test-utils';
</script>

README

@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.setOptions(options)

Sets options on the plugin loaded from MockRuntime.loadPlugin.

options.validateInputs - When enabled, when the action method is invoked with an input parameter, the MockRuntime will validate the value against the method's JSON schema and throw an Error if invalid. This ensures that the test's inputs adhere to the expected parameter JSON schema. The default is false. Note that this is a unit-test feature only to ensure that developers do not unintentionally test with invalid inputs. In your unit-tests, you will want to disable this input validation to verify that your action method can handle unexpected inputs during runtime. This is because flow-node inputs are not validated at runtime, so you still need to test that your flow-node handles invalid inputs.

options.validateOutputs - When enabled, when the action method triggers an output (e.g. "next") with a value, the MockRuntime will validate the output value against the output's JSON schema and throw an Error if invalid. This ensures that the responses from the action method adhere to the defined output JSON schema. The default is false. Note that this is a unit-test feature only to ensure that developers do not unintentionally test with invalid outputs.

plugin.validate()

Validates each flow-node within the plugin to ensure they adhere to the JSON 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.

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

The mock logger comes with all the functions mocked, allowing you to assert any log message. See simple-mock for more info about the way we mock these functions.

const logger = MockLogger.create();
logger.info('my log');
expect(logger.info.calls).to.have.length(1);
expect(logger.info.calls[0].args[0]).to.equal('my log');
const logger = MockLogger.create();

const kafkaLogger = logger.getScoped('kafka');
kafkaLogger.info('message recieved');

expect(logger.scopes.get('kafka').info.calls).to.have.length(1);
expect(logger.scopes.get('kafka').info.calls[0].args[0]).to.equal('message recieved');

Enable logging to console

MockLogger now supports logging to console. This enables you to see log messages from your flow-node, and can help when writing your unit-tests. You can enable the debug logging by setting the LOG_LEVEL environment variable when starting your tests:

LOG_LEVEL=info npm test

Accepted values are (in order of most-verbose to least) trace, debug, info, warn, error, fatal and none.

In the example above, we set the LOG_LEVEL to info. This means you would be able to see all logs at this level and above, meaning info, warn, error and fatal.

LOG_LEVEL only controls if the logs are output to console, and will not be impacted by manually setting the logger's logger.level().

Changes

1.5.3

  • 7206: Internal clean up.

1.5.2

  • 7123: Bumped @axway/api-builder-sdk dependency.

1.5.1

  • 7057: Update documentation links.

1.5.0

  • 7066: The MockLogger now supports obtaining the current log level weight, e.g. by calling logger.level() with no arguments.

  • 7066: The MockLogger now supports programmatically changing level, e.g. by setting logger.level('INFO').

  • 7066: The MockLogger now supports logger.willLogAt('INFO'). It will return true if the configured log level is compatible with the provided upper-case log level.

1.4.0

  • 7010: The MockRuntime now supports input parameter validation when unit-testing and when the option validateInputs is enabled (see setOptions).

  • 7010: Improved an error message when a method was invoked with an unknown parameter.

1.3.0

  • 6999: The MockLogger can now log to console by providing LOG_LEVEL=<desired log level> when running your tests.

1.2.1

  • 6835: Bumped @axway/api-builder-sdk dependency.

1.2.0

  • 6786: Added support for scope and getScoped methods in MockLogger.

  • 6786: All MockLogger methods now come mocked with simple-mock.

  • 6786: Deprecated options argument to MockLogger.create(). This is no longer required to provide a stub function.

1.1.16

  • 6835: Bumped @axway/api-builder-sdk dependency.

1.1.15

  • 6919: Bumped @axway/api-builder-sdk dependency.

1.1.14

  • 6826: Bumped @axway/api-builder-sdk dependency.

1.1.13

  • 6877: Bumped @axway/api-builder-sdk dependency.

1.1.12

  • 6833: Bumped @axway/api-builder-sdk dependency.

1.1.11

  • 6865: Bumped @axway/api-builder-sdk dependency.

1.1.10

  • 6775: Bumped @axway/api-builder-sdk dependency.

1.1.9

  • 6703: Bumped @axway/api-builder-sdk dependency.

1.1.8

  • 5401: Bumped @axway/api-builder-sdk dependency.

1.1.7

  • 6725: Bumped @axway/api-builder-sdk dependency.

1.1.6

  • 6660: Bumped @axway/api-builder-sdk dependency.

1.1.5

  • 6624: Bumped @axway/api-builder-sdk dependency.

1.1.4

  • 6546: Bumped @axway/api-builder-sdk dependency.

1.1.3

  • 6534: Bumped @axway/api-builder-sdk dependency.

1.1.2

  • 6522: Bumped @axway/api-builder-sdk dependency.

1.1.1

  • 6463: Bumped @axway/api-builder-sdk dependency.

1.1.0

  • 6461: Added setOptions to plugin that is returned from MockRuntime.loadPlugin, and an option validateOutputs that, when enabled, will validate output values against the output's JSON schema when the action method triggers an output. Defaults to false.

1.0.1

  • 6338: Bumped axway-flow-schema and @axway/api-builder-sdk.

1.0.0

  • 6441: No changes. First official release of @axway/api-builder-test-utils.

0.1.2

  • 6442: Fix bug in MockRuntime.loadPlugin's mockAction where parameters and authorizations were being provided as empty objects when API Builder would provide them as null.

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.