npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sassy-test

v5.0.1

Published

A simple helper utility for creating unit tests of Sass modules. Works great with mocha or jasmine.

Downloads

69

Readme

Coverage Status

Sassy Test

Sassy Test is a simple helper utility for creating unit tests of Sass modules.

Sassy Test models its testing after the unit tests in LibSass. The tests are a series of sub-folders in the "test/fixtures" directory that contain an "input" Sass file and an "output" CSS file. Its unit tests then reference a particular folder, compile the input.scss and compare the results to the output.css file.

To get started, just install Sassy Test as a development dependency of your Sass module with: npm install --save-dev sassy-test

Sassy Test will work with any Node.js test runner, like mocha or jasmine.

A quick demo of Mocha + Sassy Test

Example project's root directory:

|   # You can put your module's Sass files anywhere.
|   # We use "sass" as an example.
├─┬ sass/
│ └── _mymodule.scss
│   # Mocha prefers your tests to live in a "test" folder.
│   # Sassy Test will automatically find your fixtures if
│   # they are in /test/fixtures, but you can change the
│   # path with configurePaths().
└─┬ test/
  ├─┬ fixtures/
  │ │   # Test fixtures can be deeply nested.
  │ ├─┬ my-modules-function/
  │ │ ├── input.scss
  │ │ └── output.css
  │ ├─┬ my-modules-error/
  │ │ ├── input.scss
  │ │ └── output.css
  │ └─┬ my-modules-warn/
  │   ├── input.scss
  │   └── output.css
  └── test_mymodule.scss

Then in our test file, test/test_mymodule.js, we can use sassyTest to simplify our tests:

import path from 'node:path';
import { expect } from 'chai';
import SassyTest from 'sassy-test';

const sassyTest = new SassyTest({
  // Path to the Sass module we are testing and its dependencies.
  loadPaths: [
    path.join(__dirname, '../sass'),
    path.join(__dirname, '../node_modules/breakpoint-sass/stylesheets')
  ]
  // Since our fixtures are in test/fixtures, we don't need to override
  // the default value by setting the "fixtures" path here.
  // fixtures: path.join(__dirname, 'fixtures'),
});

describe('@import "mymodule";', function() {
  describe('@function my-modules-function()', function() {
    it('should test an aspect of this function', async function() {
      // Sassy Test's compileFixture() will run a comparison test between the
      // compiled input.scss and the output.css found in the fixtures
      // sub-directory specified in its first parameter, in this case:
      // test/fixtures/my-modules-function
      return sassyTest.compileFixture('my-modules-function')
        .catch(error => {
          // If we expect the comparison test to succeed, we just need to test
          // that no error occurred.
          expect(error).to.not.exist;
        })
        .then(result => {
          // No additional assertions are needed, but we can run other tests
          // here if we desire.
        });
    });

    it('should throw an error in this situation', async function() {
      // Sassy Test's compileFixture() can also test if your module produces an
      // intentional error with Sass' @error directive.
      return sassyTest.compileFixture('my-modules-error')
        .catch(error => {
          // If the Sass in test/fixtures/my-modules-error/input.scss triggers an
          // @error in your module, you should expect the error object to exist
          // and to contain the error message from your module.
          expect(error).to.exist;
          expect(error.message).to.equal('Some helpful error message from your module.');
        });
    });

    it('should warn in another situation', async function() {
      // Sassy Test's compileFixture() can also test if your module produces an
      // intentional warning message with Sass' @warn directive.
      return sassyTest.compileFixture('my-modules-warn')
        .catch(error => {
          // If the Sass in test/fixtures/my-modules-warn/input.scss triggers a
          // @warn in your module, you should expect the result object to exist
          // and to contain the warn message from your module.
          expect(error).to.not.exist;
        })
        .then(result => {
          // Sassy Test adds two new arrays to sass' result object:
          // result.warn and result.debug are arrays of strings.
          expect(result.warn[0]).to.equal('Some helpful warning from your module.');
        });
    });
  });
});

For more information about configuring a SassyTest object, see the configurePaths() method documentation.

JavaScript Promises

SassyTest's compile(), compileString() and compileFixture() methods return a Promise.

describe('@import "mymodule";', function() {
  describe('@function my-modules-function()', function() {
    it('should test an aspect of this function', function() {
      // Mocha accepts sassyTest's returned Promise.
      return sassyTest.compileFixture('my-modules-function');
    });

    it('should throw an error in this situation', function() {
      return sassyTest.compileFixture('my-modules-error').then(function(result) {
        // If the expected Sass error does not occur, we need to fail the test.
        expect(result).to.not.exist('An error should have occurred');
      }).catch(function(error) {
        expect(error).to.exist;
        expect(error.message).to.include('Some helpful error message from your module.');
      });
    });

    it('should warn in another situation', function() {
      return sassyTest.compileFixture('my-modules-warn').then(function(result) {
        expect(result.warn[0]).to.equal('Some helpful warning from your module.');
      });
    });
  });
});

Full documentation of Sassy Test’s JavaScript API is available online.

Development

Forking, hacking, and tearing apart of this software is welcome! It's still very simple and could use additional features and conveniences.

After you've cloned this repository, run npm install and then you'll be able to run the module's mocha and eslint tests with npm test.

Contributors

None but me yet. All are welcome! https://github.com/JohnAlbin/sassy-test/graphs/contributors