All Projects → localvoid → karma-snapshot

localvoid / karma-snapshot

Licence: MIT License
🅰️🆚🅱️ Karma plugin for snapshot testing

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to karma-snapshot

gwt-material-table
A complex table component designed for the material design specifications
Stars: ✭ 28 (-3.45%)
Mutual labels:  snapshot
angular-karma test-explorer
vscode extension for easy angular testing and debugging
Stars: ✭ 67 (+131.03%)
Mutual labels:  karma
tropic
🍍 Test Runner Library
Stars: ✭ 29 (+0%)
Mutual labels:  karma
angular-material-boilerplate
A straightforward and well structured boilerplate based on Google's Angular Material project.
Stars: ✭ 28 (-3.45%)
Mutual labels:  karma
ngx-zombie-compiler
Fast JiT compiler for Angular testing
Stars: ✭ 15 (-48.28%)
Mutual labels:  karma
schema-shot
Framework-agnostic snapshot testing using "schema by example" for highly dynamic data
Stars: ✭ 34 (+17.24%)
Mutual labels:  snapshot
karma-nested-reporter
Easy to read test output with nested describe and it blocks
Stars: ✭ 14 (-51.72%)
Mutual labels:  karma
karma-telemetry
Karma Framework for running performance tasks using Telemetry
Stars: ✭ 36 (+24.14%)
Mutual labels:  karma
metadatamanagement
Metadatamanagement (MDM) - Data Search for Higher Education Research and Science Studies
Stars: ✭ 21 (-27.59%)
Mutual labels:  karma
terraform-aws-ec2-ami-backup
Terraform module for automatic & scheduled AMI creation
Stars: ✭ 19 (-34.48%)
Mutual labels:  snapshot
angular-unit-testing-examples
Showroom for different Angular unit testing concepts
Stars: ✭ 19 (-34.48%)
Mutual labels:  karma
pytest-snapshot
A plugin for snapshot testing with pytest.
Stars: ✭ 68 (+134.48%)
Mutual labels:  snapshot
SingleFile-Lite
Feel the power of the Manifest V3. The future, right now!
Stars: ✭ 55 (+89.66%)
Mutual labels:  snapshot
react-ssr-spa
Server side rendered single page app using reactjs official libraries.
Stars: ✭ 30 (+3.45%)
Mutual labels:  karma
reducer-tester
Utilities for testing redux reducers
Stars: ✭ 19 (-34.48%)
Mutual labels:  snapshot
PlutoVideoSnapshoter
Cross platform video snapshot software, stitching snapshots automatically with subtitle position detection.
Stars: ✭ 22 (-24.14%)
Mutual labels:  snapshot
Karma-mocha-example
Karma to drive mocha
Stars: ✭ 19 (-34.48%)
Mutual labels:  karma
vue-popover
Reusable popover component for Vue
Stars: ✭ 22 (-24.14%)
Mutual labels:  karma
freeshooter
This is old-school tool for taking screenshots without bloatware features, simple as life, light as air.
Stars: ✭ 102 (+251.72%)
Mutual labels:  snapshot
godot-engine.code-snapshot
A plugin for Godot Engine which will let you take beautified snapshots of your code within the Editor. Configure the frame as you like, with GDScript syntax already highlighted.
Stars: ✭ 32 (+10.34%)
Mutual labels:  snapshot

Karma Plugin for Snapshot Testing

karma-snapshot provides a communication layer between browser and Karma to store and retrieve snapshots.

karma-snapshot Example

Supported Assertion Libraries

Snapshot Format

Snapshot can be stored in different formats. Right now there are two formats supported: md and indented-md.

Markdown Format

This format is preferred when you specify language for code blocks in an assertion plugin. With this format, code editors will automatically highlight syntax of code blocks.

# `src/html.js`

## `Sub Suite`

####   `HTML Snapshot`

```html
<div>
  <span />
</div>
```

Indented Markdown Format

# `src/html.js`

## `Sub Suite`

####   `HTML Snapshot`

    <div>
      <span />
    </div>

Snapshot File Path

Snapshot file path is extracted from the name of the root suit cases and stored alongside with a tested files in a __snapshots__ directory.

Snapshot file path can be changed by providing a custom pathResolver in snapshot config.

Usage Example with Mocha and Chai

$ npm install karma karma-webpack karma-sourcemap-loader karma-snapshot karma-mocha \
              karma-mocha-snapshot karma-mocha-reporter karma-chrome-launcher mocha \
              chai chai-karma-snapshot webpack --save-dev

Karma configuration:

// karma.conf.js
const webpack = require("webpack");

