Code Monkey home page Code Monkey logo

specify's Introduction

# specify

specify is the simplest way i could think to do node.js testing

it works with sync code and async code all the same

please use versions ~0.6.x for node 0.6 and ~1.0.x for node 0.8 or superior

if you don't like reading and want to see some code you can look at nano's tests and learn there. also read the specify source code, its just a couple of lines of code

var specify = require('specify');

specify('create_by_secret', function (assert) {
  user.create_by_secret({invitation_code: "1234321!!"}, function (err) {
    assert.equal(err.eid, "ec:api:user:create_by_secret:wrong_code");
    assert.equal(err.status_code, 400);
  });
});

specify.run();

the assert calls are callback functions that wrap the assert module. specify figures out how many callbacks you are calling by static-analysis. putting it simply it counts the number of times you wrote assert.. when that number of assertions is met, or when a timeout occurs, that test is complete and we can execute the next one.

static analysis does not work for a for loop and some other flow control constructs. in that case you can use assert.expect(nr) to tell specify how many assertions to expect:

specify('more_assertions_than_asserts', function(assert) {
  assert.expect(5);
  for(var i in [1,2,3,4,5]) {
    assert.equal(i,i);
  }
});

specify runs tests in one by one, not in parallel. if you set assert.expect higher than the number of assert. function calls the execution will stop, and your current test will never finish. you can circumvent this by setting a timeout:

specify('foo', 50, function (assert) {
  call_to_db_that_takes_a_long_time(function (data) {
    assert.equal(data, 'foo');
  });
});

because tests are serialized specify can catch uncaught exceptions and continue to run. you will get a report about the error that was throw somewhere in your stack. this is analogous to the functionality the community refers to as domains.

specify is standalone, you don't need any special binaries to run it.

if you think all these specify functions make your code look bloated you can also run a single function:

var specify = require('specify')
  , request = require('request')
  ;

specify.run(

  function (assert) {

    var get = { uri: "http://someservice.com/1/apps/dscape", json: true }
      , del = { uri: "http://someservice.com/1/apps/dscape", method: "DELETE"
              , json : true }
      , app_name
      ;

    request(get, function (err, _, body) {

      assert.equal(err,null);
      assert.ok(body.rows);
      assert.ok(body.total_rows >= 1);
      assert.ok(body.rows.length >= 1);

      app_name = body.rows[0].value.app_name;
      del.uri += "/" + app_name;

      request(del, function (err, resp, body) {

        assert.equal(err,null);
        assert.equal(resp.statusCode, 200);
        assert.equal(body.app.name, app_name);
        assert.equal(body.app.user,"dscape");

      });

    });

  }

);
# installation ## node.js
  1. install npm
  2. npm install specify
  3. var specify = require('specify');
# filtering

in specify you specify which tests you want to run:

var specify = require('specify')
  , filters = process.argv.slice(2)
  ;

specify('foo', function (assert) {
  assert.equal('foo', 'foo');
});

specify('bar', function (assert) {
  assert.equal('bar', 'baz', 'bar failed');
});

specify('baz', function (assert) {
  assert.equal('baz', 'baz');
});

specify.run(filters);
# reporters

if you feel like the output sent to stdout is ugly you can write your own reporter and send in a pull request.

now use it:

specify('specify#ask_for_a_specific_reporter', function(assert) {
  specify.reporter('my_awesome_reporter');
  setTimeout(function (){
    assert.ok(true);
  },1);
});

you can also do this with a function if you like:

specify('specify#custom_reporter_from_function', function(assert) {
  specify.reporter(function (name, report, errors) {
    console.log(name);
  });
  setTimeout(function () {
    assert.ok(false, 'i see dead people');
    assert.ok(true);
  },1);
});
# samples

samples are available in the /test folder

# contribute

everyone is welcome to contribute. patches, bug-fixes, reporters, new features.

  1. create an issue so the community can comment on your idea
  2. fork specify
  3. create a new branch git checkout -b feature_name
  4. create tests for the changes you made
  5. make sure you pass both existing and newly inserted tests
  6. commit your changes
  7. push to your branch git push origin feature_name
  8. create an pull request
# meta

(oO)--',- in caos

# license

copyright 2012 nuno job <nunojob.com> (oO)--',--

licensed under the apache license, version 2.0 (the "license"); you may not use this file except in compliance with the license. you may obtain a copy of the license at

http://www.apache.org/licenses/LICENSE-2.0

unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "as is" basis, without warranties or conditions of any kind, either express or implied. see the license for the specific language governing permissions and limitations under the license

specify's People

Contributors

dscape avatar

Watchers

Kevin Ingersoll avatar James Cloos 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.