To test if a website is looking the same, a lot of tools are using image comparement. But this leads to problem if a team has different environments for example Mac or Windows and even inside the system an update of a browser can break all the images, because of some render changes.
The module jest-element-style-snapshot makes a snapshot of a given elemennt with all styles inside by using the browser function getComputedStyle
With this approach it is possible to find visual changes by only checking the style.
Under the hood jest-element-style-snapshot is using pupetter and is only useable by jest
npm i jest-element-style-snapshot --save-dev
If you dont have globalSetup file for jest create one
globalSetup: '<rootDir>/src/globalSetupIntegrationTests.js',
Inside ./src/globalSetupIntegrationTests.js
require('expect-puppeteer');
const {
configureToMatchElementStyleSnapshotFromPage,
} = require('jest-element-style-snapshot');
expect.extend(configureToMatchElementStyleSnapshotFromPage());
Inside one of your tests you can use the global pupeeteer page object for querying the needed element.
Example:
describe("when rendering a button", () => {
it("should always look the same", () => {
const page = await browser.newPage();
await page.goto('https://localhost:3000');
await page.waitForSelector('#mybutton');
const element = await page.$('#mybutton');
await expect(element).toMatchElementStyleSnapshotFromPage(page);
});
});