Table of contents
1.
Introduction
2.
Type-Detect
2.1.
Installation
2.1.1.
NodeJs
2.1.2.
Deno
2.1.3.
Browsers
3.
Methods
3.1.
Array 
3.2.
Function 
3.3.
Date 
3.4.
Regexp 
3.5.
Arguments 
3.6.
Number 
3.7.
String 
3.8.
Undefined
3.9.
Null 
3.10.
Object
3.11.
ECMA6 Types
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Chai Type-Detect

Author Aditya Anand
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! In this blog, we will learn about the Type-Detect in Chai.Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.


As a tester, we always want to test if a piece of code is returning the required entity, etc. The typeof operator can be one way to test, but it will only specify primitive values. It will not test objects like null, arrays, regexps, etc. As an alternative, many developers use Object.prototype.toString(), which can be used to test many more types like null, Arrays, regexps, etc. Sadly, Object.prototype.toString is slower than typeOf. Also, it is very buggy, and its return value depends on the browser. So, values like a global object, iterator, dataviews, Promises, HTML elements all report different things in different browsers.

These all shortcomings with Object.prototype.toString are fixed by type-detect. It also provides extra code to speed up JS and DOM objects checks. Type-Detect also fixes any consistencies with these objects.

Let's get started, and I hope you learn a lot from this tutorial.

Type-Detect

Type Detect is a module that you can use to detect the type of a given object. It returns a string representation of the object's type, using typeof or @@toStringTag. It also normalises some object names for consistency among browsers.

The primary export of type-detect is a function that can serve as a replacement for typeof. The results of this function will be more specific than that of the native typeof.

Installation

We can install type-detect in NodeJs, Deno, Browser.

NodeJs

type-detect is available on npm. Type the below npm command to install it.

npm install type-detect

Deno

To import type-detect use the following line of code:

import type from 'https://deno.land/x/type_detect@v4.1.0/index.ts'

Browsers

To use it within the browser, install type-detect via npm and use the type-detect.js file. For example:

<script src="./node_modules/type-detect/type-detect.js"></script>

Methods

We can start using type-detect by simply requiring it using the below line of code.

var type = require('type-detect');

As already mentioned, type-detect can be used to assert different entities quickly and effectively. Let’s see some examples.

Array 

assert(type([]) === 'Array');
assert(type(new Array()) === 'Array');

Function 

assert(type(function () {}) === 'function');

Date 

assert(type(new Date) === 'Date');

Regexp 

assert(type(/a-z/gi) === 'RegExp');
assert(type(new RegExp('a-z')) === 'RegExp');

Arguments 

(function () {
    assert(type(arguments) === 'Arguments');
 })();

Number 

assert(type(3) === 'number');
assert(type(3.14) === 'number');
assert(type(-25) === 'number');
assert(type(-5.64) === 'number');
assert(type(Infinity) === 'number');
assert(type(NaN) === 'number');
assert(type(new Number(48)) === 'Number');

String 

assert(type('welcome to Coding Ninjas ') === 'string');
assert(type(new String('Coding is Fun!')) === 'String');

Undefined

assert(type(undefined) === 'undefined');
assert(type(null) !== 'undefined');

Null 

assert(type(null) === 'null');
assert(type(undefined) !== 'null');

Object

var Noop = function () {};
assert(type({}) === 'Object');
assert(type(Noop) !== 'Object');
assert(type(new Noop) === 'Object');
assert(type(new Object) === 'Object');

ECMA6 Types

assert(type(new Map() === 'Map');
assert(type(new WeakMap()) === 'WeakMap');
assert(type(new Set()) === 'Set');
assert(type(new WeakSet()) === 'WeakSet');
assert(type(Symbol()) === 'symbol');
assert(type(new Promise(callback) === 'Promise');
assert(type(new Int8Array()) === 'Int8Array');
assert(type(new Uint8Array()) === 'Uint8Array');
assert(type(new UInt8ClampedArray()) === 'Uint8ClampedArray');
assert(type(new Int16Array()) === 'Int16Array');
assert(type(new Uint16Array()) === 'Uint16Array');
assert(type(new Int32Array()) === 'Int32Array');
assert(type(new UInt32Array()) === 'Uint32Array');
assert(type(new Float32Array()) === 'Float32Array');
assert(type(new Float64Array()) === 'Float64Array');
assert(type(new ArrayBuffer()) === 'ArrayBuffer');
assert(type(new DataView(arrayBuffer)) === 'DataView');

FAQs

1. What is the difference between Mocha and Chai?
Ans: Mocha is a widely used JavaScript test framework for Node.js. Chai is a BDD / TDD assertion library for Node.js and the browser that can be paired with any JavaScript testing framework.
 

2. What is the assertion with Chai?
Ans: Assertion with Chai provides natural language assertions, expressive and readable style. The expect interface provides a function for assertion.
 

3. What is the use of the @Chai assertion library?
Ans: Chai assertion library is an external javascript library used to write assertions. Compared to what we write directly in javascript, this assertion library needs less time & effort and is easy to use.
 

4. How to test a function in Chai with JavaScript?

Ans: You have to import the validator.js file to our test file in order to access the function being tested.

Key Takeaways

In this article, we have extensively discussed Type-Detect in Chai.

We have learned:

  • Need of type-detect in Chai.
  • Shortcomings of typeOf and Object.prototype.toString() and how type-detect overcomes these shortcomings.
  • Use type-detect to assert functions, string, number, null, objects, arguments, date, ECMA6 Types, etc.

 

We hope that this blog has helped you enhance your knowledge regarding Chai Type-Detect and if you would like to learn more, check out our articles on Running Mocha tests in browsers Mocha Assertions. Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

Live masterclass