comparadise-utils 1.26.0 → 1.26.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/commands.js CHANGED
@@ -1 +1,2 @@
1
1
  require('./dist/match-screenshot');
2
+ require('./dist/create-base-image');
@@ -0,0 +1,20 @@
1
+ import { MatchScreenshotArgs } from './match-screenshot.js';
2
+ import 'pixelmatch';
3
+
4
+ declare function createBaseImage(subject: Cypress.JQueryWithSelector | Window | Document | void, args?: MatchScreenshotArgs): void;
5
+ interface ExtendedCurrentRunnable extends Mocha.Runnable {
6
+ currentRunnable?: {
7
+ order?: unknown;
8
+ };
9
+ }
10
+ declare global {
11
+ namespace Cypress {
12
+ interface Cypress {
13
+ mocha: {
14
+ getRunner: () => ExtendedCurrentRunnable;
15
+ };
16
+ }
17
+ }
18
+ }
19
+
20
+ export { createBaseImage };
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // create-base-image.ts
21
+ var create_base_image_exports = {};
22
+ __export(create_base_image_exports, {
23
+ createBaseImage: () => createBaseImage
24
+ });
25
+ module.exports = __toCommonJS(create_base_image_exports);
26
+
27
+ // match-screenshot.ts
28
+ var PREFIX_DIFFERENTIATOR = "___";
29
+ var SUFFIX_TEST_IDENTIFIER = ".spec.ts";
30
+ var SCREENSHOTS_FOLDER_NAME = "screenshots";
31
+ function forceFont() {
32
+ const iframe = window.parent.document.querySelector("iframe");
33
+ const contentDocument = iframe && iframe.contentDocument;
34
+ if (contentDocument) {
35
+ const style = contentDocument.createElement("style");
36
+ style.type = "text/css";
37
+ style.appendChild(
38
+ contentDocument.createTextNode("* { font-family: Arial !important; }")
39
+ );
40
+ contentDocument.head.appendChild(style);
41
+ return style;
42
+ }
43
+ return false;
44
+ }
45
+ function getTestFolderPathFromScripts(rawName) {
46
+ const relativeTestPath = Cypress.spec.relative;
47
+ if (!relativeTestPath) {
48
+ throw new Error(
49
+ "\u274C Could not find matching script in the Cypress DOM to infer the test folder path"
50
+ );
51
+ }
52
+ const currentTestNumber = Cypress.mocha.getRunner().currentRunnable?.order;
53
+ if (!rawName && typeof currentTestNumber === "number" && currentTestNumber > 1) {
54
+ throw new Error(
55
+ "\u274C The rawName argument was not provided to matchScreenshot and is required for test files containing multiple tests!"
56
+ );
57
+ }
58
+ const testName = relativeTestPath.substring(
59
+ relativeTestPath.lastIndexOf("/") + 1,
60
+ relativeTestPath.lastIndexOf(SUFFIX_TEST_IDENTIFIER)
61
+ );
62
+ const name = rawName || testName;
63
+ const screenshotsFolder = `${SCREENSHOTS_FOLDER_NAME}/${relativeTestPath.substring(
64
+ 0,
65
+ relativeTestPath.lastIndexOf(testName)
66
+ )}${name}`;
67
+ return {
68
+ name,
69
+ screenshotsFolder
70
+ };
71
+ }
72
+ function verifyImages() {
73
+ if (Cypress.$("img:visible").length > 0) {
74
+ cy.document().its("body").find("img").filter(":visible").then((images) => {
75
+ if (images) {
76
+ cy.wrap(images).each(($img) => {
77
+ cy.wrap($img).should("exist").and("have.prop", "naturalWidth");
78
+ });
79
+ }
80
+ });
81
+ }
82
+ }
83
+ function matchScreenshot(subject, args) {
84
+ const { rawName, options } = args || {};
85
+ forceFont();
86
+ verifyImages();
87
+ const { name, screenshotsFolder } = getTestFolderPathFromScripts(rawName);
88
+ cy.task("baseExists", screenshotsFolder).then((hasBase) => {
89
+ const target = subject ? cy.wrap(subject) : cy;
90
+ target.screenshot(
91
+ `${PREFIX_DIFFERENTIATOR}${screenshotsFolder}/new`,
92
+ options
93
+ );
94
+ if (!hasBase) {
95
+ cy.task(
96
+ "log",
97
+ `\u2705 A new base image was created for ${name}. Create this as a new base image via Comparadise!`
98
+ );
99
+ return null;
100
+ }
101
+ const pixelMatchOptions = options?.pixelMatchOptions;
102
+ const compareScreenshotsArg = {
103
+ screenshotsFolder,
104
+ pixelMatchOptions
105
+ };
106
+ cy.task("compareScreenshots", compareScreenshotsArg).then((diffPixels) => {
107
+ if (diffPixels === 0) {
108
+ cy.log(`\u2705 Actual image of ${name} was the same as base`);
109
+ } else {
110
+ throw new Error(
111
+ `\u274C Actual image of ${name} differed by ${diffPixels} pixels.`
112
+ );
113
+ }
114
+ return null;
115
+ });
116
+ return null;
117
+ });
118
+ }
119
+ Cypress.Commands.add(
120
+ "matchScreenshot",
121
+ { prevSubject: ["optional", "element", "window", "document"] },
122
+ matchScreenshot
123
+ );
124
+
125
+ // create-base-image.ts
126
+ function createBaseImage(subject, args) {
127
+ const { rawName, options } = args || {};
128
+ forceFont();
129
+ verifyImages();
130
+ const { name, screenshotsFolder } = getTestFolderPathFromScripts(rawName);
131
+ const target = subject ? cy.wrap(subject) : cy;
132
+ target.screenshot(
133
+ `${PREFIX_DIFFERENTIATOR}${screenshotsFolder}/base`,
134
+ options
135
+ );
136
+ cy.task("log", `\u2705 A new base image was created for ${name}.`);
137
+ }
138
+ Cypress.Commands.add(
139
+ "createBaseImage",
140
+ { prevSubject: ["optional", "element", "window", "document"] },
141
+ createBaseImage
142
+ );
143
+ // Annotate the CommonJS export names for ESM import in node:
144
+ 0 && (module.exports = {
145
+ createBaseImage
146
+ });
package/package.json CHANGED
@@ -22,16 +22,16 @@
22
22
  "devDependencies": {
23
23
  "@types/pixelmatch": "5.2.6",
24
24
  "@types/pngjs": "6.0.5",
25
- "cypress": "14.1.0",
25
+ "cypress": "14.3.2",
26
26
  "tsup": "8.4.0"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "cypress": ">=12"
30
30
  },
31
- "version": "1.26.0",
31
+ "version": "1.26.2",
32
32
  "scripts": {
33
33
  "build": "tsup",
34
- "postbuild": "echo \"require('./dist/match-screenshot');\" > commands.js",
34
+ "postbuild": "echo \"require('./dist/match-screenshot');\nrequire('./dist/create-base-image');\" > commands.js",
35
35
  "test": "jest"
36
36
  }
37
37
  }