Skip to content

Commit

Permalink
fix: image tolerance option
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkenos committed Aug 24, 2024
1 parent b5554d4 commit 88fcba8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
16 changes: 15 additions & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,21 @@ export type KeyValuePair = { key: string; value: string };

export type MergeTuple<A extends any[], B extends any[]> = [...A, ...B];

export type Constructor<T> = new(...args: any) => T;
export type Constructor<T> = new (...args: any) => T;

/** @see[Exactify](https://github.com/microsoft/TypeScript/issues/12936#issuecomment-368244671) */
export type ExcludePropertiesOf<X extends T, T> = T & { [K in keyof X]: K extends keyof T ? X[K] : never }

/** @see [Nested Omit](https://stackoverflow.com/a/78575949) */
export type NestedOmit<
Schema,
Path extends string
> = Path extends `${infer Head}.${infer Tail}`
? Head extends keyof Schema
? {
[K in keyof Schema]: K extends Head
? NestedOmit<Schema[K], Tail>
: Schema[K];
}
: Schema
: Omit<Schema, Path>;
28 changes: 17 additions & 11 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import * as path from "path";
import callsites from "callsites";
import dotenv from "dotenv";

import type { Config } from "./types";
import type { Config as BaseConfig } from "./types";
import type { NestedOmit } from "../common/types";

export * from "./types";

type Config = NestedOmit<BaseConfig, "snapshots.images.outDir">;

function loadEnv(baseDir: string) {
const { NODE_ENV } = process.env;
dotenv.config({ path: NODE_ENV ? path.join(baseDir, `.env.${NODE_ENV}`) : path.join(baseDir, ".env") });
Expand Down Expand Up @@ -96,24 +99,27 @@ function getConfigContextOptions(overrides: Partial<Config>) {
}

function getConfigSnapshots(overrides: Partial<Config>) {
const [ actualDir, expectedDir, diffDir ] = ["actual", "expected", "diff"];
const { snapshots } = overrides;
const snapshotsDir = getConfigSnapshotsDir(overrides);
const snapshots = {
const snapshotOptions = {
images: {
outDir: "images",
skipCompare: false,
mask: [],
maxDiffPixelRatio: 0
maxDiffPixelRatio: 0,
...snapshots?.images
}
};
Object.keys(snapshots).forEach(key => {
snapshots[key].outDir = path.resolve(snapshotsDir, snapshots[key].outDir);
snapshots[key].actualDir = path.resolve(snapshots[key].outDir, "actual");
snapshots[key].expectedDir = path.resolve(snapshots[key].outDir, "expected");
snapshots[key].diffDir = path.resolve(snapshots[key].outDir, "diff");
fs.removeSync(snapshots[key].actualDir);
fs.removeSync(snapshots[key].diffDir);
Object.keys(snapshotOptions).forEach(key => {
snapshotOptions[key].outDir = path.resolve(snapshotsDir, snapshotOptions[key].outDir);
snapshotOptions[key].actualDir = path.resolve(snapshotOptions[key].outDir, actualDir);
snapshotOptions[key].expectedDir = path.resolve(snapshotOptions[key].outDir, expectedDir);
snapshotOptions[key].diffDir = path.resolve(snapshotOptions[key].outDir, diffDir);
fs.removeSync(snapshotOptions[key].actualDir);
fs.removeSync(snapshotOptions[key].diffDir);
});
return snapshots;
return snapshotOptions;
}

function getCukesFormat(overrides: Partial<Config>) {
Expand Down
11 changes: 8 additions & 3 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Locator } from "@fixtures/locator/types";

export type WorldParameters = { [key: string]: any };

export interface Config extends Omit<IConfiguration, "publish" | "publishQuiet"> {
export interface Config extends Omit<IConfiguration, "publishQuiet"> {
/** Custom: The base directory where most config paths will be resolved from */
baseDir: string;
baseURL: string;
Expand Down Expand Up @@ -43,19 +43,24 @@ type SnapshotDirectories = {

type SnapshotOptions = {
/** Directory to store the output of this comparable object in, relative to the config file */
outDir?: string;
readonly outDir?: string;
/** Skip comparison, just save the actual files */
skipCompare?: boolean;
} & SnapshotDirectories;

type ImageSnapshotOptions = {
/** The pixel difference tolerance level when comparing image snapshots */
maxDiffPixelRatio?: number;
/** Array of locators to mask every time image comaprison is performed */
mask?: Locator[];
} & SnapshotOptions;

export type LocatorSnapshotOptions = ImageSnapshotOptions;

export type PageSnapshotOptions = { fullPage?: boolean } & ImageSnapshotOptions;
export type PageSnapshotOptions = {
/** Whether to take a snapshot of the entire page (not just the viewport) when doing image comparison */
fullPage?: boolean
} & ImageSnapshotOptions;

type Snapshots = {
/** Options used for comparing images */
Expand Down
4 changes: 2 additions & 2 deletions src/core/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ export abstract class World extends AllureWorld implements PrivateWorld {
async createBrowserContext() {
const browserType: playwright.BrowserType = playwright[this.config.browser];
const browser = await browserType.launch(this.config.browserOptions) as any;
const from = await browser.newContext(this.config.contextOptions);
const context = new BrowserContextClass(from) as BrowserContext;
const playwrightContext = await browser.newContext(this.config.contextOptions);
const context = new BrowserContextClass(playwrightContext) as BrowserContext;
context.setDefaultTimeout(this.config.timeout);
return context;
}
Expand Down

0 comments on commit 88fcba8

Please sign in to comment.