-
Notifications
You must be signed in to change notification settings - Fork 294
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
1 parent
14dcb4d
commit 5d28f08
Showing
10 changed files
with
154 additions
and
159 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,23 @@ | ||
import pixelmatch from "pixelmatch"; | ||
import * as fs from "fs"; | ||
import { PNG } from "pngjs"; | ||
|
||
export const pixelDifference = ( | ||
referencePath: string, | ||
toMatchPath: string | ||
): PNG | null => { | ||
const reference = PNG.sync.read(fs.readFileSync(referencePath)); | ||
const toMatch = PNG.sync.read(fs.readFileSync(toMatchPath)); | ||
const { width, height } = reference; | ||
const diff = new PNG({ width, height }); | ||
|
||
const numDiffPixels = pixelmatch( | ||
reference.data, | ||
toMatch.data, | ||
diff.data, | ||
width, | ||
height | ||
); | ||
|
||
return numDiffPixels > 0 ? diff : null; | ||
}; |
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,35 @@ | ||
import * as path from "path"; | ||
import { ensureArtifactsLocation, saveDiff } from "./SnapshotLocation"; | ||
|
||
import { pixelDifference } from "./PixelDifference"; | ||
|
||
export const assertSnapshot = (snapshotPath: string, testName: string) => { | ||
const testArtifactsLocation = ensureArtifactsLocation(testName); | ||
const referenceName = path.resolve(testArtifactsLocation, `${testName}.png`); | ||
|
||
const diffPNG = pixelDifference(snapshotPath, referenceName); | ||
|
||
if (diffPNG !== null) { | ||
saveDiff(diffPNG, `${testName}_diff`); | ||
|
||
throw new Error( | ||
"There is difference between reference screenshot and test run screenshot" | ||
); | ||
} | ||
}; | ||
|
||
export const assertSnapshots = ( | ||
firstPath: string, | ||
secondPath: string, | ||
testName: string | ||
) => { | ||
const diffPNG = pixelDifference(firstPath, secondPath); | ||
|
||
if (diffPNG !== null) { | ||
saveDiff(diffPNG, `${testName}_diff.png`); | ||
|
||
throw new Error( | ||
"There is difference between reference screenshot and test run screenshot" | ||
); | ||
} | ||
}; |
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,56 @@ | ||
import * as path from "path"; | ||
import * as fs from "fs"; | ||
import detox from "detox"; | ||
import { PNG } from "pngjs"; | ||
|
||
const ROOT_PATH = path.resolve(__dirname, ".."); | ||
|
||
const artifactsLocation = (testCaseName: string): string => { | ||
const platform = detox.device.getPlatform(); | ||
const location = path.resolve( | ||
ROOT_PATH, | ||
`e2e/artifacts/${platform}`, | ||
testCaseName | ||
); | ||
|
||
return location; | ||
}; | ||
|
||
export const ensureArtifactsLocation = (name: string): string => { | ||
const location = artifactsLocation(name); | ||
|
||
if (!fs.existsSync(location)) { | ||
fs.mkdirSync(location, { recursive: true }); | ||
} | ||
|
||
return location; | ||
}; | ||
|
||
export const wipeArtifactsLocation = (name: string) => { | ||
const location = artifactsLocation(name); | ||
|
||
if (fs.existsSync(location)) { | ||
fs.rmdirSync(location, { recursive: true }); | ||
} | ||
}; | ||
|
||
export const saveDiff = (diff: PNG, testName: string) => { | ||
const diffsLocation = ensureArtifactsLocation(`diffs`); | ||
const diffPath = path.resolve(diffsLocation, testName); | ||
fs.writeFileSync(diffPath, PNG.sync.write(diff)); | ||
}; | ||
|
||
export const saveReference = (referencePath: string, testName: string) => { | ||
const testArtifactsLocation = ensureArtifactsLocation(testName); | ||
const referenceName = path.resolve(testArtifactsLocation, `${testName}.png`); | ||
|
||
fs.renameSync(referencePath, referenceName); | ||
|
||
console.log(`Reference screenshot for test name "${testName}" was created`); | ||
}; | ||
|
||
export const referenceExists = (testName: string): string | null => { | ||
const testArtifactsLocation = ensureArtifactsLocation(testName); | ||
const referenceName = path.resolve(testArtifactsLocation, `${testName}.png`); | ||
return fs.existsSync(referenceName) ? referenceName : null; | ||
}; |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
{ | ||
"extends": "../shared/tsconfig.base.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"tsBuildInfoFile": "dist/tsconfig.tsbuildinfo", | ||
"outDir": "./dist", | ||
"rootDir": "./src" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../" | ||
} | ||
] | ||
} | ||
"extends": "../shared/tsconfig.base.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"tsBuildInfoFile": "dist/tsconfig.tsbuildinfo", | ||
"outDir": "./dist", | ||
"rootDir": "./src" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../" | ||
} | ||
] | ||
} |