Giter Club home page Giter Club logo

describe's Introduction

describe

Describe provides a simple method for testing asynchronous and synchronous code within JavaScript projects.

API Documentation

describe

describe( groupName, tests[, options] );

Arguments

  • groupName (string): A human-readable description of the test group.
  • tests (object): An object made up of human-readable test descriptions as keys and functions to run as tests. Tests functions will be given access to this.expect.
  • options: Configuration options. Possible values:
    • timeout (int): The max amount of time in milliseconds to wait for a test to run before timing out.
    • callbackMode (string): if set to 'node', this.expect will treat the first argument to the callback as an error and the second argument as the result.

describe.config

describe.config( key, value )

Sets the global configuration for tests.

Synchronous Assertions

this.expect( subject, expected )

Arguments

  • subject (mixed): the actual result.
  • expected (mixed): the expected result.

Example

describe("assertions", {
	'basic synchronous expectation': function() {
		this.expect(42, 42);
	}
});

Asynchronous Assertions

By passing this.expect as the callback parameter to an asynchronous function, describe will know to wait for the result of the operation before checking to see if the result matches what was expected.

this.expect( expected )

Basic Callbacks

function addNumbersAsync(a, b, callback) {
	callback(a+b);
}

describe("assertions", {
	'basic asynchronous expectation': function() {
		addNumbersAsync(2, 2, this.expect(4));
	}
});

Node.js-style (e, data) Callbacks

function addNumbersAsync(a, b, callback) {
	callback(null, a+b);
}

describe("assertions", {
	'basic asynchronous expectation': function() {
		addNumbersAsync(2, 2, this.expect(4));
	}
}, { callbackMode: 'node' });

Promises-style Callbacks

function addThingsPromise() {
	var n = 0;
	for (var i in arguments) n+=arguments[i];
	return {
		then: function(success, failure) {
			success(n);
		}
	};
}

describe("promise callback style", {
	'promises-style addition': function() {
		this.expect(addThingsPromise(2, 2), 4);
	}
}, {
	callbackMode: 'promises'
});

describe.getResults

An asynchronous method. Calls back with the results of all tests described up to that point. You should probably wait until you're done defining tests to call this.

Example Results

{ 
	passed: 1,
	total: 2,
	results: {
		"sample test group": {
			passed: 1,
			total: 2,
			results: {
				"this test passed because its error is null": null,
				"this test failed because there's an error": "Error or message"
			}
		}
	}
}

describe.logResults

Gets the results and outputs them either to the DOM or the console.

describe.new

Create a new independant instance of describe.

Test Hooks

Each test group supports beforeEach, afterEach, beforeAll, and afterAll as test hooks.

Example

(function() {

	var arr = [], bowties;

	describe('array stuff', {
		beforeAll: function() {
			bowties = 'cool';
		},
		beforeEach: function() {
			arr = arr.concat(1,2,3);
		},
		afterEach: function() {
			arr = [];
		},
		afterAll: function() {
			tests = null;
		},
		'bowties are cool': function() {
			this.expect(bowties, 'cool');
		},
		'arrays have three things': function() {
			this.expect(arr.length, 3);
			arr.push(5);
		},
		'arrays still have three things': function() {
			this.expect(arr.length, 3);
		}
	});

}());

describe's People

Contributors

mdamien avatar yuffster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

describe's Issues

Using describe in browser ...

Hello Michelle,

I very like your tiny describe testing module.

When I use it in browser, line 25 gives an error, since require is not defined. You should guard it by

if (isModule)
    require('string-color');

I inline coded the coloring as a proposal. So describenow does not depend on other modules.
See my tests how I did it.

If you like I would start a pull request.

Thanx
..
Stefan

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.