New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@thetimes/jest-lint

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thetimes/jest-lint

A tool to analyse/validate jest snapshots

  • 1.0.1
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
2
Weekly downloads
 
Created
Source

jest-lint

A linter for Jest snapshots to help you write (in a very opinionated way) "better" snapshots

Install

Install globally or locally

yarn @thetimes/jest-lint --dev

Run

Just run jest-lint in any directory where snap files may lurk for a report

The Code

The linter essentially has four stages for each discovered snap file:

Parse => Analyse => Report => Output

Parse

In this phase it takes the Jest snapshot and turns it into a JavaScript Object

Analyse

Using predefined rules it builds up a list of errors and warnings, the thresholds of which can be optionally overridden. These errors and warnings exist at both the snapshot file level itself as well as for each test within the file

To configure the rules create a .jestlint file with your overrides e.g.

{
  "maxFileSize": 5000,
  "genericAttributes": [
    "randomProp",
    "fixedProp"
  ],
  "genericValues": [
    "[Function]"
  ]
}

Report

The report phase is split out from the analysis to enable the previous step to be performed and written to disk (which is relatively fixed), allowing you to iterate on different criteria to get the right balance for your codebase

Output

Currently the only output is via the console with a litany of red, yellow and green writing

File Rules

MaxFileSizeError

config option: maxFileSize: number

default: 10,000

Choose the maximum file size for your snapshots, large snapshot files are hard to diff and/or view on GitHub

Test Rules

GenericAttributeError

config option: genericAttributes: string[]

default: []

There are many props, for example in FlatList that are just noise on a snapshot rather than providing value to the component under test. Use this to weed out props that aren't of use in a snapshot

GenericValueError

config option: genericValues: string[]

default: ["Function"]

There are many values such as [Function] that don't provide real value in a snapshot which can be excluded without detriment to the test, thus removing more noise

MaxAttributeError

config option: maxAttributes: number

default: 5

If an element has too many attributes then it's unlikely everyone is vital to the component under test and will end up contributing to changes not related to the test when things change

MaxAttrLengthError

config option: maxAttributeLength: number

default: 30

If a prop has a value that is very long it is unlikely the whole value is of interest to the test and can be shortened for easier diffing

MaxDepthError

config option: maxDepth: number

default: 10

Deep nesting of elements is likely a sign of trying to test leaves that should have their own tests without the rest of the snapshot. It is also likely that they're external dependencies that will break many snapshots when they change but not related to the test

MaxLinesError

config option: maxLines: number

default: 300

Perhaps the strongest barometer of an unspecific snapshot test is it's length. A long snapshot is hard to read, hard to diff and is likely to change between PRs not related to the tests

MaxGenericElementWarning

config option: maxGenericElements: number

default: 10

If a snapshot is filled with primitives with no props it is likely that either there are too many of them in general or the test isn't specific enough. For example 15 div elements suggest a lot of "div noise" rather than a specific render test

Keywords

FAQs

Package last updated on 05 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc