Testing function block code
npm install chai-extra-functionblocknpm install chai-extra-functionblockimport 'chai-extra-functionblock'Arrow Function AssertionarrowFunction property allows you to check if a given function is an Arrow Function.``javascript
const myArrowFunction = () => {
return 'Hello, world!';
};
const myFunctionExpression = function() {
return 'Hello, world!';
};
expect(myArrowFunction).to.be.an.arrowFunction;
expect(myFunctionExpression).to.not.be.arrowFunction;
`
Assertion property allows you to check if a given function is a Function Expression.Usage:
`javascript
const myFunctionExpression = function() {
return 'Hello, world!';
};const myArrowFunction = () => {
return 'Hello, world!';
};
expect(myFunctionExpression).to.be.a.functionExpression;
expect(myArrowFunction).to.not.be.functionExpression;
`Chai
Function Declaration Assertion
The functionDeclaration property allows you to check if a given function is a Function Declaration.Usage:
`javascript
function myFunctionDeclaration() {
return 'Hello, world!';
}const myFunctionExpression = function() {
return 'Hello, world!';
};
expect(myFunctionDeclaration).to.be.a.functionDeclaration;
expect(myFunctionExpression).to.not.be.functionDeclaration;
`Chai
RegularForLoop PropertyThe
regularForLoop property allows you to check if a given function uses a regular for loop.Usage:
`javascriptconst fn = function() {
for (let i = 0; i < 10; i++) {
// Loop body
}
}
expect(fn).to.have.regularForLoop;
const fn = function() {
while (true) {
// Loop body
}
};
expect(fn).to.not.have.regularForLoop;
`Chai
ForOfLoop PropertyThe
forOfLoop property allows you to check if a given function uses a for of loop.Usage:
`javascriptconst fn = function(arr) {
for (var ele of arr) {
// Loop body
}
}
expect(fn).to.have.forOfLoop;
const fn = function() {
while (true) {
// Loop body
}
};
expect(fn).to.not.have.forOfLoop;
`Chai
ForInLoop PropertyThe
forInLoop property allows you to check if a given function uses a for in loop.Usage:
`javascriptconst fn = function(obj) {
for (var key in obj) {
// Loop body
}
}
expect(fn).to.have.forInLoop;
const fn = function() {
while (true) {
// Loop body
}
};
expect(fn).to.not.have.forInLoop;
`Chai
WhileLoop PropertyThe
whileLoop property allows you to check if a given function uses a while loop.Usage:
`javascriptconst fn = function() {
while (true) {
// Loop body
}
}
expect(fn).to.have.whileLoop;
const fn = function() {
for (var key in obj) {
// Loop body
}
};
expect(fn).to.not.have.whileLoop;
`Chai
Operator MethodThe
operator property allows you to check if a given function uses a any operators in you function code.Note
=is not considered an operator when used in Variable Declaration.Usage:
`javascriptconst fn = function() {
let arr = ['a', 'b','c', 'd'];
let index = 0;
for(var i = 0; i < arr.length; i++) {
if(arr[i] === 'c' && typeof arr[i] === 'string') {
index = i;
}
}
return index
}
expect(fn).to.have.operator("<");
expect(fn).to.not.have.operator(">");
expect(fn).to.have.operator("===");
expect(fn).to.have.operator("=");
expect(fn).to.have.operator("typeof");
`Chai
keyword MethodThe
keyword property allows you to check if a given function uses any keyword in your function code.
Usage:
`javascriptconst fn = function() {
let arr = ['a', 'b','c', 'd'];
let index = 0;
for(let i = 0; i < arr.length; i++) {
if(arr[i] === 'c' && typeof arr[i] === 'string') {
index = i;
}
}
return index
}
expect(fn).to.have.keyword("let");
expect(fn).to.not.have.keyword("var");
expect(fn).to.have.keyword("if");
`Chai
throwJavaScriptError MethodThe
throwJavaScriptError method allows the user to see a better Type or Reference Error Message. It will by default show for example:-
- Type of Error - ReferenceError
- Error Message - vals is not defined
- Function Name - fn1
- Line Number - 162:14 The method takes in two optional arguments -
1. array of strings - pass in one or more of these:- (The order of display will depend on the order of passed in strings)
-
type - this will display the Type of Error message
- line - this will display the Line Number
- message - this will display the Error Message
- functionName - this will display the Function Name
2. offsetLineNumber - if needed give it a number to offset the Line Number if it is displayed incorrectlyUsage:
`javascriptconst fn1 = function(obj, keys) {
let val = obj[keys[0]];
return val
}
expect(() => fn1({name: "Jack"}, ['name'])).to.not.throwJavaScriptError();
expect(() => fn1({name: "Jack"}, ['name'])).to.not.throwJavaScriptError(["functionName", "line"]);
`Chai
Check Method Use MethodThe
method method allows the user test if a method is being used in a function.Usage:
`javascriptconst fn1 = function(obj) {
let values = Object.values()
}
const fn2 = function(arr) {
var result = [];
for(var i = 0; i < arr.length; i++) {
result.push(arr[i]);
}
return result;
};
// pass the method name as a string
expect(fn1).to.have.method('values');
// pass the method name as a string
expect(fn2).to.not.have.method('slice');
`Chai
Ordered Console logs MethodThe
callOrderedConsoleLogsWith method allows the user to test if log/s are displayed in required Order.Usage:
The method takes in two arguments -
- array of arrays or just an array of expected logs- expected logs to be logged by student;
- (optional) array of arguments as will be passed to the tested function. if no argument - then ignore.
Note:- The method ignores extra logs by student and only checks the expected logs.
`javascriptconst fn1 = function() {
for(var i = 0; i < 4; i++) {
console.log("Jack", JSON.stringify({a: 1}))
}
};
expect(fn1).to.callOrderedConsoleLogsWith([[ "Jack" , {a: 1}],
[ "Jack" , {b: 1}],
[ "Jack" , {a: 1}],
[ "Jack" , {a: 1}]]);
expect(fn1).to.callOrderedConsoleLogsWith([[ "Jack" , {a: 1}, "Jack" , {b: 1}, "Jack" , {b: 1}, "Jack" , {b: 1}]]);
// When function takes in arguments.
let obj = {a:1, b:2};
let key = ['a', 'b']
const fn2 = function(o, k) {
console.log(o[k[0]]);
console.log(o[k[1]])
};
expect(fn2).to.callUnorderedConsoleLogsWith([[1, 2]], [obj, key]);
`
Chai
Unordered Console logs MethodThe
callUnorderedConsoleLogsWith method allows the user to test if log/s are displayed when order of logs don't matter. The method takes in two arguments -
- array of arrays - expected logs to be logged by student;
- (optional) array of arguments as will be passed to the tested function. if no argument - then not need to pass anything.
Note:- The method ignores extra logs by student and only checks the expected logs
Usage:
`javascriptconst fn1 = function() {
console.log("jack", "jill");
console.log(JSON.stringify({a:1}));
};
expect(fn1).to.callUnorderedConsoleLogsWith([
[{a: 1}],
['jack', 'jill']
]);
expect(fn1).to.callUnorderedConsoleLogsWith([
[{a: 1}, 'jack', 'jill']
]);
// When function takes in arguments.
let obj = {a:1};
let key = ['a']
const fn2 = function(o, k) {
console.log(o[k[0]]);
};
expect(fn2).to.callUnorderedConsoleLogsWith([[1]], [obj, key]);
``