Coder Social home page Coder Social logo

declarative-test's Introduction

Declarative testing for JavaScript

This is a proof of concept of a few ideas floating around in my head for quite some time now.

The idea is to treat tests like data and for modules to export this data for a test runner to execute:

const { behavior, assertion } = 'declarative-test';

module.exports = [
  behavior('1 is 1', [assertion(() => 1 == 1)])
  behavior('something completely different', () => {
    const a = 1;
    const b = 2;

    return [
      assertion(() => a === b - 1),
      assertion(() => a + 1 === b)
    ];
  })
];

A module represents a Test, which is a collection of behaviors (test cases), which in turn are either an array of assertions or a function returning an array of assertions. (NOTE: There is a very high chance this will change very soon. But it's getting late and I have to provide a README…)

The idea is in contrast to the way popular test frameworks act: the ones I know tend to make the test modules either a very special module that can only be required by the test runner, or a test runner itself.

Usage

$ git clone …
$ cd …
$ npm install
$ node ./node_modules/typescript/bin/tsc
$ node
> test = { moduleName: 'built/example', behaviors: require('./built/example').default }
{ moduleName: 'built/example',
  behaviors: [ [ '1', [Function] ], [ '2', [Array] ] ] }
> require('./built/run').run(test).results
[ { test: { moduleName: 'built/example', behaviors: [Array] },
    behavior: [ '1', [Function] ],
    assertion: [Function],
    success: false,
    message:
     { AssertionError [ERR_ASSERTION]: undefined == true
    at exports.default.test_1.behavior (/Users/apanek/panek/declaratest/built/example.js:11:9)
    at /Users/apanek/panek/declaratest/built/run.js:33:15
    at Array.reduce (<anonymous>)
    at Object.exports.run (/Users/apanek/panek/declaratest/built/run.js:49:26)
    at repl:1:24
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at REPLServer.defaultEval (repl.js:240:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:441:10)
       generatedMessage: true,
       name: 'AssertionError [ERR_ASSERTION]',
       code: 'ERR_ASSERTION',
       actual: undefined,
       expected: true,
       operator: '==' } },
  { test: { moduleName: 'built/example', behaviors: [Array] },
    behavior: [ '2', [Array] ],
    assertion: { description: 'always true', expression: [Function] },
    success: true } ]

declarative-test's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.