New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

chai-karma-snapshot

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chai-karma-snapshot - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

95

dist/cjs/index.js

@@ -19,29 +19,85 @@ "use strict";

exports.addSerializer = addSerializer;
// Extra line breaks at the beginning and at the end of the snapshot are useful
// to make the content of the snapshot easier to read
function addExtraLineBreaks(string) {
return string.includes("\n") ? "\n" + string + "\n" : string;
}
function serialize(data) {
return addExtraLineBreaks(normalizeNewlines(prettyFormat(data, {
return normalizeNewlines(prettyFormat(data, {
escapeRegex: true,
plugins: serializationPlugins,
printFunctionName: false,
})));
}));
}
function snapshotPath(node) {
var path = [];
while (node && node.parent) {
path.push(node.title);
node = node.parent;
}
return path.reverse();
}
function matchSnapshot(chai, utils) {
var context = window.__mocha_context__;
var snapshotState = window.__snapshot_state__;
var snapshot = window.__snapshot__;
utils.addMethod(chai.Assertion.prototype, "matchSnapshot", function (update) {
var snapshotState = window.__snapshot__;
var newSnapshotState = window.__snapshot__ = {
update: snapshotState.update,
dirty: snapshotState.dirty,
suite: {
children: {},
snapshots: {}
}
};
function getSnapshot(path, index) {
var suite = snapshotState.suite;
for (var i = 0; i < path.length - 1; i++) {
var key = path[i];
var s = suite.children[key];
if (s === undefined) {
return undefined;
}
else {
suite = s;
}
}
var testName = path[path.length - 1];
var snapshotList = suite.snapshots[testName];
if (snapshotList !== undefined) {
return snapshotList[index];
}
return undefined;
}
function addSnapshot(path, index, lang, code, dirty) {
var suite = newSnapshotState.suite;
for (var i = 0; i < path.length - 1; i++) {
var key = path[i];
var s = suite.children[key];
if (s === undefined) {
suite.children[key] = suite = {
children: {},
snapshots: {},
};
}
else {
suite = s;
}
}
var testName = path[path.length - 1];
var snapshotList = suite.snapshots[testName];
if (snapshotList === undefined) {
suite.snapshots[testName] = snapshotList = [];
}
snapshotList[index] = {
visited: true,
dirty: dirty,
lang: lang,
code: code
};
}
utils.addMethod(chai.Assertion.prototype, "matchSnapshot", function (lang, update) {
var obj = serialize(chai.util.flag(this, "object"));
var ssfi = chai.util.flag(this, "ssfi");
var snapshotName = context.runnable.title + " " + context.index++;
snapshotState.visited[snapshotName] = true;
if (!update && !snapshotState.update && snapshot.hasOwnProperty(snapshotName)) {
var s = snapshot[snapshotName];
if (obj !== s) {
throw new chai.AssertionError("Received value does not match stored snapshot " + snapshotName + ".", {
var path = snapshotPath(context.runnable);
var snapshot = getSnapshot(path, context.index);
var dirty = false;
if (!update && !snapshotState.update && snapshot) {
if (obj !== snapshot.code) {
throw new chai.AssertionError("Received value does not match stored snapshot.", {
actual: obj,
expected: s,
expected: snapshot.code,
showDiff: true

@@ -52,5 +108,6 @@ }, ssfi);

else {
snapshotState.dirty = true;
snapshot[snapshotName] = obj;
dirty = true;
newSnapshotState.dirty = true;
}
addSnapshot(path, context.index, lang === undefined ? null : lang, obj, dirty);
});

@@ -57,0 +114,0 @@ }

@@ -16,29 +16,85 @@ import * as prettyFormat from "pretty-format";

}
// Extra line breaks at the beginning and at the end of the snapshot are useful
// to make the content of the snapshot easier to read
function addExtraLineBreaks(string) {
return string.includes("\n") ? "\n" + string + "\n" : string;
}
function serialize(data) {
return addExtraLineBreaks(normalizeNewlines(prettyFormat(data, {
return normalizeNewlines(prettyFormat(data, {
escapeRegex: true,
plugins: serializationPlugins,
printFunctionName: false,
})));
}));
}
function snapshotPath(node) {
var path = [];
while (node && node.parent) {
path.push(node.title);
node = node.parent;
}
return path.reverse();
}
export function matchSnapshot(chai, utils) {
var context = window.__mocha_context__;
var snapshotState = window.__snapshot_state__;
var snapshot = window.__snapshot__;
utils.addMethod(chai.Assertion.prototype, "matchSnapshot", function (update) {
var snapshotState = window.__snapshot__;
var newSnapshotState = window.__snapshot__ = {
update: snapshotState.update,
dirty: snapshotState.dirty,
suite: {
children: {},
snapshots: {}
}
};
function getSnapshot(path, index) {
var suite = snapshotState.suite;
for (var i = 0; i < path.length - 1; i++) {
var key = path[i];
var s = suite.children[key];
if (s === undefined) {
return undefined;
}
else {
suite = s;
}
}
var testName = path[path.length - 1];
var snapshotList = suite.snapshots[testName];
if (snapshotList !== undefined) {
return snapshotList[index];
}
return undefined;
}
function addSnapshot(path, index, lang, code, dirty) {
var suite = newSnapshotState.suite;
for (var i = 0; i < path.length - 1; i++) {
var key = path[i];
var s = suite.children[key];
if (s === undefined) {
suite.children[key] = suite = {
children: {},
snapshots: {},
};
}
else {
suite = s;
}
}
var testName = path[path.length - 1];
var snapshotList = suite.snapshots[testName];
if (snapshotList === undefined) {
suite.snapshots[testName] = snapshotList = [];
}
snapshotList[index] = {
visited: true,
dirty: dirty,
lang: lang,
code: code
};
}
utils.addMethod(chai.Assertion.prototype, "matchSnapshot", function (lang, update) {
var obj = serialize(chai.util.flag(this, "object"));
var ssfi = chai.util.flag(this, "ssfi");
var snapshotName = context.runnable.title + " " + context.index++;
snapshotState.visited[snapshotName] = true;
if (!update && !snapshotState.update && snapshot.hasOwnProperty(snapshotName)) {
var s = snapshot[snapshotName];
if (obj !== s) {
throw new chai.AssertionError("Received value does not match stored snapshot " + snapshotName + ".", {
var path = snapshotPath(context.runnable);
var snapshot = getSnapshot(path, context.index);
var dirty = false;
if (!update && !snapshotState.update && snapshot) {
if (obj !== snapshot.code) {
throw new chai.AssertionError("Received value does not match stored snapshot.", {
actual: obj,
expected: s,
expected: snapshot.code,
showDiff: true

@@ -49,7 +105,8 @@ }, ssfi);

else {
snapshotState.dirty = true;
snapshot[snapshotName] = obj;
dirty = true;
newSnapshotState.dirty = true;
}
addSnapshot(path, context.index, lang === undefined ? null : lang, obj, dirty);
});
}
//# sourceMappingURL=index.js.map

25

dist/typings/index.d.ts
/// <reference types="mocha" />
import { IRunnable } from "mocha";
declare global {
interface Suite {
children: {
[key: string]: Suite;
};
snapshots: {
[key: string]: Snapshot[];
};
}
interface Snapshot {
dirty: boolean;
visited: boolean;
lang: string | null;
code: string;
}
interface Window {

@@ -9,16 +23,11 @@ __mocha_context__: {

};
__snapshot_state__: {
__snapshot__: {
update: boolean;
dirty: boolean;
visited: {
[key: string]: boolean;
};
suite: Suite;
};
__snapshot__: {
[key: string]: any;
};
}
namespace Chai {
interface Assertion {
matchSnapshot(update?: boolean): Assertion;
matchSnapshot(lang?: string, update?: boolean): Assertion;
}

@@ -25,0 +34,0 @@ }

{
"name": "chai-karma-snapshot",
"version": "0.2.0",
"version": "0.3.0",
"main": "dist/cjs/index.js",

@@ -5,0 +5,0 @@ "module": "dist/js/index.js",

@@ -7,2 +7,20 @@ # Chai Plugin for Snapshot Testing with Karma

## Snapshot Format
Snapshots are stored in a [Markdown](https://en.wikipedia.org/wiki/Markdown) format to improve readability.
````md
## `Root Suite`
## `Sub Suite`
#### `HTML Snapshot`
```html
<div>
<span />
</div>
```
````
## Usage Example

@@ -27,3 +45,6 @@

reporters: ["mocha"],
preprocessors: { "__tests__/index.js": ["webpack", "sourcemap"] },
preprocessors: {
"**/__snapshot__/**/*.md": ["snapshot"],
"__tests__/index.js": ["webpack", "sourcemap"]
},
files: ["__tests__/index.js"],

@@ -30,0 +51,0 @@

@@ -5,2 +5,14 @@ import { IRunnable } from "mocha";

declare global {
interface Suite {
children: { [key: string]: Suite };
snapshots: { [key: string]: Snapshot[] };
}
interface Snapshot {
dirty: boolean;
visited: boolean;
lang: string | null;
code: string;
}
interface Window {

@@ -11,8 +23,7 @@ __mocha_context__: {

};
__snapshot_state__: {
update: boolean,
dirty: boolean,
visited: { [key: string]: boolean },
__snapshot__: {
update: boolean;
dirty: boolean;
suite: Suite;
};
__snapshot__: { [key: string]: any };
}

@@ -22,3 +33,3 @@

interface Assertion {
matchSnapshot(update?: boolean): Assertion;
matchSnapshot(lang?: string, update?: boolean): Assertion;
}

@@ -45,40 +56,96 @@ }

// Extra line breaks at the beginning and at the end of the snapshot are useful
// to make the content of the snapshot easier to read
function addExtraLineBreaks(string: string) {
return string.includes("\n") ? `\n${string}\n` : string;
}
function serialize(data: any): string {
return addExtraLineBreaks(
normalizeNewlines(
prettyFormat(data, {
escapeRegex: true,
plugins: serializationPlugins,
printFunctionName: false,
}),
),
return normalizeNewlines(
prettyFormat(data, {
escapeRegex: true,
plugins: serializationPlugins,
printFunctionName: false,
}),
);
}
function snapshotPath(node: any): string[] {
let path = [];
while (node && node.parent) {
path.push(node.title);
node = node.parent;
}
return path.reverse();
}
export function matchSnapshot(chai: any, utils: any): void {
const context = window.__mocha_context__;
const snapshotState = window.__snapshot_state__;
const snapshot = window.__snapshot__;
const snapshotState = window.__snapshot__;
const newSnapshotState = window.__snapshot__ = {
update: snapshotState.update,
dirty: snapshotState.dirty,
suite: {
children: {},
snapshots: {}
} as Suite
};
utils.addMethod(chai.Assertion.prototype, "matchSnapshot", function (this: any, update?: boolean) {
function getSnapshot(path: string[], index: number): Snapshot | undefined {
let suite = snapshotState.suite;
for (let i = 0; i < path.length - 1; i++) {
const key = path[i];
const s = suite.children[key];
if (s === undefined) {
return undefined;
} else {
suite = s;
}
}
const testName = path[path.length - 1];
let snapshotList = suite.snapshots[testName];
if (snapshotList !== undefined) {
return snapshotList[index];
}
return undefined;
}
function addSnapshot(path: string[], index: number, lang: string | null, code: string, dirty: boolean): void {
let suite = newSnapshotState.suite;
for (let i = 0; i < path.length - 1; i++) {
const key = path[i];
const s = suite.children[key];
if (s === undefined) {
suite.children[key] = suite = {
children: {},
snapshots: {},
};
} else {
suite = s;
}
}
const testName = path[path.length - 1];
let snapshotList = suite.snapshots[testName];
if (snapshotList === undefined) {
suite.snapshots[testName] = snapshotList = [];
}
snapshotList[index] = {
visited: true,
dirty: dirty,
lang: lang,
code: code
};
}
utils.addMethod(chai.Assertion.prototype, "matchSnapshot", function (this: any, lang?: string, update?: boolean) {
const obj = serialize(chai.util.flag(this, "object"));
const ssfi = chai.util.flag(this, "ssfi")
const snapshotName = context.runnable.title + " " + context.index++;
snapshotState.visited[snapshotName] = true;
const path = snapshotPath(context.runnable);
const snapshot = getSnapshot(path, context.index);
let dirty = false;
if (!update && !snapshotState.update && snapshot.hasOwnProperty(snapshotName)) {
const s = snapshot[snapshotName];
if (obj !== s) {
if (!update && !snapshotState.update && snapshot) {
if (obj !== snapshot.code) {
throw new chai.AssertionError(
`Received value does not match stored snapshot ${snapshotName}.`,
`Received value does not match stored snapshot.`,
{
actual: obj,
expected: s,
expected: snapshot.code,
showDiff: true

@@ -90,6 +157,7 @@ },

} else {
snapshotState.dirty = true;
snapshot[snapshotName] = obj;
dirty = true;
newSnapshotState.dirty = true;
}
addSnapshot(path, context.index, lang === undefined ? null : lang, obj as string, dirty);
});
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc