npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

testing-code-js

v0.0.5

Published

Node.js module to create Test Classes and run it

Downloads

10

Readme

TestingCode

It is a javascript library to support unity tests. It was made like that way to improve flexibility to create and run tests. It, firstly, run at node.js that has a portable version. Link to portable node.js: http://imsky.co/files/nodejs-0.10.0.zip Once that perform the download, it is enough put the directory where the files was extracted in enviroment variable PATH of operational system.

How to use

Lets start with that basic example.

(function () {
  
  var StringType = typeof '', NumberType = typeof 1, UndefinedType = typeof undefined, ObjectType = typeof {},
  	TestingCode = require('./TestingCode'), Math = {
			sum: function(op1, op2) {
				if(typeof op1!==NumberType)
					throw new Error('The first operator is not a number');
				if(typeof op2!==NumberType)
					throw new Error('The second operator is not a number');
				return op1 + op2;
			}
		};
	
	var MathTests = TestingCode.createTestClass({
		settings:{
			name:'MathTests'
		},
		tests: {
			when_sum_1_with_1_must_has_2_as_result:function(context){
				context.asserts.assertEquals([2, Math.sum(1,1)], 'The sum value of 1 + 1 was not as expected.');
			},
			when_sum_2_with_2_must_has_4_as_result:function(context){
				context.asserts.assertEquals([4, Math.sum(2,2)], 'The sum value of 2 + 2 was not as expected.');
			},
			when_sum_1_with_4_must_has_5_as_result:function(context){
				context.asserts.assertEquals([5, Math.sum(1)], 'The sum value of 1 + 4 was not as expected.');
			}
		}
	});
	var math = new MathTests();
	TestingCode.run([math]);
  
})();

Through of TestingCode class it is possible makes test classes and run it. In the case above, we have a class Math that contains a function "sum", which must be tested.

To create a test class

The test class may be created through the calling TestingCode.createTestClass(options);, where options can be an object like that:

var options = {
  settings:{
		name:'TestingClass', //Name of test class to show in a console output 
		setup:function(context){}, //Function that will be executed once by class at first
		finalize:function(context){}, //Function that will be executed once by class at end
		beforeEveryTest:function(context){}, //Function that will be executed once by test method at first
		afterEveryTest:function(context){} //Function that will be executed once by test method at end
	},
	args:{
    //Some argument that will be used by test methods
    //May be accessed by context, like context.args.variable_name_here
  },
	asserts:{
		puts:function(message, methodName, className){
			message+='\n['+className;
			if (typeof methodName!==null&&typeof methodName!==UndefinedType)
				message+='.'+methodName;
			message+=']';
			TestingCode.write(message,'\n\n');
		},
		handleException:function(e){
			if (e instanceof TestingCode.asserts.AssertFailException){
				e.handleException();
			}else{
				this.fail(e.message);
				throw new TestingCode.asserts.AssertFailException(e.message, TestingCode.asserts.AssertFailLevel.ErrorUncatched);
			}
		}
	},
  tests:{
    //Test methods must be putted here
  }
}; 

So, lets improve the old example. Below, we are going to execute 2 test classes and the last one has an implementation of setup function to initialize the argument 'operator1'. Also, it will put 2 sentences in console, one when perform setup and the other when perform finalize.

(function () {
  
  var StringType = typeof '', NumberType = typeof 1, UndefinedType = typeof undefined, ObjectType = typeof {},
  	TestingCode = require('./TestingCode'), Math = {
			sum: function(op1, op2) {
				if(typeof op1!==NumberType)
					throw new Error('The first operator is not a number');
				if(typeof op2!==NumberType)
					throw new Error('The second operator is not a number');
				return op1 + op2;
			}
		};
	
	var math2, MathTests2, math, MathTests = TestingCode.createTestClass({
		settings:{
			name:'MathTests'
		},
		tests: {
			when_sum_1_with_1_must_has_2_as_result:function(context){
				context.asserts.assertEquals([2, Math.sum(1,1)], 'The sum value of 1 + 1 was not as expected.');
			},
			when_sum_2_with_2_must_has_4_as_result:function(context){
				context.asserts.assertEquals([4, Math.sum(2,2)], 'The sum value of 2 + 2 was not as expected.');
			},
			when_sum_1_with_4_must_has_5_as_result:function(context){
				context.asserts.assertEquals([5, Math.sum(1)], 'The sum value of 1 + 4 was not as expected.');
			}
		}
	});
	math = new MathTests();
	
	MathTests2 = TestingCode.createTestClass({
		settings:{
			name:'MathTests2',
			setup:function(context){ console.log('Hello World First\n\n'); context.args.operator1 = 2; }, //Function that will be executed once by class at first
			finalize:function(context){ console.log('Hello World Second\n\n'); } //Function that will be executed once by class at end
		},
		args:{
		  //Some argument that will be used by test methods
		  //May be accessed by context, like context.args.variable_name_here
		  operator1:0
		},
		tests: {
			when_sum_2_with_1_must_has_3_as_result:function(context){
				context.asserts.assertEquals([3, Math.sum(context.args.operator1,1)], 'The sum value of 2 + 1 was not as expected.');
			},
			when_sum_2_with_2_must_has_4_as_result:function(context){
				context.asserts.assertEquals([4, Math.sum(context.args.operator1,2)], 'The sum value of 2 + 2 was not as expected.');
			},
			when_sum_2_with_4_must_has_6_as_result:function(context){
				context.asserts.assertEquals([6, Math.sum(context.args.operator1)], 'The sum value of 2 + 4 was not as expected.');
			}
		}
	});
	math2 = new MathTests2();
	
	TestingCode.run([math, math2]);
  
})();

So, that is it! Easy like that.