2.0.1 • Published 6 years ago

fit-integration-tests v2.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
6 years ago

FIT Integration Tests

Build Status

FIT (Fast Integration Tests) is a tool to run integration tests fast and in parallel.

Main features

  • Run tests in parallel according to the available CPUs
  • Each test runs on a new isolated node process
  • Test can receive webhooks on a public Url created dynamically
  • The code can use a "await" to stop until a webhook is received
  • Don't rely on mocha or any other test framework
  • Can be used with chai or others assertion tools
  • Fast, I mean... really fast

Setup

1. Download and install the project

npm install fit-integration-tests

2. Create a folder for your integration tests

project    
|-- integration_tests

3. Inside the folder, create a index.js, to start the tests

project    
|-- integration_tests
    |-- index.js    

This is were the tests are configured, here can set the options and the tests folder.

const IntegrationTestFw = require( 'mw-integration-test-fw' );

IntegrationTestFw.run( { path: './tests' } );

4. Create a folder for the actual tests

project    
|-- integration_tests
    |-- index.js
    |-- tests

5. Create some tests

Each of your tests must have a file with this interface:

module.exports = {

  before( env, ctx, logger ) { 

  },

  exec( env, ctx, logger ) { 

  },

  after( env, ctx, logger ) { 

  }
};

Test anatomy

Every test have 3 distinct phases. The main phase exec is mandatory.

If any phase fails, the tests are considered failing.

Phases run in order and synchronously. Each phase is called by a method with the same name:

NameDescription
beforeThis will run before the exec method. If it fails, the test stops here, failing.
execThis will have your test logic. If it fails, the next phase will run anyway. The tests failed.
afterThis will run after the exec method. If if fails, the tests fail.

Arguments

All test methods (before, exec and rollback) receive the same arguments env, ctx, logger:

env

The test environment, this is a object containing any tools the framework provides.

PropertyTypeDescription
serverUrlstringThe public accessible Url so the test can receive webhooks via GET or POST.
asyncEventfunctionAn async function to block the test and await for a event to occur. See events below. If the event don't happen in the time limit, it throws an error.
.asyncEvent usage:

.asyncEvent is used to await to a specific async event from FIT to happen. It always returns a promise.

Arguments:

NameTypeDescription
eventNamestringEvent name to await. Possible: http-get, http-post
thresholdnumberMax time in milliseconds to await for this event to happen, throws an Error if the event doest no happen. Default: one minute

Events results:

  • http-get: resolves an object:
{
  url: 'url', // witch Url received the event
  headers: { }, // http headers received
  qs: { } // deserialized query string object received on the url
}
  • http-post: resolves an object:
{
  url: 'url', // witch Url received the event
  headers: { }, // http headers received
  qs: { }, // deserialized query string object received on the url
  body: { } // deserialized post body
}

Example:

// do some request
axios.get( env.serverUrl );


// get the response
const response = await env.asyncEvent( 'http-get' );

ctx

The test context. Used to shared values between each test phase.

Start value is an empty object ({}).

Each phase can change it at will.

logger

A handy tool to print test outputs.

Important: As the tests run in parallel, any stdout like console.log will be mixed across all tests, use this logger instead.

The logger have the following methods:

MethodDescriptionOutput color
logUse this to print some basic logswhite
warnUse this to print something importantyellow
okUse this to print something goodgreen
errorUse this to print an errorred
infoUse this to print an oddityblue

All logger methods can receive any number of arguments, which will be converted to string and concatenated using a space.

Tests path

FIT reads the tests recursively, looking for:

  • Any index.js file inside a folder that ends with _test;
  • Any file that ends with .test.js;
  • A single file, if the path option points to that file.

Examples:

Given that the path folder is ./tests:

project    
|-- integration_tests
    |-- index.js    
    |-- tests
        |-- anything_test
            |-- index.js // this is called!
            |-- helper.js // not called
            |-- other_file.js // not called
        |-- no_so_much_test
            |-- helper.js // not called
        |-- another_folder
            |-- index.js // not called
        |-- common.js // not called
        |-- common.test.js // this is called!

Given that the path folder is ./tests/foo.js:

project    
|-- integration_tests
    |-- index.js    
    |-- tests
        |-- anything_test // not called
            |-- index.js
            |-- helper.js
            |-- other_file.js
        |-- foo.js // this is called!
        |-- foo.test.js // not called

Options

Configurations send to .run() method.

PropertyTypeRequiredDefaultDescription
testsDirstringyesnoneThe directory where the tests will be read from.
timeoutTimestring5 minutesThe time in milliseconds to wait before a test is killed due timeout.
displaySuccessOutputboolfalsePrint out logs from tests that passed.
2.0.1

6 years ago

1.6.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago