-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
145 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import sharp from 'sharp'; | ||
|
||
import {mkDir} from '../../utils/directory'; | ||
import {getDiffDir, getSnapshotDir, buildSnapshotName} from '../../utils/image-snapshot.name'; | ||
import {SharpService} from './image-snapshot.sharp'; | ||
|
||
import type {SharpContext} from './image-snapshot.sharp'; | ||
|
||
export interface SnapshotData { | ||
snapshotPath: string; | ||
diffPath: string; | ||
diffDir: string; | ||
|
||
current: { | ||
img: sharp.Sharp; | ||
buffer: SharpContext; | ||
}; | ||
|
||
previous: { | ||
buffer: SharpContext | undefined; | ||
}; | ||
} | ||
|
||
export class SnapshotDataProcessor { | ||
public static async process(context: jest.MatcherContext, received: Buffer): Promise<SnapshotData> { | ||
const {testPath, currentTestName} = context; | ||
|
||
const snapshotDir = getSnapshotDir(testPath!); | ||
const diffDir = getDiffDir(testPath!); | ||
mkDir(snapshotDir); | ||
|
||
const snapshotPath = path.join(snapshotDir, buildSnapshotName(currentTestName!)); | ||
const diffPath = path.join(diffDir, buildSnapshotName(currentTestName!, 'diff')); | ||
|
||
const shouldUpdate = !fs.existsSync(snapshotPath) || context.snapshotState._updateSnapshot === 'all'; | ||
const recievedJPG = SharpService.toJPEG(received); | ||
return { | ||
diffPath, | ||
snapshotPath, | ||
diffDir, | ||
|
||
current: { | ||
img: recievedJPG, | ||
buffer: await (SharpService.toRawBuffered(await recievedJPG.toBuffer())) | ||
}, | ||
|
||
previous: { | ||
buffer: shouldUpdate ? undefined : await (SharpService.toRawBuffered(snapshotPath)) | ||
} | ||
}; | ||
} | ||
|
||
public static async saveDiffImage(data: SnapshotData, diffBuffer: Buffer): Promise<string | undefined> { | ||
const {current, previous, diffDir, diffPath} = data; | ||
const {width, height} = previous.buffer!.info; | ||
mkDir(diffDir); | ||
|
||
const rawOptions = {raw: {width, height, channels: 4}}; | ||
await sharp({ | ||
create: { | ||
width: width * 3, | ||
height, | ||
channels: 4, | ||
background: {r: 0, g: 0, b: 0, alpha: 0} | ||
} | ||
}) | ||
.composite([ | ||
{input: await SharpService.toJPEGBufferd(previous.buffer!.data, rawOptions), left: 0, top: 0}, | ||
{input: await SharpService.toJPEGBufferd(diffBuffer, rawOptions), left: width, top: 0}, | ||
{input: await SharpService.toJPEGBufferd(current.buffer.data, rawOptions), left: width * 2, top: 0} | ||
]) | ||
.jpeg() | ||
.toFile(diffPath); | ||
|
||
return diffPath; | ||
} | ||
} |
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
e2e/setup/serializers/image.ts → e2e/setup/serializers/image-snapshot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export declare function mkDir(dirPath: string): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const fs = require('fs'); | ||
|
||
function mkDir(dirPath) { | ||
if (!fs.existsSync(dirPath)) fs.mkdirSync(dirPath, {recursive: true}); | ||
} | ||
|
||
module.exports = { | ||
mkDir | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export declare function buildSnapshotName(...snapshotParts: string[]): string; | ||
|
||
export declare function getSnapshotDir(testPath: string): string; | ||
|
||
export declare function getDiffDir(testPath: string): string; | ||
|
||
export declare function getDirName(testPath: string): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const path = require('path'); | ||
const jestConfig = require(path.resolve(process.cwd(), 'jest.config.js')); | ||
|
||
function buildSnapshotName(...snapshotParts) { | ||
const sanitize = (str) => (str || '').replace(/\W+/g, '-').toLowerCase(); | ||
return snapshotParts.map(sanitize).join('-') + '.jpg'; | ||
} | ||
|
||
const getRootDir = (testPath) => jestConfig.roots.find((root) => path.resolve(testPath).includes(path.resolve(root))); | ||
|
||
function getDirName(testPath) { | ||
const rootDir = getRootDir(testPath); | ||
const basePath = path.resolve(rootDir); | ||
return path.relative(basePath, testPath) | ||
} | ||
|
||
function getSnapshotDir(testPath) { | ||
const snapshotDir = path.resolve(getRootDir(testPath), '__image_snapshots__'); | ||
return path.join(snapshotDir, getDirName(testPath)); | ||
} | ||
|
||
function getDiffDir(testPath) { | ||
const diffDir = path.resolve('.diff'); | ||
return path.join(diffDir, getDirName(testPath)); | ||
} | ||
|
||
module.exports = { | ||
buildSnapshotName, | ||
getSnapshotDir, | ||
getDiffDir, | ||
getDirName | ||
}; |