Skip to content

Custom Chai assertions, mainly filesystem-related. 🍋

License

Notifications You must be signed in to change notification settings

Alhadis/Chinotto

Repository files navigation

Chinotto: Custom assertions for Chai.js

Build status: TravisCI Build status: AppVeyor Coverage status Latest release

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

  1. Add chinotto to your project's devDependencies:

    $ npm install --save-dev chinotto
  2. 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 calls register() for you automatically, and makes Chinotto 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

Extension list

Exports

  • 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:

Methods

Check if an HTMLElement contains one or more CSS classes.

ParameterTypeAttributesDescription
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.

ParameterType
target String

Example:

"/bin".should.equalPath("/bin/");
"/bin/../bin".should.equalPath("/bin");

Assert that two files have the same inode and device number.

ParameterType
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.

ParameterType
target String

Example:

"/tmp".should.be.a.symlink.pointingTo("/private/tmp");

Properties

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

Utils

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.

ParameterType
names String, Array.<String>
fn function

Example:

addMethod(["pointTo", "pointingTo"], function(target){  });

Variant of chai.Assertion.addProperty that supports plugin aliases.

ParameterType
names String, Array.<String>
fn function

Example:

addProperty(["coloured", "colored"], fn);

Variant of defineAssertions that defines only one assertion.

ParameterType
names String, Array.<String>
handler function

Wrapper for defining simple custom Chai assertions.

ParameterType
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();