declarative-test-structure-generator v0

  • Automation
  • Bdd
  • Generator
  • js
  • Mocha
  • Node
  • NPM
  • Structure
  • Test

declarative-test-structure-generator allows writing tests in an object definition style.

{
  // ...
  name: 'admin CAN create',
  test: () => { /* test code here */ }
}

It uses Mocha and exposes its BDD API.

Motivations

The main motivation for this package was to reuse the test generation mechanism in other projects but also to avoid duplication of the documentation.

Disclaimer

This package is not a replacement for Mocha, just a way to reuse code and documentation between some of my projects.

Installation

npm install --save-dev declarative-test-structure-generator

# or
npm i -D declarative-test-structure-generator

Basics

const testGen = require('declarative-test-structure-generator');

testGen.run({
  'Read access': {
    tests: [
      {
        name: 'should do something',
        test: () => {
          expect(1 + 1).to.equal(2);
        }
      },
      {
        name: 'something should be done',
        test: done => {
          waitForSomething(done);
        }
      },
      {
        name: 'waiting for a promise to be kept',
        test: () => waitForAPromise()
      }
    ]
  }
});

The code should be very self explanatory. Read the test suite definition and the test definition for additional API.

Definitions

Test suite definition

The test suite definition accepts the following:

{
  skip:       {boolean}
  only:       {boolean}
  before:     {function | Array[function]}
  beforeEach: {function | Array[function]}
  after:      {function | Array[function]}
  afterEach:  {function | Array[function]}
  tests:      {Array[TestDefinition] | Object<string, TestSuiteDefinition>}
}

tests accepts either an object or an array of test definitions, see Test suites definition structure for more details.

TIP: It exposes the Mocha API through skip, only, before, beforeEach, after, afterEach, see run only / skip, test hooks for more details.

Test definition

The test definition accepts the following:

{
  name:       {string}
  skip:       {boolean}
  only:       {boolean}
  test:       {function}
}
  • name: test name.
  • test: test function.

TIP: It exposes the Mocha API through skip, only, see run only / skip for more details.

Advanced usage

Test suites definition structure

The test suite definition allows for nested structure:

testGen.run(server, {
  'Root level': {
    'Test suite lvl 1': {
      tests: [{
        // test definition
      }]
    },
    'Test suite lvl 2': {
      tests: {
        'Test suite lvl 2.0': {
          tests: {
            'Test suite lvl 2.0.0': {
              tests: [{
                // test definition
              }]
            },
            'Test suite lvl 2.0.1': {
              // each definition is independent
              // you can set hooks, skip, only...
              skip: true,
              tests: [{
                // test definition
              }]
            }
          }
        }
      }
    }
  }
});

The test suite name is the key value associated to its object definition.

tests accepts:

Test hooks

It is possible to run one or many function at different phase of the test. The Mocha hooks API is exposed and you can run any of the before, beforeEach, after, afterEach.

If you need to define many hooks of the same type, simply use an array of functions. Each hook is called in the specified order.

Mocha: Asynchronous hooks

Run only / skip

declarative-test-structure-generator exposes the Mocha skip and only in the definition.

It is possible to run only one test (or test suite) by defining only: true. Similarly skip a test (or test suite) with skip: true.

See usage in the test suite definition / test definition.

View the test logging data

declarative-test-structure-generator uses debug to log information during the tests.

You can view these logs by setting the DEBUG env variable to declarative-test-structure-generator.

DEBUG=declarative-test-structure-generator npm test