Giter VIP home page Giter VIP logo

slapshot's Introduction

Slapshot

Codeship Status for mattapperson/slapshot NPM version dependencies Status

A simple, easy utility to write a single set of tests that can be run:

  • "online" (full integration test)

OR

  • "offline" (auto-mocked unit tests with result snapshotted from when last run "online")

Install

yarn install --dev @mattapperson/slapshot

Why would someone want this?

Developers love to debate unit tests vs integration tests. But the honest truth is they bith are extreamly useful, and both have serious down sides.

Integration tests give you a ton of confidence about your code... but are often slow, and in many cases require an internet connection.

Unit tests Run quickly, giving rapid feedback on the code your developing, thus allowing you to move faster... but many times you begin to assemble the many pices of your codebase that are all well unit tested, but together they fail, or are buggy. Not to mention often times maintaining Mocks for intigration points is like maintaining comments in code... too often things dont match up, or are insuffecient.

You could write 2 test suites... but that's 2x the tests to maintain.

But with Slapshot:

  • one test/mock file, and one step... No need to write a test that generates the mocks first, allowing it to feel like Jest snapshots.
  • Tests to be run as unit tests... but if the ENV var SLAPSHOT_ONLINE=true is set the tests are run as intigration tests. All with a single test being written!
  • No setup to ensure is working localy or a CI... enviorment starting or not can be programaticly controlled in the test itself using callWhenOnline
  • To update the snapshotted results, simply run your tests with the ENV var SLAPSHOT_ONLINE=true AND the cli arg of --updateSnapshot

Requirements

  • Node 8 or higher
  • Jest 20.0 or higher

Example usage / API

=======

/**
 * Method 1: callWhenOnline
 *
 * This block only runs if recording or running as intigration, and starts mongodb to be used in the tests
 **/
await callWhenOnline(async () => {
  await new Promise(resolve => {
    const pipe = spawn("mongod", ["--dbpath=<LOCATION>", "--port", "1223"]);
    pipe.stdout.on("data", function(data) {
      if (data.indexOf("started") !== -1) {
        resolve();
      }
    });
  });
});

/**
 * Method 2: memorize
 *
 * if CLI flag `--updateSnapshot` or the env var `SLAPSHOT_ONLINE=true` is set to record, this returns the live server results
 * if not, it returns a snapshotted version. When live, the above callWhenOnline would have
 * started the server it would connect to making the process seamless
 **/
test("get user by ID", async () => {
  const results = await memorize("get foo user", async () => getUser("foo"));
  expect(results.fullName).toBe("Matt Apperson");
});

async function getUser(userId) {
  const client = await mongodb.connect({ port: 1223 });
  return client.query({ id: userId });
}

slapshot's People

Contributors

mattapperson avatar nchaulet 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.