GithubHelp home page GithubHelp logo

routablehq / jest-scss-transform Goto Github PK

View Code? Open in Web Editor NEW
10.0 35.0 6.0 42 KB

A jest transformer for SCSS file exports

License: MIT License

JavaScript 90.74% CSS 0.09% SCSS 9.17%
jest javascript scss transform

jest-scss-transform's Introduction

jest-scss-transform

Branches coverage Functions coverage Lines coverage Statements coverage

Jest transformer for .scss files.

Useful for when you're sharing variables between Sass and JavaScript, and makes your snapshots much nicer.

Install

npm i -D jest-scss-transform

or

yarn add -D jest-scss-transform

Add to your Jest config

Using jest.config.js:

module.exports = {
  // A map from regular expressions to paths to transformers
  transform: {
    "^.+\\.scss$": 'jest-scss-transform',
  },
};

Similarly, using "jest" key in package.json (create-react-app, etc):

{
  "jest": {
    "transform": {
      "^.+\\.scss$": "<rootDir>/node_modules/jest-scss-transform"
     }
  }
}

Example

Assume you have an scss file, e.g. colors.scss--

$brandPrimary: #000000;
$brandSecondary: #ffffff;

:export {
  brandPrimary: $brandPrimary;
  brandSecondary: $brandSecondary;
}

And you're importing those variables for use in your js app, e.g. colors.js--

import colors from 'colors.scss';

export const brandPrimary = colors.brandPrimary;
export const brandSecondary = colors.brandSecondary;

Webpack will compile these nicely, but Jest will not. Most configurations will result in undefined or empty string values for each of those constants.

Using this package, they'll be read-in as the string literal value of the scss variables. For example--

import * as colors from 'colors.js';

console.log(colors.brandPrimary); // $brandPrimary
console.log(colors.brandSecondary); // $brandSecondary

This makes for really nice snapshot tests, where instead of being written as hex codes (#04ae46), undefined, etc, style properties are written as their semantic scss variable names ($brandPrimary).

See the example directory for a demo of some basic snapshot output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.