Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/matchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,17 @@ describe('matchers.ts', () => {
expect(result.pass).toBe(false);
expect(writeFileMock).toHaveBeenCalledTimes(1);
});

it('should return early, skipping the comparison if the latestPath is the baseline path (fresh screenshot)', () => {
const latestPath = 'baseline/ios/screen.png';

const result = toMatchBaseline(latestPath);

expect(result.message()).toBe(
'Generated a fresh baseline, skipping comparison.'
);
expect(result.pass).toBe(true);
expect(writeFileMock).toHaveBeenCalledTimes(0);
});
});
});
7 changes: 7 additions & 0 deletions src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export const toMatchBaseline = (latestPath: string) => {
path.basename(latestPath)
);

if (latestPath === baselinePath) {
return {
message: () => 'Generated a fresh baseline, skipping comparison.',
pass: true,
};
}

const diffPath = path.join(
screenshotsDir,
'diff',
Expand Down
31 changes: 31 additions & 0 deletions src/take-screenshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import execa from 'execa';
import path from 'path';

import { takeScreenshot } from './take-screenshot';
import * as fileExistsHelpers from './utils/file-exists';

const SCREENSHOT_FILENAME = 'screen';

Expand Down Expand Up @@ -75,6 +76,8 @@ describe('take-screenshot.ts', () => {
});

it('should take a screenshot', async () => {
jest.spyOn(fileExistsHelpers, 'fileExists').mockResolvedValueOnce(true);

await takeScreenshot(SCREENSHOT_FILENAME);

expect(commandMock).toHaveBeenCalledWith(
Expand All @@ -86,6 +89,19 @@ describe('take-screenshot.ts', () => {
}
);
});

it('should take a screenshot - baseline does not exist', async () => {
await takeScreenshot(SCREENSHOT_FILENAME);

expect(commandMock).toHaveBeenCalledWith(
'xcrun simctl io booted screenshot screen.png',
{
cwd: path.join(process.cwd(), '.owl', 'baseline', 'ios'),
shell: false,
stdio: 'ignore',
}
);
});
});

describe('Android', () => {
Expand All @@ -95,6 +111,8 @@ describe('take-screenshot.ts', () => {
});

it('should take a screenshot', async () => {
jest.spyOn(fileExistsHelpers, 'fileExists').mockResolvedValueOnce(true);

await takeScreenshot(SCREENSHOT_FILENAME);

expect(commandMock).toHaveBeenCalledWith(
Expand All @@ -106,6 +124,19 @@ describe('take-screenshot.ts', () => {
}
);
});

it('should take a screenshot - baseline does not exist', async () => {
await takeScreenshot(SCREENSHOT_FILENAME);

expect(commandMock).toHaveBeenCalledWith(
'adb exec-out screencap -p > screen.png',
{
cwd: path.join(process.cwd(), '.owl', 'baseline', 'android'),
shell: true,
stdio: 'ignore',
}
);
});
});
});
});
8 changes: 6 additions & 2 deletions src/take-screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import execa from 'execa';
import { promises as fs } from 'fs';
import path from 'path';

import { Platform } from './cli/types';
import { fileExists } from './utils/file-exists';
import { Logger } from './logger';
import { Platform } from './cli/types';

/**
* Takes a screenshot from the simulator.
Expand All @@ -22,7 +23,10 @@ export const takeScreenshot = async (filename: string): Promise<string> => {
const screenshotsDirPath = path.join(process.cwd(), '.owl');
await fs.mkdir(screenshotsDirPath, { recursive: true });

const DIR_NAME = updateBaseline ? 'baseline' : 'latest';
const baselineExist = await fileExists(
path.join(screenshotsDirPath, 'baseline', platform, screenshotFilename)
);
const DIR_NAME = updateBaseline || !baselineExist ? 'baseline' : 'latest';
const cwd = path.join(screenshotsDirPath, DIR_NAME, platform);
await fs.mkdir(cwd, { recursive: true });

Expand Down
10 changes: 10 additions & 0 deletions src/utils/file-exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { promises as fs } from 'fs';

export const fileExists = async (filePath: string): Promise<boolean> => {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
};