Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alhadis/chinotto

Custom Chai assertions, mainly filesystem-related. :lemon:
https://github.com/alhadis/chinotto

assertions bdd chai chai-assertions chai-plugin chaijs javascript

Last synced: 23 days ago
JSON representation

Custom Chai assertions, mainly filesystem-related. :lemon:

Awesome Lists containing this project

README

        

Chinotto: Custom assertions for [Chai.js](https://www.chaijs.com)
=================================================================

[![Build status: TravisCI][TravisCI-badge]][TravisCI-link]
[![Build status: AppVeyor][AppVeyor-badge]][AppVeyor-link]
[![Coverage status][Coverage-badge]][Coverage-link]
[![Latest release][NPM-badge]][NPM-link]

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`:
~~~console
$ npm install --save-dev chinotto
~~~

2. Then call the [`register()`][] function it exports:
~~~js
require("chinotto").register();
~~~

This automatically registers every available extension with Chai,
which are used like any other [BDD-style assertion](https://chaijs.com/api/bdd/):

~~~js
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:

~~~js
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;
~~~

[`atom-mocha`]: https://github.com/Alhadis/Atom-Mocha/blob/dec4a46c/docs/extensions.md
[`Object`]: https://mdn.io/Object.prototype
[`Function`]: https://mdn.io/Function
[`Map`]: https://mdn.io/Map
[`chai`]: https://npmjs.com/package/chai
[`register()`]: #register
[AppVeyor-badge]: https://ci.appveyor.com/api/projects/status/6cx2pnqvgc8g50q0?svg=true
[AppVeyor-link]: https://ci.appveyor.com/project/Alhadis/Chinotto
[TravisCI-badge]: https://travis-ci.org/Alhadis/Chinotto.svg?branch=master
[TravisCI-link]: https://travis-ci.org/Alhadis/Chinotto
[Coverage-badge]: https://img.shields.io/coveralls/Alhadis/Chinotto.svg
[Coverage-link]: https://coveralls.io/github/Alhadis/Chinotto?branch=master
[NPM-badge]: https://img.shields.io/npm/v/chinotto.svg?colorB=brightgreen
[NPM-link]: https://github.com/Alhadis/chinotto/releases/latest

API reference
-------------

Extension list

* [Methods](#methods)
* [`class()`](#class)
* [`equalPath()`](#equal-path)
* [`hardLink()`](#hard-link)
* [`pointTo()`](#point-to)
* [Properties](#properties)
* [`.blockDevice`](#block-device)
* [`.characterDevice`](#character-device)
* [`.device`](#device)
* [`.directory`](#directory)
* [`.door`](#door)
* [`.drawn`](#drawn)
* [`.existOnDisk`](#exist-on-disk)
* [`.fifo`](#fifo)
* [`.file`](#file)
* [`.focus`](#focus)
* [`.socket`](#socket)
* [`.symlink`](#symlink)

Exports

* `chai`: Object — Reference to the Chai module used by Chinotto.
* `methods`: [`Map`](https://mdn.io/Map) — Handler functions for assertion methods, keyed by name(s)
* `properties`: [`Map`](https://mdn.io/Map) — Handler functions for assertion properties, keyed by name(s)

The remaining exports are detailed under [Utils](#utils):
* [`addMethod()`](#add-method)
* [`addProperty()`](#add-property)
* [`defineAssertion()`](#define-assertion)
* [`defineAssertions()`](#define-assertions)
* [`register()`](#register)

Methods


class()/classes()

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:**
~~~js
document.body.should.have.class("content");
expect($(".btn.large")).to.have.classes("btn", "large");
~~~

equalPath()

Assert that two filesystem paths are logically the same.

ParameterType

target
String

**Example:**
~~~js
"/bin".should.equalPath("/bin/");
"/bin/../bin".should.equalPath("/bin");
~~~


hardLink()/hardLinkOf()

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

ParameterType

target
String

**Example:**
~~~js
"/a/huge/file".should.have.hardLink("/same/huge/file");
expect("huge.file").to.be.hardLinkOf("also.huge");
~~~


pointTo()/pointingTo()

Assert that a symbolic link points to the specified file.

ParameterType

target
String

**Example:**
~~~js
"/tmp".should.be.a.symlink.pointingTo("/private/tmp");
~~~

Properties

.drawn

Assert that an HTMLElement is rendered in the DOM tree.

**Example:**
~~~js
document.body.should.be.drawn;
document.head.should.not.be.drawn;
~~~

.focus

Assert that an HTMLElement has user focus, or contains something which does.

**Example:**
~~~js
document.activeElement.should.have.focus;
document.createElement("div").should.not.have.focus;
~~~


.existOnDisk/.existsOnDisk

Assert that a file exists in the filesystem.

**Example:**
~~~js
"/bin/sh".should.existOnDisk
"<>:*?\0".should.not.existOnDisk
~~~


.file/.regularFile

Assert that subject is a path pointing to a regular file.

**Example:**
~~~js
"/bin/sh".should.be.a.file
"/bin".should.not.be.a.file
~~~

.directory

Assert that subject is a path pointing to a directory.

**Example:**
~~~js
"/bin".should.be.a.directory
"/bin/sh".should.not.be.a.directory
~~~


.symlink/.symbolicLink

Assert that subject is a path pointing to a symbolic link.

**Example:**
~~~js
"/usr/local/bin/node".should.be.a.symlink
~~~


.device/.deviceFile

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:**
~~~js
"/dev/zero".should.be.a.device;
~~~

.blockDevice

Assert that subject is a path pointing to a block device.

**Example:**
~~~js
"/dev/disk0s1".should.be.a.blockDevice
~~~


.characterDevice/.charDevice

Assert that subject is a path pointing to a character device.

**Example:**
~~~js
"/dev/null".should.be.a.characterDevice
~~~


.fifo/.namedPipe

Assert that subject is a path pointing to a FIFO (named pipe).

**Example:**
~~~js
"/tmp/154B17E1-2BF7_IN".should.be.a.fifo
~~~

.door

Assert that subject is a path pointing to a door.

**Example:**
~~~js
"/system/volatile/syslog_door".should.be.a.door
~~~

.socket

Assert that subject is a path pointing to a socket.

**Example:**
~~~js
"/run/systemd/private".should.be.a.socket
~~~

Utils

addMethod()

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:**
~~~js
addMethod(["pointTo", "pointingTo"], function(target){ … });
~~~

addProperty()

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

ParameterType

names
String, Array.<String>

fn
function

**Example:**
~~~js
addProperty(["coloured", "colored"], fn);
~~~

defineAssertion()

Variant of defineAssertions that defines only one assertion.

ParameterType

names
String, Array.<String>

handler
function

defineAssertions()

Wrapper for defining simple custom Chai assertions.

ParameterType

spec
Object

**Example:**
~~~js
Defining a "colour" assertion
// 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");

Shorthand for the above
defineAssertions({
["colour, coloured"](subject, expected){
return [
subject.colour === expected,
"to be coloured #{exp}",
];
},
});
~~~

register()

Register every available Chai extension.

**Example:**
~~~js
import Chinotto from "./lib/index.mjs";
Chinotto.register();
~~~