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

jest-styled-components-stylelint

v0.7.0

Published

A helper for running stylelint on your styled-components styles at runtime.

Downloads

19

Readme

jest-styled-components-stylelint

A helper for running stylelint on your styled-components styles at runtime.

That means it lints the styles after all your dynamic interpolations are resolved! So it doesn’t get tripped up or need annotations, and will more accurately reflect the styles you’re actually shipping.

Screenshot

Usage

In a Jest setup file like setupTestFramework.js:

import configure from 'jest-styled-components-stylelint'

// NOTE: This should be configured before `styled-components` and `stylis` are
// imported anywhere!
configure({ failOnError: true })

Or simply:

require('jest-styled-components-stylelint')()

Then in your tests, just make sure something renders your components:

import TestRenderer from 'react-test-renderer'

test('renders successfully', () => {
  const wrapper = TestRenderer.create(<Button />)
  expect(wrapper.toJSON()).toMatchSnapshot()
})

Any stylelint errors will cause the test to fail – or if failOnError is false, they will simply be logged.

Options

failOnError

Whether there should be an automatic assertion at the end of every test (added via afterEach) that asserts there were no stylelint errors when running the test.

If true, stylelint errors will cause the test to fail. The failure message will include the formatted lint errors.

If false, stylelint errors will be logged to stderr but the test won’t fail.

Default: true

formatterOptions

By default, this package uses its own custom stylelint formatter (seen in the screenshot above). It shows the styles inline so they’re easier to find and fix. You can tune some aspects of the formatter:

  • collapseLines: Whether each line of code with warnings will be printed just once, or multiple times (once for each warning). Defaults to true.

See below if you’d like to change the stylelint formatter completely.

More…

All remaining options are passed along to stylelint’s lint() function.

You can use this to pass a custom formatter (or any of the defaults from stylelint, like string).

Troubleshooting

It’s not doing anything!

If you’re using this with jest-styled-components, make sure to import and configure() this module first, before importing jest-styled-components. Otherwise, the necessary modules aren’t mocked in time.


There are a lot of errors, but my code looks fine.

Are they spacing errors, like declaration-block-semicolon-space-after?

Expected single space after ";" in a single-line declaration block

If so, this is because babel-plugin-styled-components ships with the minify option enabled by default, so your styles come pre-minified.

You can try disabling this in your test environment by modifying your Babel configuration:

plugins: [
  ['styled-components', { ssr: true, minify: process.env.NODE_ENV !== 'test' }]
]

(As a last resort, you could disable the stylelint rules in question.)


I’m including some third-party CSS in my template strings and I don’t care about linting it.

You can try putting normal stylelint comment directives around it, they should work just fine:

/* stylelint-disable */
${someExternalCSS}
/* stylelint-enable */