Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for snapshot path names #20

Merged
merged 1 commit into from
Aug 3, 2018
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
5 changes: 0 additions & 5 deletions __tests__/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

global.Cypress = {
config: () => 'cheese',
env: () => false,
Commands: {
add: jest.fn(),
Expand All @@ -25,7 +24,6 @@ const {
const defaultOptions = {
failureThreshold: 0,
failureThresholdType: 'pixel',
fileNameTransform: () => 'snap',
};

const boundMatchImageSnapshot = matchImageSnapshotCommand(defaultOptions).bind({
Expand All @@ -43,13 +41,10 @@ describe('command', () => {
boundMatchImageSnapshot(subject, commandOptions);

expect(cy.task).toHaveBeenCalledWith('Matching image snapshot', {
screenshotsFolder: 'cheese',
fileServerFolder: 'cheese',
updateSnapshots: false,
options: {
failureThreshold: 10,
failureThresholdType: 'pixel',
fileNameTransform: expect.any(Function),
},
});
});
Expand Down
8 changes: 3 additions & 5 deletions __tests__/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@ jest.mock('fs', () => ({ readFileSync: () => 'cheese' }));
describe('plugin', () => {
it('should pass options through', () => {
const options = {
screenshotsFolder: '/screenshots',
fileServerFolder: '/fileserver',
updateSnapshots: true,
};

matchImageSnapshotOptions(options);

const result = matchImageSnapshotPlugin({ path: 'cheese', name: 'snap' });
const result = matchImageSnapshotPlugin({ path: '/path/to/cheese' });
expect(result).toEqual({ path: '/path/to/diff' });
expect(diffImageToSnapshot).toHaveBeenCalledWith({
snapshotsDir: 'cheese',
snapshotsDir: '/path/to',
updateSnapshot: true,
receivedImageBuffer: 'cheese',
snapshotIdentifier: 'snap',
snapshotIdentifier: 'cheese',
failureThreshold: 0,
failureThresholdType: 'pixel',
});
Expand Down
10 changes: 9 additions & 1 deletion examples/cypress/integration/App.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
describe('App', () => {
it('should visit the app', () => {
it('should render on desktop', () => {
cy.visit('http://localhost:3000');

cy.matchImageSnapshot();
});

it('should render on mobile', () => {
cy.visit('http://localhost:3000');

cy.viewport(375, 812);
cy.matchImageSnapshot('mobile/app');
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"examples": "concurrently \"npm run start\" \"npm run cypress:open\""
},
"devDependencies": {
"cypress": "^3.0.2",
"cypress": "^3.0.3",
"cypress-image-snapshot": "^1.0.2"
}
}
30 changes: 13 additions & 17 deletions examples/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ async-each@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"

async@2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/async/-/async-2.1.4.tgz#2d2160c7788032e4dd6cbe2502f1f9a2c8f6cde4"
async@2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/async/-/async-2.4.0.tgz#4990200f18ea5b837c2cc4f8c031a6985c385611"
dependencies:
lodash "^4.14.0"

Expand Down Expand Up @@ -2003,9 +2003,9 @@ cypress-image-snapshot@^1.0.2:
jest-image-snapshot "2.4.3"
lodash.kebabcase "^4.1.1"

cypress@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/cypress/-/cypress-3.0.2.tgz#90caef84c91bd52b9cdf123aa76213249a289694"
cypress@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/cypress/-/cypress-3.0.3.tgz#b8d64d36a8c642f5343f826b6ebac312eed5a51d"
dependencies:
"@cypress/listr-verbose-renderer" "0.4.1"
"@cypress/xvfb" "1.2.3"
Expand All @@ -2030,13 +2030,13 @@ cypress@^3.0.2:
executable "4.1.1"
extract-zip "1.6.6"
fs-extra "4.0.1"
getos "2.8.4"
getos "3.1.0"
glob "7.1.2"
is-ci "1.0.10"
is-installed-globally "0.1.0"
lazy-ass "1.6.0"
listr "0.12.0"
lodash "4.17.4"
lodash "4.17.10"
log-symbols "2.2.0"
minimist "1.2.0"
progress "1.1.8"
Expand Down Expand Up @@ -3173,11 +3173,11 @@ get-value@^2.0.3, get-value@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28"

getos@2.8.4:
version "2.8.4"
resolved "https://registry.yarnpkg.com/getos/-/getos-2.8.4.tgz#7b8603d3619c28e38cb0fe7a4f63c3acb80d5163"
getos@3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/getos/-/getos-3.1.0.tgz#db3aa4df15a3295557ce5e81aa9e3e5cdfaa6567"
dependencies:
async "2.1.4"
async "2.4.0"

getpass@^0.1.1:
version "0.1.7"
Expand Down Expand Up @@ -4619,11 +4619,7 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

lodash@4.17.4:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

"lodash@>=3.5 <5", lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.3.0, lodash@^4.5.1:
lodash@4.17.10, "lodash@>=3.5 <5", lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.3.0, lodash@^4.5.1:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

Expand Down
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"prebuild": "rimraf *.js",
"build": "babel src --out-dir .",
"test": "jest",
"lint": "eslint src",
"prepublish": "npm run build",
"precommit": "lint-staged"
},
Expand All @@ -23,6 +24,11 @@
"babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"eslint": "^5.2.0",
"eslint-config-airbnb-base": "^13.0.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-cypress": "^2.0.1",
"eslint-plugin-import": "^2.13.0",
"husky": "^0.14.3",
"jest": "^23.2.0",
"lint-staged": "^7.2.0",
Expand All @@ -49,6 +55,18 @@
"singleQuote": true,
"trailingComma": "es5"
},
"eslintConfig": {
"extends": [
"airbnb-base",
"prettier"
],
"plugins": [
"cypress"
],
"env": {
"cypress/globals": true
}
},
"babel": {
"presets": [
[
Expand Down
22 changes: 7 additions & 15 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,28 @@
* LICENSE file in the root directory of this source tree.
*/

import kebabCase from 'lodash.kebabcase';
import { MATCH, RECORD } from './constants';

const screenshotsFolder = Cypress.config('screenshotsFolder');
const fileServerFolder = Cypress.config('fileServerFolder');
const updateSnapshots = Cypress.env('updateSnapshots') || false;

const defaultFileNameTransform = (name = '', test) =>
kebabCase(`${test.title}-${name}`);

export function matchImageSnapshotCommand(defaultOptions) {
return function matchImageSnapshot(subject, name, commandOptions) {
return function matchImageSnapshot(subject, maybeName, commandOptions) {
const options = {
...defaultOptions,
...((typeof name === 'string' ? commandOptions : name) || {}),
...((typeof maybeName === 'string' ? commandOptions : maybeName) || {}),
};

const { fileNameTransform = defaultFileNameTransform } = options;
const fileName = fileNameTransform(name, this.test);
cy.task('Matching image snapshot', {
screenshotsFolder,
fileServerFolder,
cy.task(MATCH, {
updateSnapshots,
options,
});

const name = typeof maybeName === 'string' ? maybeName : undefined;
const target = subject ? cy.wrap(subject) : cy;
target.screenshot(fileName, options);
target.screenshot(name, options);

return cy
.task('Recording snapshot results')
.task(RECORD)
.then(
({
pass,
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MATCH = 'Matching image snapshot';
export const RECORD = 'Recording snapshot results';
27 changes: 12 additions & 15 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import fs from 'fs';
import { diffImageToSnapshot } from 'jest-image-snapshot/src/diff-snapshot';
import kebabCase from 'lodash.kebabcase';

let snapshotOptions = {};
let snapshotResults = {};
Expand All @@ -23,17 +24,12 @@ export function matchImageSnapshotResults() {
return snapshotResults;
}

export function matchImageSnapshotPlugin({
path: screenshotsPath,
name: snapshotIdentifier,
}) {
export function matchImageSnapshotPlugin({ path: screenshotPath }) {
if (!snapshotRunning) {
return;
return null;
}

const {
screenshotsFolder,
fileServerFolder,
updateSnapshots,
options: {
failureThreshold = 0,
Expand All @@ -42,13 +38,14 @@ export function matchImageSnapshotPlugin({
} = {},
} = snapshotOptions;

const receivedImageBuffer = fs.readFileSync(screenshotsPath);
const screenshotName = `${snapshotIdentifier}.png`;
const snapshotName = `${snapshotIdentifier}-snap.png`;
const snapshotsPath = screenshotsPath
.replace('screenshots', 'snapshots')
.replace(screenshotName, snapshotName);
const snapshotsDir = snapshotsPath.replace(snapshotName, '');
const receivedImageBuffer = fs.readFileSync(screenshotPath);
const screenshotFileName = screenshotPath.slice(
screenshotPath.lastIndexOf('/')
);
const screenshotDir = screenshotPath.replace(screenshotFileName, '');
const snapshotIdentifier = kebabCase(screenshotFileName.replace('.png', ''));
const snapshotsDir = screenshotDir.replace('screenshots', 'snapshots');
const snapshotPath = `${snapshotsDir}/${snapshotIdentifier}-snap.png`;

snapshotResults = diffImageToSnapshot({
snapshotsDir,
Expand All @@ -69,7 +66,7 @@ export function matchImageSnapshotPlugin({
}

return {
path: snapshotsPath,
path: snapshotPath,
};
}

Expand Down
Loading