cypress-plugin-snapshots
Plugin for snapshot tests in Cypress.io.
Installation
npm i cypress-plugin-snapshots -S
Usage for text snapshots
describe('data test', () => {
it('toMatchSnapshot - JSON', () => {
return cy.request('data.json')
.its('body')
.toMatchSnapshot();
});
it('toMatchSnapshot - JSON with options', () => {
return cy.request('data.json')
.its('body')
.toMatchSnapshot({
ignoreExtraFields: true,
});
});
it('toMatchSnapshot - HTML', () => {
cy.visit('page.html')
.then(() => {
cy.get('div').toMatchSnapshot();
});
});
});
You can pass the following options to toMatchSnapshot
to override default behavior.
{
"ignoreExtraFields": false,
"ignoreExtraArrayItems": false,
"normalizeJson": true,
"replace": {
"key": "value",
}
}
replace
Use replace
with caution. Tests should be deterministic. It's often a better solution to influence your
test result instead of your snapshot (by mocking data for example).
Usage for image snapshots
it('toMatchImageSnapshot - element', () => {
cy.visit('/static/stub.html')
.then(() => {
cy.get('[data-test=test]')
.toMatchImageSnapshot();
});
});
it('toMatchImageSnapshot - whole page', () => {
cy.visit('/static/stub.html')
.then(() => {
cy.document()
.toMatchImageSnapshot();
});
});
You can pass the following options to toMatchImageSnapshot
to override default behavior.
{
"imageConfig": {
"createDiffImage": true,
"threshold": 0.01,
"thresholdType": "percent",
},
"name": "custom image name",
"separator": "custom image separator",
}
You can also use any option from the cypress.screenshot
arguments list.
For example:
cy.get('.element')
.toMatchImageSnapshot({
clip: { x: 0, y: 0, width: 100, height: 100 },
});
Configure Cypress.io
Add this to your cypress.json
configuration file:
"ignoreTestFiles": [
"**/__snapshots__/*",
"**/__image_snapshots__/*"
]
Plugin
Find your cypress/plugins/index.js
file and change it to look like this:
const { initPlugin } = require('cypress-plugin-snapshots/plugin');
module.exports = (on, config) => {
initPlugin(on, config);
return config;
};
Command
Find your cypress/support/index.js
file and add the following line:
import 'cypress-plugin-snapshots/commands';
Make changes to default configuration
You can customize the configuration in the cypress.json
file in the root of your Cypress project.
Add the configuration below to your cypress.json
file to make changes to the default values.
"env": {
"cypress-plugin-snapshots": {
"autoCleanUp": false,
"autopassNewSnapshots": true,
"diffLines": 3,
"excludeFields": [],
"ignoreExtraArrayItems": false,
"ignoreExtraFields": false,
"normalizeJson": true,
"prettier": true,
"imageConfig": {
"createDiffImage": true,
"resizeDevicePixelRatio": true,
"threshold": 0.01,
"thresholdType": "percent"
},
"screenshotConfig": {
"blackout": [],
"capture": "fullPage",
"clip": null,
"disableTimersAndAnimations": true,
"log": false,
"scale": false,
"timeout": 30000
},
"serverEnabled": true,
"serverHost": "localhost",
"serverPort": 2121,
"updateSnapshots": false,
"backgroundBlend": "difference"
}
}
Caveats :warning:
There is currently an issue when running "All Tests" in Cypress with this plugin. You can follow the progress on the issue here and here. When running "All Tests" any tests that utilize cypress-plugin-snapshots
will throw an error.
Roadmap
Below is a list of functionality that is under consideration for implementing in a next version.
- Fix handling of "update snapshot" button that contains a replacable field
- Disable "update snapshots" server in headless mode
- Add more unit tests
- Add JSDoc documentation
- Improve TypeScript bindings
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style.
License
This plugin is released under the MIT license.