Coder Social home page Coder Social logo

xray-css's Introduction

X-Ray CSS

Installation

npm install --save-dev xray-css

Purpose

CSS code, like JavaScript code, changes all the time. Many attempts at verifying that changes don't cause unwanted effects are brittle. We can unit test CSS visually at a component level though to great effect.

How

We want to unit test CSS visually. Let's break that last statement down:

unit test CSS visually

Unit testing code using small, controlled test cases can be very effective compared to full integration tests. Integration tests suffer from false positives when someone tweaks some text. Unit tests on the other hand keep their environment controlled and won't newly break if your home page changes. For example, say you want to test your buttons:

button.css:

button {
  background-color: blue;
  border-radius: 3px;
  color: white;
  min-width: 50rem;
  max-width: 200rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

button[disabled] {
  background-color: gray;
  color: lightgray;
}

We start by simply enumerating the cases we care about (short text, long text, disabled).

button.html:

<link href="button.css" rel="stylesheet"/>

<example title="a simple button">
  <button>Short</button>
</example>

<example title="long text">
  <button>Some really long text that could happen, you know?</button>
</example>

<example title="disabled">
  <button disabled>Disabled</button>
</example>

Now, when we run xray-css for the first time, it'll generate an expected screenshot of just this unit test. If you were to run it again, you'd expect the screenshot to stay the same. Now if someone were to come along and change the min-width of a button, xray-css would fail to compare correctly against the last screenshot. If someone were to change a button on your home page though, this unit test would not be affected: your buttons are likely still all-ok CSS-wise.

In this way of unit testing CSS, we can avoid false positives in unreleated changes in how our CSS is used.

unit test CSS visually

Using a pre- or post-processor? That's cool (if not, you should be). Since xray-css only cares about the end result and rendering a unit test page, it's agnostic to your current toolchain. To help you with your processing though, it knows how to generate the resulting CSS for inclusion into the test page.

unit test CSS visually

Some concepts of unit testing CSS try to use the expected computed CSS values as their expected result. We've noticed this also can generate false positives when someone changes both padding and margin. Generally, we only care about the output, which we consider to be the visual look of the page. Since we're doing screenshot diffing, tweaking related values that would result in identical visuals is fine.

Tactics

CSS can cover a lot of states. Here are a few ways to deal with them all.

:hover

:active

transitions

https://css-tricks.com/controlling-css-animations-transitions-javascript/

animations

animation-play-state: paused

https://css-tricks.com/controlling-css-animations-transitions-javascript/

xray-css's People

Contributors

visnup avatar

Watchers

Nolan Evans avatar  avatar Ian Wong avatar James Cloos avatar Kevin Teh avatar Sarah avatar  avatar

xray-css's Issues

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.