module.exports = function (config) {
  config.set({
    browsers: ["ChromeHeadless"],
    frameworks: ["mocha", "snapshot", "mocha-snapshot"],
    reporters: ["mocha"],
    preprocessors: {
      "**/__snapshots__/**/*.md": ["snapshot"],
      "__tests__/index.js": ["webpack", "sourcemap"]
    },
    files: [
      "**/__snapshots__/**/*.md",
      "__tests__/index.js"
    ],

    colors: true,
    autoWatch: true,

    webpack: {
      devtool: "inline-source-map",
      performance: {
        hints: false
      },
    },

    webpackMiddleware: {
      stats: "errors-only",
      noInfo: true
    },

    snapshot: {
      update: !!process.env.UPDATE,
      prune: !!process.env.PRUNE,
    },

    mochaReporter: {
      showDiff: true,
    },

    client: {
      mocha: {
        reporter: "html",
        ui: "bdd",
      }
    },
  });
};

Source file:

// src/index.js

export function test() {
  return "Snapshot Test";
}

Test file:

// __tests__/index.js
import { use, expect, assert } from "chai";
import { matchSnapshot } from "chai-karma-snapshot";
import { test } from "../src/index.js";
use(matchSnapshot);

describe("src/index.js", () => {
  it("check snapshot", () => {
    // 'expect' syntax
    expect(test()).to.matchSnapshot();
    // 'assert' syntax
    assert.matchSnapshot(test());
  });
});

Run tests:

$ karma start

Update snapshots:

$ UPDATE=1 karma start --single-run

Prune snapshots:

$ PRUNE=1 karma start --single-run

Config

function resolve(basePath, suiteName) {
  return path.join(basePath, "__snapshots__", suiteName + ".md");
}

config.set({
  ...
  snapshot: {
    update: true,           // Run snapshot tests in UPDATE mode (default: false)
    prune: false,           // Prune unused snapshots (default: false)
    format: "indented-md",  // Snapshot format (default: md)
    checkSourceFile: true,  // Checks existince of the source file associated with tests (default: false)
    pathResolver: resolve,  // Custom path resolver,
    limitUnusedSnapshotsInWarning: -1  // Limit number of unused snapshots reported in the warning
                                       // -1 means no limit

  }
});

Custom Snapshot Format

Snapshot config option format also works with custom serialization formats. Custom snapshot serializer should have interface:

interface SnapshotSerializer {
  serialize: (name: string, suite: SnapshotSuite) => string,
  deserialize: (content: string) => { name: string, suite: SnapshotSuite },
}

Internals

Snapshot Data

karma-snapshot plugin is communicating with a browser by assigning a global variable __snapshot__ on a window object.

Snapshot data has a simple data structure:

declare global {
  interface Window {
    __snapshot__: SnapshotState;
  }
}

interface SnapshotState {
  update?: boolean;
  suite: SnapshotSuite;
}

interface SnapshotSuite {
  children: { [key: string]: SnapshotSuite };
  snapshots: { [key: string]: Snapshot[] };
  visited?: boolean;
  dirty?: boolean;
}

interface Snapshot {
  lang?: string;
  code: string;
  visited?: boolean;
  dirty?: boolean;
}

When SnapshotState.update variable is true, it indicates that assertion plugin should run in update mode, and instead of checking snapshots, it should update all values.

SnapshotState.suite is a reference to the root suite.

SnapshotSuite is a tree with snapshots that has a similar structure to test suites. children property is used to store references to children suites, and snapshots is used to store snapshot lists for tests in the current snapshot. Snapshots are stored as a list because each test can have multiple snapshot checks, and they should be automatically indexed by their position.

Snapshot is an object that stores details about snapshot. lang property indicates which language should be used in a markdown format to improve readability. code property stores snapshot value that will be checked by an assertion plugin.

visited is a flag that should be marked by an assertion plugin when it visits suites and snapshots. Visited flags are used to automatically prune removed snapshots.

dirty is a flag that should be marked by an assertion plugin when it updates or adds a new snapshot.

Interface for Assertion Libraries

To make it easier to add support for assertion libraries, SnapshotState has two methods that should be used when creating an API for an assertion library.

interface SnapshotSuite {
  get(path: string[], index: number): Snapshot | undefined;
  set(path: string[], index: number, code: string, lang?: string): void;
  match(received: string, expected: string): boolean;
}

get() method tries to find a Snapshot in a current snapshot state. It also automatically marks all nodes on the path as visited.

set() method adds or updates an existing Snapshot.

match() method checks if two snapshots are matching in normalized form.

Here is an example how it should be used:

function matchSnapshot(path: string[], index: number, received: string) {
  if (snapshotState.update) {
    snapshotState.set(path, index, received);
  } else {
    const snapshot = snapshotState.get(path, index);
    if (!snapshot) {
      snapshotState.set(path, index, received);
    } else {
      const pass = snapshotState.match(received, snapshot.code);
      if (!pass) {
        throw new AssertionError(`Received value does not match stored snapshot ${index}`);
      }
    }
  }
}
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].