Class: module:sassy-test

module:sassy-test(configopt)

A SassyTest object represents test helper for testing sass modules.

This class is normally accessed via the sassy-test module:

import SassyTest from 'sassy-test';
const sassyTest = new SassyTest();

Constructor

new module:sassy-test(configopt)

Creates a SassyTest object.

If the optional initialization object is given to the constructor, it will be passed to the configurePaths() method.

For example, this:

import SassyTest from 'sassy-test';
const sassyTest = new SassyTest({loadPaths: ['/my/path/to/library']});

is equivalent to:

import SassyTest from 'sassy-test';
const sassyTest = new SassyTest();
sassyTest.configurePaths({loadPaths: ['/my/path/to/library']});
Parameters:
Name Type Attributes Description
config object <optional>

Optional initialization object.

Source:

Methods

assertResult(result)

Runs assertions against compileFixture()'s result object.

The compileFixture() automatically calls this method to run a standard set of assertions against the result object before it is returned. If no Sass error occurs, assertResult() checks for an error when reading the output.css file using assert.ifError() and compares the results to the expected output using assert.strictEqual().

If the SassyTest user chooses, this method can be overridden to perform different assertions.

Parameters:
Name Type Description
result object

The result object returned by compileFixture().

Source:

compile(path, optionsnullable) → {Promise}

Runs sass' compile() with a light-weight wrapper.

In addition to running sass' compile(), this method:

  • adds the test fixtures path directory to the loadPaths
  • ensures the loadPaths are passed to sass

Sassy-test also adds the following properties to the sass result object:

  • warn: An array containing the output of any @warn statements.
  • debug: An array containing the output of any @debug statements.
import SassyTest from 'sassy-test';
const sassyTest = new SassyTest();

describe('a test suite', function() {
  it('should test something', function(done) {
    const path = 'fixtures/_init.scss';
    sassyTest.compile(path, options, function(error, result) {
      assert.ifError(error);
      assert.ok(result.css);
      done();
    });
  });
});
Parameters:
Name Type Attributes Description
path string

The path to the file to be compiled.

options object <nullable>

The options to pass to sass' compile(). For the full list of options, see the sass documentation for "options".

Source:
Returns:

A Promise that resolves to sass' result object or rejects to sass' exception object.

Type
Promise

compileFixture(fixtureDirectory, options) → {Promise}

Compiles the test fixture and returns the result.

Looks inside the specified folder in test/fixtures, compiles the input.scss file and reads the output.css file. Before it returns the sass result object, it calls assertResult() to run a standard set of assertions.

compileFixture() does not test for errors itself; it requires the caller to decide if a Sass error is a test failure or not. Good Sass libraries should @error if used incorrectly and sassy-test lets you see these errors and assert they were the expected result.

Sassy-test also adds the following properties to the sass result object:

  • warn: An array containing the output of any @warn statements.
  • debug: An array containing the output of any @debug statements.
  • sassError: A sass error object which contains @error statements, if any.
  • expectedOutput: The text of the output.css file; should match the css property provided by sass.
import SassyTest from 'sassy-test';
const sassyTest = new SassyTest();

describe('a test suite', function() {
  it('should test something', async function() {
    sassyTest.compileFixture('sometest', options, function(error, result) {
      // If there was no error, compileFixture() has already compared
      // the compiled output of fixtures/sometest/input.scss to
      // fixtures/sometest/output.css.
      assert.ifError(error);
      done();
    });
  });
});
Parameters:
Name Type Description
fixtureDirectory string

The path (relative to the fixtures base directory) to the fixture to test.

options object

The options to pass to sass' compile(). For the full list of options, see the sass documentation for "options".

Source:
Returns:

A Promise that resolves to sass' result object or rejects to sass' exception object.

Type
Promise

compileString(string, optionsnullable) → {Promise}

Runs sass' compileString() with a light-weight wrapper.

In addition to running sass' compileString(), this method:

  • adds the test fixtures path directory to the loadPaths
  • ensures the loadPaths are passed to sass

Sassy-test also adds the following properties to the sass result object:

  • warn: An array containing the output of any @warn statements.
  • debug: An array containing the output of any @debug statements.
import SassyTest from 'sassy-test';
const sassyTest = new SassyTest();

describe('a test suite', function() {
  it('should test something', async function() {
    let result;
    try {
      result = sassyTest.compileString(
        '@import "init"; // Imports fixtures/_init.scss.'
        options
      );
    } catch (error) {
      assert.ifError(error);
    }
    assert.ok(result.css);
  });
});
Parameters:
Name Type Attributes Description
string string

The path to the file to be compiled.

options object <nullable>

The options to pass to sass' compile(). For the full list of options, see the sass documentation for "options".

Source:
Returns:

A Promise that resolves to sass' result object or rejects to sass' exception object.

Type
Promise

configurePaths(config)

Configures the paths needed for the sassyTest object.

import SassyTest from 'sassy-test';
const sassyTest = new SassyTest();
sassyTest.configurePaths({
  fixtures: '/my/path/to/fixtures',
  loadPaths: ['/my/path/to/library']
});

If sassy-test is installed in node_modules and your test fixtures are in ./test/fixtures (relative to the root of your project), then sassy-test will automatically configure the fixtures path without you having to set fixtures with configurePaths().

Parameters:
Name Type Description
config object

A configuration object containing the properties: fixtures and loadPaths.

Source:

fixture(…path) → {string}

Returns the full path to the requested test fixture.

When called without any parameters, this method returns the path to the test fixtures directory. If one or more parameters are given, the method will append them to the returned path.

import SassyTest from 'sassy-test';
const sassyTest = new SassyTest();

// Returns full path to the test fixtures.
const fixturePath = sassyTest.fixture();
// Returns full path to [fixtures]/sub-folder.
const fixturePath = sassyTest.fixture('sub-folder');
// Returns full path to [fixtures]/sub-folder/_file.scss.
const fixturePath = sassyTest.fixture('sub-folder', '_file.scss');
Parameters:
Name Type Attributes Description
path string <repeatable>

Optional paths inside the fixtures directory.

Source:
Returns:

The path to the requested test fixture.

Type
string