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>




