Sassaby
A Unit Testing Library for SASS
Sassaby is a unit testing library for SASS mixins and functions. It is written in Node to provide a consistent interface with other front-end tests in your system and for easy integration into a build system. Its interface can be used with any Node testing library (Mocha, Jasmine, etc.).
Installation
Install via npm:
npm install --save-dev sassaby
Setup
Setting up Sassaby is simple with easy integration into your existing Javascript testing library. After installation, simply require it at the top of the file and instantiate a new object with the .sass or .scss file that you want to include. Here is a sample file using Mocha.
var path = ;var Sassaby = ; ;
Note that the Sassaby constructor takes the absolute path to the SASS file. We recommend using Node's path
and __dirname
(which gives you the directory of the test file) plus the remaining path here. Also, note that this file must ONLY include SASS function and mixin declarations. Any code that compiles to CSS in this file will cause Sassaby's parsers to give inconsistent results.
Dependencies
We recommend testing SASS files in isolation. However, depending on the setup of your SASS import tree some functions and mixins may rely on externally declared variables, mixins, or functions. In this case, you can pass an options object to the Sassaby constructor with variables
and/or dependencies
defined. Here is the sample file with these options:
var path = ;var Sassaby = ; ;
variables
should be an object with string keys. It will declare each key-value pair as a SASS variable before compiling the given function/mixin.
dependencies
should be an array of file paths to be imported into the compiled SASS. We recommend using the same approach (with path
and __dirname
) that is used with setting the file path.
Features
Sassaby breaks down testable features into four categories:
- Functions
- Standalone Mixins
- Included Mixins
Functions are your typical SASS functions, defined like this:
Standalone Mixins are mixins that define new rules, like this:
Included Mixins are mixins that do not define new rules, just declarations that should be placed into existing rules. For example:
Each of these categories can be defined by a respective function defined on an instance of Sassaby
. For example, you can set up each of them with this syntax:
var sassaby = filePath;var testFunction = sassaby;var testStandaloneMixin = sassaby;var testIncludedMixin = sassaby;
These functions will read the given file (with variables and dependencies if given) and return an object that will takes one of the "called" functions documented below. SASS compilation will occur at this step.
calledWithArgs
Calls the mixin or function with the given arguments.
sassaby;sassaby;sassaby
called
Calls the mixin or function with no arguments.
sassaby;sassaby;sassaby;
calledWithBlock
Calls the mixin with a block. This is only available on mixins and the block is to be given as a string without wrapping brackets.
sassaby;sassaby
calledWithBlockAndArgs
Calls the mixin with a block and arguments. This is only available on mixins and the block is to be given as a string without wrapping brackets. The block is always the first argument of this function and the mixin arguments will follow.
sassaby;sassaby
Rules
Each of these types has their own set of functions, or rules, that assert certain conditions on the result of the function or mixin. The arguments of these rules are normalized to match the output from the SASS compilation, so it can be formatted however you wish as long as it is compilable SASS.
Function Rules
equals
Asserts that the function output equals a certain value.
sassaby;
doesNotEqual
Assert that the function output does not equal a certain value.
sassaby;
isTrue
Assert that the function output equals true.
sassaby;
isFalse
Assert that the function output equals false.
sassaby;
isTruthy
Assert that the function output is a truthy value in SASS. Keep in mind that this is SASS truthy, not Javascript truthy.
sassaby;
isFalsy
Assert that the function output is a falsy value in SASS. Keep in mind that this is SASS truthy, not Javascript truthy.
sassaby;
Standalone Mixin Rules
createsSelector
Assert that the mixin creates the given selector.
sassaby;
doesNotCreateSelector
Assert that the mixin does not create the given selector.
sassaby;
createsMediaQuery
Assert that the mixin creates a media query with the given string.
sassaby;
doesNotCreateMediaQuery
Assert that the mixin does not create a media query with the given string.
sassaby;
createsFontFace
Assert that the mixin creates a font-face rule.
sassaby;
doesNotCreateFontFace
Assert that the mixin does not create a font-face rule.
sassaby;
hasNumDeclarations
Assert that the mixin creates the given number of declarations.
sassaby;
declares
Assert that the mixin makes a declaration of the given rule-property pair.
sassaby;
doesNotDeclare
Assert that the mixin does not make a declaration of the given rule-property pair.
sassaby;
equals
Assert that the mixin output equals the given string.
sassaby;
doesNotEqual
Assert that the mixin output does not equal the given string.
sassaby;
calls
Assert that the mixin calls another mixin.
sassaby;
doesNotCall
Assert that the mixin does not call another mixin.
sassaby;
Included Mixin Rules
hasNumDeclarations
Assert that the mixin creates the given number of declarations.
sassaby;
declares
Assert that the mixin makes a declaration of the given rule-property pair.
sassaby;
doesNotDeclare
Assert that the mixin does not make a declaration of the given rule-property pair.
sassaby;
equals
Assert that the mixin output equals the given string.
sassaby;
doesNotEqual
Assert that the mixin output does not equal the given string.
sassaby;
calls
Assert that the mixin calls another mixin.
sassaby;
doesNotCall
Assert that the mixin does not call another mixin.
sassaby;
Testing Imports
Often your SASS project will have a single entry point from where all other files are imported. Sassaby exposes two assertion methods on the sassaby object itself to test this. These two methods take the same path that would be included in the @import
statement in your SASS file.
imports
Assert that the file imports the given path.
sassaby;
doesNotImport
Assert that the file does not import the given path.
sassaby;
Contributing
Pull requests are welcome. If you add functionality, then please add unit tests to cover it. Continuous Integration is handled by Travis.
License
MIT © Ryan Bahniuk