comparadise-utils 1.25.2 → 1.26.1

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/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ declare global {
6
6
  namespace Cypress {
7
7
  interface Chainable {
8
8
  matchScreenshot(args?: MatchScreenshotArgs): Chainable;
9
+ createBaseImage(args?: MatchScreenshotArgs): Chainable;
9
10
  }
10
11
  }
11
12
  }
@@ -2,6 +2,13 @@ import pixelmatch from 'pixelmatch';
2
2
 
3
3
  type PixelMatchOptions = Parameters<typeof pixelmatch>[5];
4
4
 
5
+ declare const PREFIX_DIFFERENTIATOR = "___";
6
+ declare function forceFont(): false | HTMLStyleElement;
7
+ declare function getTestFolderPathFromScripts(rawName?: string): {
8
+ name: string;
9
+ screenshotsFolder: string;
10
+ };
11
+ declare function verifyImages(): void;
5
12
  type MatchScreenshotArgs = {
6
13
  rawName?: string;
7
14
  options?: Partial<Cypress.ScreenshotOptions> & {
@@ -24,4 +31,4 @@ declare global {
24
31
  }
25
32
  }
26
33
 
27
- export { type MatchScreenshotArgs, matchScreenshot };
34
+ export { type MatchScreenshotArgs, PREFIX_DIFFERENTIATOR, forceFont, getTestFolderPathFromScripts, matchScreenshot, verifyImages };
@@ -20,7 +20,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // match-screenshot.ts
21
21
  var match_screenshot_exports = {};
22
22
  __export(match_screenshot_exports, {
23
- matchScreenshot: () => matchScreenshot
23
+ PREFIX_DIFFERENTIATOR: () => PREFIX_DIFFERENTIATOR,
24
+ forceFont: () => forceFont,
25
+ getTestFolderPathFromScripts: () => getTestFolderPathFromScripts,
26
+ matchScreenshot: () => matchScreenshot,
27
+ verifyImages: () => verifyImages
24
28
  });
25
29
  module.exports = __toCommonJS(match_screenshot_exports);
26
30
  var PREFIX_DIFFERENTIATOR = "___";
@@ -121,5 +125,9 @@ Cypress.Commands.add(
121
125
  );
122
126
  // Annotate the CommonJS export names for ESM import in node:
123
127
  0 && (module.exports = {
124
- matchScreenshot
128
+ PREFIX_DIFFERENTIATOR,
129
+ forceFont,
130
+ getTestFolderPathFromScripts,
131
+ matchScreenshot,
132
+ verifyImages
125
133
  });
package/package.json CHANGED
@@ -28,10 +28,10 @@
28
28
  "peerDependencies": {
29
29
  "cypress": ">=12"
30
30
  },
31
- "version": "1.25.2",
31
+ "version": "1.26.1",
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
  }