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

chai-extra-functionblock

v1.0.12

Published

Testing function block code

Downloads

16

Readme

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