Chinotto: Custom assertions for Chai.js
Compilation of useful Chai assertions that I've written over time,
migrated from the test-suites of various projects (atom-mocha
in particular).
Because if Chai and Mocha proved anything, it's that beverages make memorable library names. Also, I enjoy Chinotto and the name on NPM was available, so I took it. Superalo.
-
Add
chinottoto your project'sdevDependencies:$ npm install --save-dev chinotto -
Then call the
register()function it exports:require("chinotto").register();
This automatically registers every available extension with Chai, which are used like any other BDD-style assertion:
expect(__filename).to.be.a.file.and.to.existOnDisk; __dirname.should.be.a.file.and.equalPath(__dirname + "/");
Alternatively, you can just import
chinotto/registerinstead. This callsregister()for you automatically, and makesChinottoglobally available:require("chinotto/register"); // Define a hypothetical extension to check modification status: global.Chinotto.defineAssertion("modified", (subject, expected) => [subject.isModified(), "to be modified"]); // Usage: expect("/some/edited/file").to.be.modified; expect("/unedited/file").not.to.be.modified;
chai:Object— Reference to the Chai module used by Chinotto.methods:Map— Handler functions for assertion methods, keyed by name(s)properties:Map— Handler functions for assertion properties, keyed by name(s)
The remaining exports are detailed under Utils:
Check if an HTMLElement contains one or more CSS classes.
| Parameter | Type | Attributes | Description |
|---|---|---|---|
expected |
String, Array.<String> |
Variadic | An array or whitespace-delimited list of CSS class-names |
Example:
document.body.should.have.class("content");
expect($(".btn.large")).to.have.classes("btn", "large");Assert that two filesystem paths are logically the same.
| Parameter | Type |
|---|---|
target |
String |
Example:
"/bin".should.equalPath("/bin/");
"/bin/../bin".should.equalPath("/bin");Assert that two files have the same inode and device number.
| Parameter | Type |
|---|---|
target |
String |
Example:
"/a/huge/file".should.have.hardLink("/same/huge/file");
expect("huge.file").to.be.hardLinkOf("also.huge");Assert that a symbolic link points to the specified file.
| Parameter | Type |
|---|---|
target |
String |
Example:
"/tmp".should.be.a.symlink.pointingTo("/private/tmp");Assert that an HTMLElement is rendered in the DOM tree.
Example:
document.body.should.be.drawn;
document.head.should.not.be.drawn;Assert that an HTMLElement has user focus, or contains something which does.
Example:
document.activeElement.should.have.focus;
document.createElement("div").should.not.have.focus;Assert that a file exists in the filesystem.
Example:
"/bin/sh".should.existOnDisk
"<>:*?\0".should.not.existOnDiskAssert that subject is a path pointing to a regular file.
Example:
"/bin/sh".should.be.a.file
"/bin".should.not.be.a.fileAssert that subject is a path pointing to a directory.
Example:
"/bin".should.be.a.directory
"/bin/sh".should.not.be.a.directoryAssert that subject is a path pointing to a symbolic link.
Example:
"/usr/local/bin/node".should.be.a.symlinkAssert that subject is a path pointing to a device file.
“Device file” refers to either a character device or a block device, making
this assertion preferable to blockDevice and characterDevice
for cross-platform testing.
Example:
"/dev/zero".should.be.a.device;Assert that subject is a path pointing to a block device.
Example:
"/dev/disk0s1".should.be.a.blockDeviceAssert that subject is a path pointing to a character device.
Example:
"/dev/null".should.be.a.characterDeviceAssert that subject is a path pointing to a FIFO (named pipe).
Example:
"/tmp/154B17E1-2BF7_IN".should.be.a.fifoAssert that subject is a path pointing to a door.
Example:
"/system/volatile/syslog_door".should.be.a.doorAssert that subject is a path pointing to a socket.
Example:
"/run/systemd/private".should.be.a.socketVariant of chai.Assertion.addMethod that supports plugin aliases.
If the property already exists on the prototype, it will not be overwritten.
To redefine existing methods and prototypes, use chai.util.addMethod
or chai.util.overwriteMethod.
| Parameter | Type |
|---|---|
names |
String, Array.<String> |
fn |
function |
Example:
addMethod(["pointTo", "pointingTo"], function(target){ … });Variant of chai.Assertion.addProperty that supports plugin aliases.
| Parameter | Type |
|---|---|
names |
String, Array.<String> |
fn |
function |
Example:
addProperty(["coloured", "colored"], fn);Variant of defineAssertions that defines only one assertion.
| Parameter | Type |
|---|---|
names |
String, Array.<String> |
handler |
function |
Wrapper for defining simple custom Chai assertions.
| Parameter | Type |
|---|---|
spec |
Object |
Example:
<caption>Defining a "colour" assertion</caption>
// Typical definition:
defineAssertions({
["colour, coloured"](subject, expected){
const actual = subject.colour;
this.assert(
actual === expected,
"expected #{this} to be coloured #{exp}",
"expected #{this} not to be coloured #{exp}",
expected,
actual
);
},
});
// Usage:
expect({colour: 0xFF0000}).to.have.colour(0xFF0000);
expect({colour: "red"}).not.to.be.coloured("green");
<caption>Shorthand for the above</caption>
defineAssertions({
["colour, coloured"](subject, expected){
return [
subject.colour === expected,
"to be coloured #{exp}",
];
},
});Register every available Chai extension.
Example:
import Chinotto from "./lib/index.mjs";
Chinotto.register();