All Projects → nicoespeon → jest-extended-snapshot

nicoespeon / jest-extended-snapshot

Licence: MIT license
Additional Jest matchers for snapshot testing.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to jest-extended-snapshot

snappify
A generator of Jest snapshot-based tests for React components written with TypeScript 📸
Stars: ✭ 64 (+255.56%)
Mutual labels:  jest, jest-snapshots
jest-playground
Playing around with Jest - Subscribe to my YouTube channel: https://bit.ly/CognitiveSurge
Stars: ✭ 24 (+33.33%)
Mutual labels:  jest, jest-snapshots
Jest Dom
🦉 Custom jest matchers to test the state of the DOM
Stars: ✭ 2,908 (+16055.56%)
Mutual labels:  jest, jest-matchers
angularjs-jest
Testing AngularJS applications with Jest
Stars: ✭ 24 (+33.33%)
Mutual labels:  jest, jest-snapshots
testing-reactjs-examples
🧪 "What should we test in our React components" - presentation examples.
Stars: ✭ 23 (+27.78%)
Mutual labels:  jest, jest-snapshots
nuxt-boilerplate
Nuxt.js Boilerplate
Stars: ✭ 25 (+38.89%)
Mutual labels:  jest, jest-snapshots
Jest Image Snapshot
✨ Jest matcher for image comparisons. Most commonly used for visual regression testing.
Stars: ✭ 2,940 (+16233.33%)
Mutual labels:  jest, jest-snapshots
coconat
🍥 StarterKit Builder for rocket-speed App creation on 🚀 React 17 + 📙 Redux 4 + 🚠 Router 5 + 📪 Webpack 5 + 🎳 Babel 7 + 📜 TypeScript 4 + 🚔 Linters 23 + 🔥 HMR 3
Stars: ✭ 95 (+427.78%)
Mutual labels:  jest
gobarber-api-gostack11
API GoBarber / NodeJS / Express / Typescript / SOLID
Stars: ✭ 39 (+116.67%)
Mutual labels:  jest
typescript-boilerplate
A modern TypeScript project setup, for Node.js and browsers (using esbuild).
Stars: ✭ 502 (+2688.89%)
Mutual labels:  jest
react-initial-bootstrap
an "almost complete" template for your new projects
Stars: ✭ 24 (+33.33%)
Mutual labels:  jest
Express-REST-API-Template
Minimal starter project for a Node.js RESTful API based off express generator
Stars: ✭ 26 (+44.44%)
Mutual labels:  jest
node-js-starter-kit
This is the starter kit project for node js REST API development with express js, mongodb, typescript, webpack specially designed for REST API projects.
Stars: ✭ 14 (-22.22%)
Mutual labels:  jest
clean-architecture-api-boilerplate
A boilerplate for creating TypeScript APIs following the Clean Architecture principles.
Stars: ✭ 116 (+544.44%)
Mutual labels:  jest
Splain
small parser to create more interesting language/sentences
Stars: ✭ 15 (-16.67%)
Mutual labels:  jest
fly-helper
It's a Tool library, method collection
Stars: ✭ 21 (+16.67%)
Mutual labels:  jest
jest-snapshot-talk
React Conf 2017: Jest, Snapshots and Beyond
Stars: ✭ 48 (+166.67%)
Mutual labels:  jest
nimbus
Centralized CLI for JavaScript and TypeScript developer tools.
Stars: ✭ 112 (+522.22%)
Mutual labels:  jest
generator-node
🔧 Yeoman generator for Node projects.
Stars: ✭ 16 (-11.11%)
Mutual labels:  jest
express-typeorm-rest-boilerplate
Boilerplate code to get started with building RESTful API Services (Express, TypeORM MongoDB stack)
Stars: ✭ 53 (+194.44%)
Mutual labels:  jest

👹 jest-extended-snapshot

Additional Jest matchers for snapshot testing.

📝 Read the blog post.

Requires Jest version >= 23.


version Build Status Changelog

Why?

If you find yourself in a scenario where you want to add tests after code has been written, you might want to use Jest snapshots.

A typical scenario is working with legacy code: it has no test, but you need to change/fix it. You should set up a test harness first, to ensure there would be no regression. Jest snapshots make this job easier.

This lib adds convenient matchers to work in such scenario.

Approval testing with Jest snapshots

Consider the previous example: you don’t know what a piece of code precisely does, but you don't want to break existing behavior. One approach to use in this situation is called "Approval testing".

It can get you test coverage quickly, without having to understand the code.

Unit testing asserts can be difficult to use. Approval tests simplify this by taking a snapshot of the results, and confirming that they have not changed.

Further information: http://approvaltests.com/

Installation

With npm:

npm install --save-dev jest-extended-snapshot

With yarn:

yarn add -D jest-extended-snapshot

Set up

Jest 23

Add jest-extended-snapshot to your Jest setupTestFrameworkScriptFile configuration. See Jest website for more information.

"jest": {
  "setupTestFrameworkScriptFile": "jest-extended-snapshot"
}

If you are already using another test framework, like jest-extended, you should create a test setup file to require each of the frameworks you are using.

For example:

// ./test-setup.js
require("jest-extended-snapshot");
require("any other test framework libraries you are using");

Then in your Jest config:

"jest": {
  "setupTestFrameworkScriptFile": "./test-setup.js"
}

Jest version >= 24

Add jest-extended-snapshot to your setupFilesAfterEnv array in the Jest configuration. See Jest website for more information.

"jest": {
  "setupFilesAfterEnv": ["jest-extended-snapshot"]
}

TypeScript

If your editor does not recognise the custom jest-extended-snapshot matchers, add a global.d.ts file to your project with:

import "jest-extended-snapshot";

List of additional matchers (API)

.toVerifyAllCombinations([args])

Test all combinations of given parameters to the function under test, and match against snapshot.

Usage:

expect(myFunction).toVerifyAllCombinations([args]);

Example

function myFunction(aNumber, aString) {
  if (aNumber > 0) {
    return `${aString} #${aNumber}`;
  }

  if (aString === "foo") {
    return `${aNumber} bar`;
  }

  return "This is twisted…";
}

it("should continue working as before", () => {
  expect(myFunction).toVerifyAllCombinations([1, -1], ["random", "foo"]);
});

Will produce following snapshot:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should continue working as before 1`] = `
Object {
  "-1,foo": "-1 bar",
  "-1,random": "This is twisted…",
  "1,foo": "foo #1",
  "1,random": "random #1",
}
`;

How to use best

  1. Determine the inputs you could combine to test your code
  2. Determine the output you could make a snapshot from
  3. Use test coverage to determine which parts are not covered yet
  4. Augment your inputs combination until you cover all of the code
  5. Perform little mutations in your covered code to check the quality of your snapshot
  6. Augment your inputs combination until there is no way you can add mutations without the test failing
  7. In the end, you got a snapshot of what your code does. And you can start refactoring with confidence

👌 I recommend you watch this video of Emily Bache, applying this over the Gilded Rose refactoring kata (in Java): https://youtu.be/zyM2Ep28ED8

List of helpers

range(start, end) / range(end)

Generate a list of numbers from start to end. Syntactic sugar to generate exhaustive combinations of number inputs.

Defaults start to 0 if called with a single parameter.

import { range } from "jest-extended-snapshot";

// All are equivalent
expect(myFunction).toVerifyAllCombinations([0, 1, 2, 3, 4], ["foo"]);
expect(myFunction).toVerifyAllCombinations(range(0, 4), ["foo"]);
expect(myFunction).toVerifyAllCombinations(range(4), ["foo"]);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].