Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom test without node using pure javascript #76

Open
xxMrPHDxx opened this issue Feb 15, 2018 · 2 comments
Open

Custom test without node using pure javascript #76

xxMrPHDxx opened this issue Feb 15, 2018 · 2 comments

Comments

@xxMrPHDxx
Copy link

xxMrPHDxx commented Feb 15, 2018

Hey, I just tried to create my own test function which kind of similar to the Jest library.
Have been testing for 5 statements below and yet to find any failure.
But for now, it only have 2 functional tests which is expect(value).toBe(other) and expect(value).toEqual(something).

Let me see what do you guys think about this kind of stuff. Is it worth it?

Below is the implementation of the tests

function check(current,other){
	if(typeof other !== 'object' && typeof current !== 'object') return current === other;
	let equal = false;
	for(let prop in other){
		if(current[prop] === undefined) throw new Error("FAILED");
		equal = equal || check(current[prop],other[prop]);
	}
	if(!equal) throw new Error("FAILED");
	return equal;
}


function test(comment,callback){
	let error;
	try{
		//execute the tests;
		callback();
	}catch(e){
		// Get the result whether it passed or failed 
		error = e.message;
	}
	console.log(comment,error === "FAILED" ? "FAILED" : "PASSED");
}

function expect(value){
	this.toBe = function(other){
		if(value !== other)	throw new Error("FAILED");
	}
	this.toEqual = function(something){
		check(value,something);
	}
	return this;
}

// The tests start HERE

test('Is 2+2 be 4?',() => {
	expect(2 + 2).toBe(4);						// returns : Is 2+2 be 4? PASSED
});

test('Is 2+3 be 4?',() => {
	expect(2 + 3).toBe(4);						// returns : Is 2+3 be 4? FAILED
});

test('Is {value:3*3} equal {value:9}?',() => {
	expect({value:3*3}).toEqual({value:9});        			// returns : Is {value:3*3} equal {value:9}? PASSED
});

test('Is {value:2*3} equal {value:9}?',() => {
	expect({value:2*3}).toEqual({value:9});       			// returns : Is {value:2*3} equal {value:9}? FAILED
});

test('Is {value:{arr:[6]}} equal {value:9}?',() => {
	expect({value:{arr:[6]}}).toEqual({value:9});    		// Is {value:{arr:[6]}} equal {value:9}? FAILED
});
@Adil-Iqbal
Copy link

That's pretty amazing dude. Maybe not the best direction for the toy nn library but its one heck of an achievement.

I was wondering how jest was doing what it was.

@xxMrPHDxx
Copy link
Author

xxMrPHDxx commented Feb 15, 2018

Well actually after looking at it for a few times, it seems that the check function missing something.

I loop into other's property and check them all but forgot to check current's property.

So if I do this

test('Is {value:9,arr:[6]} equal {value:9}?',() => {
	expect({value:9,arr:[6]}).toEqual({value:9});
});

it will be PASSED because arr property was not being checked.

This should fix it

function check(current,other){
	if(typeof other !== 'object' && typeof current !== 'object') return current === other;
	let equal = false;
	const already = [];
	for(let prop in other){
		if(current[prop] === undefined) throw new Error("FAILED");
		equal = equal || check(current[prop],other[prop]);
		already.push(prop);
	}
	for(let prop in current){
		if(already.includes(prop)) continue;
		if(other[prop] === undefined) throw new Error("FAILED");
		equal = equal || check(current[prop],other[prop]);
	}
	if(!equal) throw new Error("FAILED");
	return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants