Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Latest commit

 

History

History
134 lines (96 loc) · 3.83 KB

README.md

File metadata and controls

134 lines (96 loc) · 3.83 KB

@cybernaut/test ❤️

Package Version Build Status Coverage Status

A solution for writing reliable tests using any Web UI testing framework.

Installation

npm install @cybernaut/test

Getting started

Cybernaut provides a testing-framework independent test runner, that reliably performs individual test steps using a retry mechanism.

Example

The following example uses language features of ECMAScript 2017. Particularly useful are async functions which are natively supported by Node.js 7.6.0 or later.

const {createTestRunner} = require('@cybernaut/test/lib/TestRunner');

/* This function will be called before any of the test steps run. */
async function testSetup() {
  const testContext = {attempts: 0};

  return testContext;
}

/* This function will be called after all the test steps have completed. */
async function testTeardown(testContext) {
  console.log('attempts:', testContext.attempts);
}

const run = createTestRunner(testSetup, testTeardown, {
  testStepMaxRetries: 2,
  testStepRetryDelay: 100 /* ms */
});

const test = run(testContext => [
  async () => {
    testContext.attempts += 1;

    if (testContext.attempts < 3) {
      console.log('test step 1: error');

      throw new Error();
    }

    console.log('test step 1: ok');
  },
  async () => {
    console.log('test step 2: ok');
  }
]);

/* Vanilla */

const throwOnMainThread = error =>
  setTimeout(() => {
    throw error; /* https://stackoverflow.com/a/30741722 */
  });

test().catch(throwOnMainThread);

/* Jest / Mocha */

it('should run reliably', test);

Vanilla output

test step 1: error
test step 1: error
test step 1: ok
test step 2: ok
attempts: 3

More examples

You can find more examples with Puppeteer and Jest in the 👉 @cybernaut/puppeteer 👈 README.

Type definitions

@cybernaut/test/lib/TestRunner

export declare type TestSetup<T> = () => Promise<T>;
export declare type TestTeardown<T> = (testContext: T) => Promise<void>;

export interface TestOptions {
  readonly testStepMaxRetries?: number; /* Default: 4 */
  readonly testStepRetryDelay?: number; /* Default: 1000 ms */
}

export declare type TestStep<T> = (testContext: T) => Promise<any>;
export declare type TestCase<T> = (testContext: T) => TestStep<T>[];
export declare type Test = () => Promise<void>;
export declare type TestRunner<T> = (testCase: TestCase<T>) => Test;

export declare function createTestRunner<T>(
  testSetup: TestSetup<T>,
  testTeardown: TestTeardown<T>,
  testOptions?: TestOptions
): TestRunner<T>;

Built by (c) Clemens Akens. Released under the terms of the MIT License.