4.0.0 • Published 1 year ago

decoder-api-tester v4.0.0

Weekly downloads
-
License
ICS
Repository
gitlab
Last release
1 year ago

ApiTester

The goal of this package is to test your project's decoders with their corresponding api.

We recommend using the typescript-json-decoder package which provides detailed error messages.

Features and specifications

  • The tested decoders must throw an error if it cannot decode a json.
  • A CLI is provided to use run the tests.
  • A report can be generated as an html file using the -r option.
  • A complete log of the tests is generated after each execution.
  • The process will always return 1 if one or more test failed.
  • This package uses Axios to run the Http requests.

Setup

Instalation : npm i decoder-api-tester


Create the apitester-config.ts file in the root of your project.

A configuration contains a list of API configuration which contains a list of test configuration.

import { ApiTesterConfig } from "api-tester";
import { firstDecoder, secondDecoder } from "src/decoders";
import { getAccessToken, isWantedFormat } from "src/utils";

const config: ApiTesterConfig = {
    apisConfig: [
        {
            baseUrl: "https://my-api.com/",
            defaultMethod: "post",
            defaultinterceptor: (axiosConfig: AxiosRequestConfig, context: any) => {
                axiosConfig.headers["AccessToken"] = getAccessToken();
                axiosConfig.params = { id: context.myId }
                return axiosConfig;
            },
            defaultBeforeDecode: (json: unknown) => {
                if (json.code == 404) {
                    throw 'Error 404';
                }
            },
            defaultOnDecoded: (data: Data, json: any, setContext: (context: any) => void) => {
                if (!isWantedFormat(data.formatedString)) {
                    throw 'formatedString is not on the right format';
                }
                setContext({ myId: data.array[0].id })
            },
            defaultQueryParameters: {
                apiKey: "my-api-key"
            },
            defaultHeaders: {
                "Connection": "Keep-Alive"
            },
            defaultData: {
                foo: "bar"
            },
            tests: [
                // This test will use the default options 
                {
                    description: "Post data",
                    endpointPath: "data/post",
                    decoder: firstDecoder
                },
                {
                    description: "Get random data",
                    endpointPath: "data/random",
                    method: "get",
                    decoder: secondDecoder,
                    interceptor: (axiosConfig: AxiosRequestConfig) => {
                        return axiosConfig;
                    },
                    beforeDecode: (json: unknown) => {
                        if (json.code == 404) {
                            throw 'Error 404';
                        }
                    },
                    onDecoded: (data: Data) => {
                        if (!isWantedFormat(data.formatedString)) {
                            throw 'formatedString is not on the right format';
                        }
                    },
                    queryParameters: {
                        limit: false
                    },
                    headers: {},
                    data: {},
                },
            ],
        }
    ]
};

export default config;

Run the command api-tester

Type api-tester -h to show command manual.

Config parameters

Common parameters

The default options set in the api will be used if none are set in a test. Both the default functions and the functions will run if they are set.

AttributeTypeDescription
method'get' | 'delete' | 'head' | 'options' | 'post' | 'put' | 'patch' | 'purge' | 'link' | 'unlink'http rest method
decoder(input: unknown) => Tdecoder function
interceptor(axiosConfig: AxiosRequestConfig, contest: any) => AxiosRequestConfigfunction called after the axios config object is generated, it must return axiosConfig
beforeDecode(json: any) => voidfunction called after the axios config object is generated, it must return axiosConfig
onDecoded(data: T, json: any, setContext: (context: any) => void) => voidfunction called after decoder, it must throw an error if the decoded data or the received raw json is not valid
queryParametersDict<string[] | string | number | boolean>query parameters of the request
headersDict<string | number | boolean>headers of the request
dataanydata stored in the body of the request

Api

AttributeTypeDescription
baseUrlstringbase url of the api
testsTest[]tests configuration

Test

AttributeTypeDescription
descriptionstringname or description of the test
endpointPathstringroute to the endpoint