chai-as-typed
v0.2.1
Published
Chai assertions for types defined via JSDoc
Downloads
50
Maintainers
Readme
chai-as-typed
Motivation
TypeScript has now the ability to do type checking in plain JavaScript files. This behavior is not based on magic, but on well formed JSDoc comments.
Now, if I have a JavaScript file augmented with the proper JSDoc comments, an editor such as Visual Studio Code will be able to automatically get IntelliSense, which includes autocompletion and type checking:
However, exposing reliably the type information about a module APIs requires testing. This plugin enhances Chai with specific helpers that make the task of checking the type information in plain JavaScript files easy.
How to use
Considering that we have a javascript file, which starts with the following line:
// @ts-check
we can test that the types used are correct with the following assertion:
expect('/absolute/path/to/filename.js').to.have.types.validated();
If there are type errors, an error will be thrown indicating the position of the type violations:
Error: /absolute/path/to/filename.js (Ln 5, Col 6): Argument of type '123' is not assignable to parameter of type 'string'
Also, in case errors are expected, we can test using the following assertions indicating the number of errors expected:
expect('/absolute/path/to/filename.js').to.have.types.errors(1);
Installation ans setup
Do npm install --save-dev chai-as-typed
, then:
var chai = require('chai');
var chaiAsTyped = require('chai-as-typed');
chai.use(chaiAsTyped);
Disclaimer
The plugin is in experimental phase. Use it gently and provide feedback! :)