Giter Club home page Giter Club logo

jest-change-matchers's Introduction

jest-change-matchers npm version codecov CircleCI

Jest matchers to evaluate side effect changes.

Installation

yarn add --dev jest-change-matchers

Usage

After installing the packge you will have a few more matchers in your toolbelt. If you are familiar with RSpec, the new matchers should make you feel like home :).

toChange

describe("toChange", () => {
  beforeEach(() => {
    sharedState = 0
  })

  describe(".toChange", () => {
    it("passes when there is a change", async () => {
      await expect(async () => {
        sharedState += 1
      }).toChange(async () => {
        return sharedState
      })
    })

    it("throws an error when there is no change", async () => {
      const testCase = () => {
        return expect(async () => {
          sharedState += 0
        }).toChange(async () => {
          return sharedState
        })
      }

      await expect(testCase()).rejects.toThrowErrorMatchingSnapshot()
    })
  })

  describe(".not.toChange", () => {
    it("passes when there is no change", async () => {
      await expect(async () => {
        // no-op
      }).not.toChange(async () => {
        return sharedState
      })
    })

    it("throws an error when there is a change", async () => {
      const testCase = () => {
        return expect(async () => {
          sharedState += 1
        }).not.toChange(async () => {
          return sharedState
        })
      }

      await expect(testCase()).rejects.toThrowErrorMatchingSnapshot()
    })
  })
})

toChangeBy

describe("toChangeBy", () => {
  beforeEach(() => {
    sharedState = 0
  })

  describe(".toChangeBy", () => {
    it("passes with positive changes", async () => {
      await expect(async () => {
        sharedState += 1
      }).toChangeBy(async () => {
        return sharedState
      }, 1)
    })

    it("passes with negative changes", async () => {
      await expect(async () => {
        sharedState -= 1
      }).toChangeBy(async () => {
        return sharedState
      }, -1)
    })

    it("throws an error when it doesn't change the desired amount", async () => {
      const testCase = () => {
        return expect(async () => {
          sharedState += 2
        }).toChangeBy(async () => {
          return sharedState
        }, 1)
      }

      await expect(testCase()).rejects.toThrowErrorMatchingSnapshot()
    })
  })

  describe(".not.toChangeBy", () => {
    it("passes with positive changes", async () => {
      await expect(async () => {
        sharedState += 1
      }).not.toChangeBy(async () => {
        return sharedState
      }, -1)
    })

    it("passes with negative changes", async () => {
      await expect(async () => {
        sharedState -= 1
      }).not.toChangeBy(async () => {
        return sharedState
      }, 1)
    })

    it("throws an error when it changes the desired amount", async () => {
      const testCase = () => {
        return expect(async () => {
          sharedState += 2
        }).not.toChangeBy(async () => {
          return sharedState
        }, 2)
      }

      await expect(testCase()).rejects.toThrowErrorMatchingSnapshot()
    })
  })
})

toChangeFromTo

describe("toChangeFromTo", () => {
  beforeEach(() => {
    sharedState = 0
  })

  describe(".toChangeFromTo", () => {
    it("passes when it changes from previous and new value", async () => {
      await expect(async () => {
        sharedState += 1
      }).toChangeFromTo(async () => {
        return sharedState
      }, 0, 1)
    })

    it("throws an error when it doesn't change from previous value", async () => {
      const testCase = () => {
        return expect(async () => {
          sharedState += 2
        }).toChangeFromTo(async () => {
          return sharedState
        }, 1, 2)
      }

      await expect(testCase()).rejects.toThrowErrorMatchingSnapshot()
    })

    it("throws an error when it doesn't change to new value", async () => {
      const testCase = () => {
        return expect(async () => {
          sharedState += 2
        }).toChangeFromTo(async () => {
          return sharedState
        }, 0, 1)
      }

      await expect(testCase()).rejects.toThrowErrorMatchingSnapshot()
    })
  })

  describe(".not.toChangeFromTo", () => {
    it("passes when it doesn't change to previous and new value", async () => {
      await expect(async () => {
        sharedState += 1
      }).not.toChangeFromTo(async () => {
        return sharedState
      }, 0, 0)
    })

    it("throws an error when it change to previous and new value", async () => {
      const testCase = () => {
        return expect(async () => {
          sharedState += 2
        }).not.toChangeFromTo(async () => {
          return sharedState
        }, 0, 2)
      }

      await expect(testCase()).rejects.toThrowErrorMatchingSnapshot()
    })

  })
})

Contact

jest-change-matchers's People

Contributors

bilby91 avatar

Stargazers

Takahiro, NAKAMURA (na9amura) avatar

Watchers

James Cloos avatar  avatar

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.