Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@swimlane/cy-dom-diff

Package Overview
Dependencies
Maintainers
40
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@swimlane/cy-dom-diff - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

3

CHANGELOG.md

@@ -9,2 +9,5 @@ CHANGELOG

## 2.0.0 (2021-02-02)
* Possible breaking: Replaced @open-wc/sematic-dom-diff with @swimlane/dom-diff
## 1.0.1 (2021-02-01)

@@ -11,0 +14,0 @@ * Fixed bug causing uncaught failures in Cypress 6

9

commands.js

@@ -1,8 +0,9 @@

import { clean, disambiguateArgs, getDom } from './lib/util';
import { diff, getDiffableHTML, } from '@swimlane/dom-diff';
import { disambiguateArgs, getDom } from './lib/util';
import { chaiDomMatch } from './lib/assertion';
chai.use(chaiDomMatch);
const logDiff = (name, state, $el, re, options) => {
const actual = clean(getDom($el), options);
const expected = (re === null || re === void 0 ? void 0 : re.pattern) ? re.replace(re.pattern) : re;
const difference = (re === null || re === void 0 ? void 0 : re.diff) ? re.diff(actual) : undefined;
const actual = getDiffableHTML(getDom($el), options);
const expected = (re === null || re === void 0 ? void 0 : re.matchers) ? re.expected : re;
const difference = (re === null || re === void 0 ? void 0 : re.matchers) ? diff(re, actual) : undefined;
if (state === undefined) {

@@ -9,0 +10,0 @@ state = difference === '' ? 'passed' : 'failed';

export * from './lib/regexps';
export * from './lib/matchers';
export { dom } from '@swimlane/dom-diff';

@@ -1,2 +0,3 @@

import { getDom, clean, disambiguateArgs } from './util';
import { getDiffableHTML } from '@swimlane/dom-diff';
import { getDom, disambiguateArgs } from './util';
export const chaiDomMatch = (chai, utils) => {

@@ -7,3 +8,3 @@ function assertDomMatch(re, ...args) {

const object = utils.flag(this, 'object');
const dom = clean(getDom(object), options);
const dom = getDiffableHTML(getDom(object), options);
if (negate) {

@@ -10,0 +11,0 @@ new chai.Assertion(dom).not.match(re, message);

@@ -1,60 +0,1 @@

import { clean } from './util';
import { createPatch } from 'diff';
const escape = (source) => {
if (source instanceof RegExp)
return source.source;
return source.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
const formatPatch = (text) => {
return text
.replace(/^([^\n]+)\n([^\n]+)\n/m, '')
.replace(/--- \t\n/g, '') // headers
.replace(/\+\+\+ \t\n/g, '')
.split('\n')
.filter((x) => !x.includes('--- removed')) // Explanation
.filter((x) => !x.includes('+++ added'))
.filter((x) => !x.includes('@@ '))
.filter((x) => !x.includes('No newline at end of file'))
.join('\n')
.replace(/\n+$/, '\n')
.trim();
};
export class PatternRegExp extends RegExp {
constructor(source, pattern, matchers) {
super(source);
this.pattern = pattern;
this.matchers = matchers;
this.compare = function (left, right) {
if (right.includes('__arg')) {
const source = this.matchers.reduce((acc, m, i) => {
return acc.replace(`__arg${i}__`, escape(m));
}, escape(right));
return new RegExp(source).test(left);
}
return left === right;
};
this.replace = function (str) {
return this.matchers.reduce((acc, m, i) => {
return acc.replace(`__arg${i}__`, `\${${m}}`);
}, str);
};
this.diff = function (str) {
const options = {
comparator: (l, r) => this.compare(l, r),
};
const patch = createPatch('', this.pattern, str, '', '', options);
return formatPatch(this.replace(patch));
};
}
}
export const dom = (strings, ...args) => {
const result = [strings[0]];
args.forEach((arg, i) => {
result.push(`__arg${i}__`, strings[i + 1]);
});
const pattern = clean(result.join(''));
const source = args.reduce((acc, arg, i) => {
return acc.replace(`__arg${i}__`, escape(arg));
}, escape(pattern));
return new PatternRegExp(`^${source}$`, pattern, args);
};
export { dom } from '@swimlane/dom-diff';

@@ -1,3 +0,1 @@

import { getDiffableHTML, } from '@open-wc/semantic-dom-diff/get-diffable-html';
import unindent from 'strip-indent';
const { isJquery, isElement } = Cypress.dom;

@@ -13,8 +11,2 @@ export const getDom = ($el) => {

};
const domparser = new DOMParser();
export const clean = (html, options) => {
// Create a Node using DOMParser to avoid rendering in Cypress
const doc = domparser.parseFromString(`<diff-container>${html}</diff-container>`, 'text/html');
return unindent(getDiffableHTML(doc.body.firstChild, options));
};
export const disambiguateArgs = (args) => {

@@ -21,0 +13,0 @@ if (args.length === 2) {

{
"name": "@swimlane/cy-dom-diff",
"version": "1.0.1",
"version": "2.0.0",
"description": "matching chunks of DOM against HTML; including dynamic content.",

@@ -21,3 +21,3 @@ "main": "index.js",

"fix:tslint": "eslint -c .eslintrc.js --ext .ts ./src/ --fix",
"test": "run-s test:*",
"test": "run-s build test:*",
"test:prettier": "prettier \"src/**/*.ts\" --list-different",

@@ -35,4 +35,3 @@ "test:lint": "eslint -c .eslintrc.js --ext .ts ./src/",

"dependencies": {
"@open-wc/semantic-dom-diff": "^0.18.0",
"diff": "^4.0.2",
"@swimlane/dom-diff": "^1.0.1",
"strip-indent": "^3.0.0"

@@ -47,3 +46,3 @@ },

"chg": "^0.4.0",
"cypress": "^5.6.0",
"cypress": "^6.4.0",
"eslint": "^7.14.0",

@@ -50,0 +49,0 @@ "eslint-config-prettier": "^6.15.0",

@@ -1,5 +0,9 @@

import { DiffOptions } from '@open-wc/semantic-dom-diff/get-diffable-html';
import {
diff,
getDiffableHTML,
PatternRegExp,
ProcessorOptions,
} from '@swimlane/dom-diff';
import { PatternRegExp } from './lib/matchers';
import { clean, disambiguateArgs, getDom } from './lib/util';
import { disambiguateArgs, getDom } from './lib/util';
import { chaiDomMatch } from './lib/assertion';

@@ -10,3 +14,3 @@

type Options =
| Partial<Cypress.Loggable & Cypress.Timeoutable & DiffOptions>
| Partial<Cypress.Loggable & Cypress.Timeoutable & ProcessorOptions>
| undefined;

@@ -21,5 +25,5 @@

) => {
const actual = clean(getDom($el), options);
const expected = re?.pattern ? re.replace(re.pattern) : re;
const difference = re?.diff ? re.diff(actual) : undefined;
const actual = getDiffableHTML(getDom($el), options);
const expected = re?.matchers ? re.expected : re;
const difference = re?.matchers ? diff(re, actual) : undefined;

@@ -26,0 +30,0 @@ if (state === undefined) {

export * from './lib/regexps';
export * from './lib/matchers';
export { dom, PatternRegExp } from '@swimlane/dom-diff';

@@ -1,4 +0,4 @@

import { DiffOptions } from '@open-wc/semantic-dom-diff/get-diffable-html';
import { getDiffableHTML, ProcessorOptions } from '@swimlane/dom-diff';
import { getDom, clean, disambiguateArgs } from './util';
import { getDom, disambiguateArgs } from './util';

@@ -9,5 +9,8 @@ export const chaiDomMatch = (chai: Chai.ChaiStatic, utils: Chai.ChaiUtils) => {

re: RegExp,
...args: [string | DiffOptions, DiffOptions]
...args: [string | ProcessorOptions, ProcessorOptions]
) {
const [message, options] = disambiguateArgs(args) as [string, DiffOptions];
const [message, options] = disambiguateArgs(args) as [
string,
ProcessorOptions
];

@@ -17,3 +20,3 @@ const negate = utils.flag(this, 'negate');

const dom = clean(getDom(object), options);
const dom = getDiffableHTML(getDom(object), options);

@@ -20,0 +23,0 @@ if (negate) {

@@ -1,72 +0,1 @@

import { clean } from './util';
import { createPatch } from 'diff';
const escape = (source: RegExp | string) => {
if (source instanceof RegExp) return source.source;
return source.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
const formatPatch = (text: string) => {
return text
.replace(/^([^\n]+)\n([^\n]+)\n/m, '')
.replace(/--- \t\n/g, '') // headers
.replace(/\+\+\+ \t\n/g, '')
.split('\n')
.filter((x) => !x.includes('--- removed')) // Explanation
.filter((x) => !x.includes('+++ added'))
.filter((x) => !x.includes('@@ '))
.filter((x) => !x.includes('No newline at end of file'))
.join('\n')
.replace(/\n+$/, '\n')
.trim();
};
export class PatternRegExp extends RegExp {
constructor(
source: string,
public readonly pattern: string,
public readonly matchers: any[]
) {
super(source);
}
compare = function (this: PatternRegExp, left: string, right: string) {
if (right.includes('__arg')) {
const source = this.matchers.reduce((acc, m, i) => {
return acc.replace(`__arg${i}__`, escape(m));
}, escape(right));
return new RegExp(source).test(left);
}
return left === right;
};
replace = function (this: PatternRegExp, str: string) {
return this.matchers.reduce((acc, m, i) => {
return acc.replace(`__arg${i}__`, `\${${m}}`);
}, str);
};
diff = function (this: PatternRegExp, str: string) {
const options = {
comparator: (l: string, r: string) => this.compare(l, r),
};
const patch = createPatch('', this.pattern, str, '', '', options as any);
return formatPatch(this.replace(patch));
};
}
export const dom = (
strings: TemplateStringsArray,
...args: any[]
): PatternRegExp => {
const result = [strings[0]];
args.forEach((arg, i) => {
result.push(`__arg${i}__`, strings[i + 1]);
});
const pattern = clean(result.join(''));
const source = args.reduce((acc, arg, i) => {
return acc.replace(`__arg${i}__`, escape(arg));
}, escape(pattern));
return new PatternRegExp(`^${source}$`, pattern, args);
};
export { dom, PatternRegExp } from '@swimlane/dom-diff';

@@ -1,8 +0,1 @@

import {
DiffOptions,
getDiffableHTML,
} from '@open-wc/semantic-dom-diff/get-diffable-html';
import unindent from 'strip-indent';
const { isJquery, isElement } = Cypress.dom;

@@ -20,13 +13,2 @@

const domparser = new DOMParser();
export const clean = (html: string, options?: DiffOptions): string => {
// Create a Node using DOMParser to avoid rendering in Cypress
const doc = domparser.parseFromString(
`<diff-container>${html}</diff-container>`,
'text/html'
);
return unindent(getDiffableHTML(doc.body.firstChild as Node, options));
};
export const disambiguateArgs = (

@@ -33,0 +15,0 @@ args: [

declare namespace Chai {
type DiffOptions = import('@open-wc/semantic-dom-diff/get-diffable-html').DiffOptions;
type ProcessorOptions = import('@swimlane/dom-diff').ProcessorOptions;
interface Assertion {
domMatch(re: RegExp, message?: string | DiffOptions): Assertion;
domMatch(re: RegExp, message?: string, options?: DiffOptions): Assertion;
domMatch(re: RegExp, message?: string | ProcessorOptions): Assertion;
domMatch(
re: RegExp,
message?: string,
options?: ProcessorOptions
): Assertion;
}
}
/// <reference types="cypress" />
declare namespace Cypress {
type DiffOptions = import('@open-wc/semantic-dom-diff/get-diffable-html').DiffOptions;
type ProcessorOptions = import('@swimlane/dom-diff').ProcessorOptions;
type Options =
| Partial<Cypress.Loggable & Cypress.Timeoutable & DiffOptions>
| Partial<Cypress.Loggable & Cypress.Timeoutable & ProcessorOptions>
| undefined;

@@ -8,0 +8,0 @@

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