async-test

0.1.1 • Public • Published

ATest

ATest is a small framework to test asynchronous code, intended to be used to test node.js code. ATest can be used for unit tests as well as integration tests.

Examples

Simple test

var ATest = require('ATest');

// Define the tests
var tests = [
	{
		name: 'first',
		test: function(test) {
			test.assertEqual(5, '5');
			test.assertNotEqualStrict(5, '5');
			
			test.complete(); // Required to inform the suite the test has finished successfuly.
		}
	},
	{
		name: 'second',
		test: function(test) {
			test.assertTrue(false); // Meant to fail.
			
			test.complete();
		}
	}
];

// Create a suite for the tests.
var suite = new ATest(tests);

// Add a listener to the 'done' event that reports the amount of failed and passed tests.
suite.on('done', function(results) {
	var passed = 0, failed = 0;
	results.forEach(function(element) {
		if(element.isSuccess) {
			passed ++;
		} else {
			failed ++;
		}
	});
	console.log(passed + ' passed, ' + failed + ' failed, ' + results.length + ' total.');
});

// Run the tests!
suite.start();

Test with events

var ATest = require('ATest');
var http = require('http')

// The test
var tests = [
	{
		// A test 
		name: 'http.get',
		test: function(test) {
			var options = { host:'www.github.com', port: 81, path: '/'}; // incorrect port, so will fail.
			var req = http.get(options, function(res) {
				res.on('end', function() {
					test.run(function() { // Since the events come from the Node.js event loop, test code needs to be encapsulated in test.run.
						test.complete();
					})
				})
				
				res.on('data', function(data) {
					console.log('data.length=' + data.length);
				})
			});
			
			req.on('error', function(e) {
				test.run(function() {
					test.fail('An error occurred when sending the request.', req, {options: options, error: e});
				})
			})
			req.end();
		}
	}
];

// Create a suite for the test.
var suite = new ATest(tests);
suite.on('done', function(results) {
	console.log('----  testing done  ----');
	
	// Process results.
	results.forEach(function(element) {
		if(element.isSuccess) {
			console.log('> ' + element.name + ' passed.');
		} else {
			// Show a tiny report for the error.
			var out = '> ' + element.name + ' FAILED!' + "\n";
			out += 'Reason: ' + element.failure.message + "\n";
			if(element.failure.info) {
				out += 'info: ' + JSON.stringify(element.failure.info, null, 4) + "\n";
			}
			console.log(out);
		}
	});
});

// Start test.
console.log('---- starting tests ----');
suite.start();

License

The MIT License (MIT)

Copyright (c) 2015 Lars Bokkers

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

Package Sidebar

Install

npm i async-test

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • lbokkers