All Projects → Dean177 → Jest To Match Shape Of

Dean177 / Jest To Match Shape Of

Licence: mit
A Jest matcher to verify the shape of an object. Makes integration testing simple.

Programming Languages

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

Projects that are alternatives of or similar to Jest To Match Shape Of

Dockest
Docker + Jest integration testing for Node.js
Stars: ✭ 81 (+138.24%)
Mutual labels:  jest, integration-testing
Javascript Testing Best Practices
📗🌐 🚢 Comprehensive and exhaustive JavaScript & Node.js testing best practices (August 2021)
Stars: ✭ 13,976 (+41005.88%)
Mutual labels:  jest, integration-testing
Gest
👨‍💻 A sensible GraphQL testing tool - test your GraphQL schema locally and in the cloud
Stars: ✭ 109 (+220.59%)
Mutual labels:  jest, integration-testing
awesome-javascript-testing
🔧 Awesome JavaScript testing resources
Stars: ✭ 28 (-17.65%)
Mutual labels:  jest, integration-testing
Frisby
Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.
Stars: ✭ 1,484 (+4264.71%)
Mutual labels:  jest, integration-testing
Jest Puppeteer
Run your tests using Jest & Puppeteer 🎪✨
Stars: ✭ 3,267 (+9508.82%)
Mutual labels:  jest, integration-testing
Yarn Package Boilerplate
An Yarn package with babel, jest, flow, prettier and more
Stars: ✭ 10 (-70.59%)
Mutual labels:  jest
Enzyme Matchers
Jasmine/Jest assertions for enzyme
Stars: ✭ 881 (+2491.18%)
Mutual labels:  jest
Javascript Data Structures And Algorithms
Exercises, algorithms
Stars: ✭ 26 (-23.53%)
Mutual labels:  jest
Jest Styled Components Stylelint
Run stylelint on your styled-components styles at runtime.
Stars: ✭ 25 (-26.47%)
Mutual labels:  jest
Express React Boilerplate
🚀🚀🚀 This is a tool that helps programmers create Express & React projects easily base on react-cool-starter.
Stars: ✭ 32 (-5.88%)
Mutual labels:  jest
Headless
Create a virtual X screen from Ruby, record videos and take screenshots.
Stars: ✭ 951 (+2697.06%)
Mutual labels:  integration-testing
Majestic
⚡ Zero config GUI for Jest
Stars: ✭ 7,261 (+21255.88%)
Mutual labels:  jest
Koa Starter
An opinionated Koa starter kit
Stars: ✭ 10 (-70.59%)
Mutual labels:  jest
React Ssr Starter
All have been introduced React environment
Stars: ✭ 20 (-41.18%)
Mutual labels:  jest
Angular Builders
Angular build facade extensions (Jest and custom webpack configuration)
Stars: ✭ 843 (+2379.41%)
Mutual labels:  jest
Enzyme To Json
Snapshot test your Enzyme wrappers
Stars: ✭ 954 (+2705.88%)
Mutual labels:  jest
Svelte Tailwind Extension Boilerplate
A Chrome extension boilerplate built with Svelte, TailwindCSS, Jest, and Rollup.
Stars: ✭ 26 (-23.53%)
Mutual labels:  jest
Relay Testing Utils
Easy to use relay mock and unit testing tool (works with Jest & Enzyme)
Stars: ✭ 13 (-61.76%)
Mutual labels:  jest
Eslint Import Resolver Jest
🃏 Jest import resolution plugin for eslint-plugin-import
Stars: ✭ 29 (-14.71%)
Mutual labels:  jest

toMatchShapeOf

CircleCI codecov Npm

A Jest matcher to verify the structure of an object, particularly useful for api integration tests

example-gif

Installation

yarn add jest-to-match-shape-of
npm install jest-to-match-shape-of --save

In your setupTests.js

// src/setupTests.js
const { toMatchOneOf, toMatchShapeOf } = require('jest-to-match-shape-of')

expect.extend({
  toMatchOneOf,
  toMatchShapeOf,
})

or if you are using Typescript

// src/setupTests.ts
import { toMatchOneOf, toMatchShapeOf } from 'jest-to-match-shape-of'

expect.extend({
  toMatchOneOf,
  toMatchShapeOf,
})

Then in the "jest" section of your package.json add the following: "setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js"

or for typescript: "setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.ts"

Installation with create-react-app

For project created using CRA (create-react-app) you need to add setup code to the setupTests.js file, there is no need to modify package.json.

// src/setupTests.js
const { toMatchOneOf, toMatchShapeOf } = require('jest-to-match-shape-of')
// or with ES6 module import { toMatchOneOf, toMatchShapeOf } from 'jest-to-match-shape-of';

expect.extend({
  toMatchOneOf,
  toMatchShapeOf,
})

Usage

expect(someThing).toMatchOneOf([someOtherThingA, someOtherThingB, someOtherThingC])
expect(someThing).toMatchShapeOf(someOtherThing)

Works particularly well when being used with Typescript to write integration tests e.g.

type Resource = {
  maybeNumber: number | null, 
  someString: string,
}

const testResource: Resource = {
  maybeNumber: 6,
  someString: 'some real looking data',
}

const testResourceAlt: Resource = {
  maybeNumber: null,
  someString: 'some real looking data',
}

describe('an api', () => {
  it('returns what I was expecting', () => {
    return fetch('/resources/1').then(response => response.json()).then((data) => {
      expect(data).toMatchShapeOf(testResource)  
    })            
  })
  
  it('could return a couple of different things', () => {
    return fetch('/resources/1').then(response => response.json()).then((data) => {
      expect(data).toMatchOneOf([testResource, testResourceAlt])  
    })    
  })
})

How to match a shape with optional fields?

Sometimes, the expected shape may vary during integration tests. (e.g: A field may be missing)

If you want to make a shape allow optional fields, the simplest way is to remove those fields from the expected shape, as follow:

toMatchOneOf([{ ant: 17 }]) // bat is optional here

A more robust alternative is to define all possible shapes, this way you still test the types of the properties:

toMatchOneOf([{ ant: 17, bat: 176 }, { ant: 17 }]) // bat is still optional, but must be numeric

Motivation

I wanted to write integration test for my frontend code but found it was tedious, brittle and hard to debug when I encountered a legitimate failure.

I realised that

  • Almost all of the errors were due to bad data from the API, most often missing data
  • I did not care about exactly what data came back, but more about the shape of the data.
  • Since I was using React and Typescript I could be confident my app would work as intended if the types were correct
  • Thanks to Enzyme I already had a great way to test my component interactions

toMatchShapeOf hopefully achieves a lot of the value of full blown integration test written with something like Nightwatch whilst being simpler to write, understand and debug.

Additionally I found that the test data I created for use with this matcher were useful for other unit tests in my application.

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].