chinotto
v1.0.0
Published
Custom Chai assertions, mainly filesystem-related.
Downloads
12
Maintainers
Readme
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).
“Uh, why did you name thi-”
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.
Usage
Add
chinotto
to 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/register
instead. This callsregister()
for you automatically, and makesChinotto
globally 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;
API reference
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.
Example:
document.body.should.have.class("content");
expect($(".btn.large")).to.have.classes("btn", "large");
Assert that two filesystem paths are logically the same.
Example:
"/bin".should.equalPath("/bin/");
"/bin/../bin".should.equalPath("/bin");
Assert that two files have the same inode and device number.
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.
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.existOnDisk
Assert that subject is a path pointing to a regular file.
Example:
"/bin/sh".should.be.a.file
"/bin".should.not.be.a.file
Assert that subject is a path pointing to a directory.
Example:
"/bin".should.be.a.directory
"/bin/sh".should.not.be.a.directory
Assert that subject is a path pointing to a symbolic link.
Example:
"/usr/local/bin/node".should.be.a.symlink
Assert 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.blockDevice
Assert that subject is a path pointing to a character device.
Example:
"/dev/null".should.be.a.characterDevice
Assert that subject is a path pointing to a FIFO (named pipe).
Example:
"/tmp/154B17E1-2BF7_IN".should.be.a.fifo
Assert that subject is a path pointing to a door.
Example:
"/system/volatile/syslog_door".should.be.a.door
Assert that subject is a path pointing to a socket.
Example:
"/run/systemd/private".should.be.a.socket
Variant 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.
Example:
addMethod(["pointTo", "pointingTo"], function(target){ … });
Variant of chai.Assertion.addProperty that supports plugin aliases.
Example:
addProperty(["coloured", "colored"], fn);
Variant of defineAssertions that defines only one assertion.
Wrapper for defining simple custom Chai assertions.
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();