1.0.12 • Published 11 months ago

chai-extra-functionblock v1.0.12

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

How to use this library

  1. run npm install chai-extra-functionblock
  2. In your test file -
  • import 'chai-extra-functionblock'
  1. write your test using the added custom methods and properties to chai

Chai Arrow Function Assertion

The arrowFunction property allows you to check if a given function is an Arrow Function.

Usage:

const myArrowFunction = () => {
  return 'Hello, world!';
};

const myFunctionExpression = function() {
  return 'Hello, world!';
};

expect(myArrowFunction).to.be.an.arrowFunction;
expect(myFunctionExpression).to.not.be.arrowFunction;

Chai Function Expression Assertion

The functionExpression property allows you to check if a given function is a Function Expression.

Usage:

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:

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 Property

The regularForLoop property allows you to check if a given function uses a regular for loop.

Usage:

const 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 Property

The forOfLoop property allows you to check if a given function uses a for of loop.

Usage:

const 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 Property

The forInLoop property allows you to check if a given function uses a for in loop.

Usage:

const 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 Property

The whileLoop property allows you to check if a given function uses a while loop.

Usage:

const 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 Method

The 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:

const 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 Method

The keyword property allows you to check if a given function uses any keyword in your function code.

Usage:

const 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 Method

The 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
    1. offsetLineNumber - if needed give it a number to offset the Line Number if it is displayed incorrectly

Usage:

const 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 Method

The method method allows the user test if a method is being used in a function.

Usage:

const 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 Method

The 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.

const 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 Method

The 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:

const 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]);
1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.11

11 months ago

1.0.10

12 months ago

1.0.12

11 months ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago